简体   繁体   English

没有玩家 2 采取行动,基本井字游戏不会获胜

[英]basic Tic tac toe game not winning without player 2 making a move

I am a beginner in c++ and am taking a course at my university, I am creating a C++ [rogram for tic tac toe and im having some struggles I cannot figure out how to make the game stop when player 1 wins at the moment if player 1 wins player 2 still has to take their next turn to make the game output Player 1 wins... I will include the code below.我是 c++ 的初学者,正在我的大学上一门课程,我正在创建一个 C++ [井字游戏,我遇到了一些困难,如果玩家 1 获胜,我无法弄清楚如何让游戏停止1 赢玩家 2 仍然需要下一轮来制作游戏 output 玩家 1 赢...我将包含下面的代码。 also I need a user defined function and can't think of one that would be good to use besides displaying an empty board if anyone has any ideas that would be great.我还需要一个用户定义的 function 并且如果有人有任何很棒的想法,除了显示一个空板之外,我想不出一个很好用的。 Thanks for the help in advance!!我在这里先向您的帮助表示感谢!!

#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void gameBoard() {
  cout << " Reference:" << endl;
  cout << " | | "
       << " 1 2 3" << endl;
  cout << " | | " << endl;
  cout << "______|________|______ " << endl;
  cout << " | | "
       << " 4 5 6" << endl;
  cout << " | | " << endl;
  cout << "______|________|______ " << endl;
  cout << " | | "
       << " 7 8 9" << endl;
  cout << " | | " << endl;
}
int main(int argc, const char* argv[]) {
  string player1Name;
  string player2Name;
  int j;
  int k;
  int l;
  int userInput;
  const int NUM_ELEMENTS = 10;
  vector<char> boxVals(NUM_ELEMENTS);
  unsigned int i;
  for (i = 0; i < boxVals.size(); ++i) {
    boxVals.at(i) = ' ';
  }
  cout << "enter Player 1's Name: ";
  cin >> player1Name;
  cout << "enter Player 2's Name: ";
  cin >> player2Name;
  gameBoard();
  for (k = 0; k < 10; k++) {
    for (j = 0; j < 10; j++) {
      cin >> userInput;
      boxVals.at(userInput) = 'x';
      break;
    }
    cout << " Reference:" << endl;
    cout << " | | "
         << " 1 2 3" << endl;
    cout << " " << boxVals.at(1) << " | " << boxVals.at(2) << " | "
         << boxVals.at(3) << " " << endl;
    cout << "______|________|______ " << endl;
    cout << " | | "
         << " 4 5 6" << endl;
    cout << " " << boxVals.at(4) << " | " << boxVals.at(5) << " | "
         << boxVals.at(6) << " " << endl;
    cout << "______|________|______ " << endl;
    cout << " | | "
         << " 7 8 9" << endl;
    cout << " " << boxVals.at(7) << " | " << boxVals.at(8) << " | "
         << boxVals.at(9) << " " << endl;
    for (l = 0; l < 10; l++) {
      cin >> userInput;
      if (boxVals.at(userInput != 'x')) {
        boxVals.at(userInput) = 'O';
      } else if (boxVals.at(userInput) == 'x') {
        cout << "invalid move" << endl;
        continue;
      }
      break;
    }
    cout << " Reference:" << endl;
    cout << " | | "
         << " 1 2 3" << endl;
    cout << " " << boxVals.at(1) << " | " << boxVals.at(2) << " | "
         << boxVals.at(3) << " " << endl;
    cout << "______|________|______ " << endl;
    cout << " | | "
         << " 4 5 6" << endl;
    cout << " " << boxVals.at(4) << " | " << boxVals.at(5) << " | "
         << boxVals.at(6) << " " << endl;
    cout << "______|________|______ " << endl;
    cout << " | | "
         << " 7 8 9" << endl;
    cout << " " << boxVals.at(7) << " | " << boxVals.at(8) << " | "
         << boxVals.at(9) << " " << endl;
    if ((boxVals.at(1) == 'x' && boxVals.at(2) == 'x' &&
         boxVals.at(3) == 'x') ||
        (boxVals.at(4) == 'x' && boxVals.at(5) == 'x' &&
         boxVals.at(6) == 'x') ||
        (boxVals.at(7) == 'x' && boxVals.at(8) == 'x' &&
         boxVals.at(9) == 'x') ||
        (boxVals.at(1) == 'x' && boxVals.at(5) == 'x' &&
         boxVals.at(9) == 'x') ||
        (boxVals.at(3) == 'x' && boxVals.at(5) == 'x' &&
         boxVals.at(7) == 'x') ||
        (boxVals.at(1) == 'x' && boxVals.at(4) == 'x' &&
         boxVals.at(7) == 'x') ||
        (boxVals.at(2) == 'x' && boxVals.at(5) == 'x' &&
         boxVals.at(8) == 'x') ||
        (boxVals.at(3) == 'x' && boxVals.at(6) == 'x' &&
         boxVals.at(9) == 'x')) {
      cout << player1Name << " Wins!!" << endl;
      break;
    }
    if ((boxVals.at(1) == 'O' && boxVals.at(2) == 'O' &&
         boxVals.at(3) == 'O') ||
        (boxVals.at(4) == 'O' && boxVals.at(5) == 'O' &&
         boxVals.at(6) == 'O') ||
        (boxVals.at(7) == 'O' && boxVals.at(8) == 'O' &&
         boxVals.at(9) == 'O') ||
        (boxVals.at(1) == 'O' && boxVals.at(5) == 'O' &&
         boxVals.at(9) == 'O') ||
        (boxVals.at(3) == 'O' && boxVals.at(5) == 'O' &&
         boxVals.at(7) == 'O') ||
        (boxVals.at(1) == 'O' && boxVals.at(4) == 'O' &&
         boxVals.at(7) == 'O') ||
        (boxVals.at(2) == 'O' && boxVals.at(5) == 'O' &&
         boxVals.at(8) == 'O') ||
        (boxVals.at(3) == 'O' && boxVals.at(6) == 'O' &&
         boxVals.at(9) == 'O')) {
      cout << player2Name << " Wins!" << endl;
      break;
    }
  }
  return 0;
}

please ignore the probably terrible code this is some of my best work so far请忽略可能很糟糕的代码这是我迄今为止最好的一些工作

The simple explanation is that you don't check for a win condition until after player2 has gone.简单的解释是直到player2离开后才检查获胜条件。 You have your loop to take player1 's input, then it looks like you display the board, then you have the loop to take player2 's input, and then you check if a player won.你有你的循环来接受player1的输入,然后看起来你显示了棋盘,然后你有循环来接受player2的输入,然后你检查一个玩家是否赢了。

Move the win-checking code to a function, and call that function after each player's turn.将获胜检查代码移动到 function,并在每个玩家回合后调用该 function。 It might look something like this (pseudocode):它可能看起来像这样(伪代码):

bool is_game_over(char player) {
  if (player == 'x' && <your code to check for x winning>) {
    <your code that returns true or false>
  }

  if (player == 'o' && <your code to check for o winning>) {
    <your code that returns true or false>
  }

  return false;
}

I would recommend breaking up each 'phase' of the game into its own function.我建议将游戏的每个“阶段”分解为自己的 function。 Player input is its own function, win checking as its own, board drawing as its own, etc. It will make your code easier to debug and tweak, and you'll be able to follow the overall game logic a lot easier.玩家输入是它自己的 function,它自己的胜利检查,它自己的棋盘绘图等。它将使您的代码更易于调试和调整,并且您将能够更轻松地遵循整体游戏逻辑。

You must merge your two loops waiting for player's input into one by using a char variable for the expected output.您必须使用预期 output 的 char 变量将等待玩家输入的两个循环合并为一个。 It would looks like:它看起来像:


#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void gameBoard() {
  cout << " Reference:" << endl;
  cout << " | | "
       << " 1 2 3" << endl;
  cout << " | | " << endl;
  cout << "______|________|______ " << endl;
  cout << " | | "
       << " 4 5 6" << endl;
  cout << " | | " << endl;
  cout << "______|________|______ " << endl;
  cout << " | | "
       << " 7 8 9" << endl;
  cout << " | | " << endl;
}
int main(int argc, const char* argv[]) {
  string player1Name;
  string player2Name;
  int j;
  int k;
  int l;
  int userInput;
  const int NUM_ELEMENTS = 10;
  vector<char> boxVals(NUM_ELEMENTS);
  unsigned int i;
  char currentChar='x'; //tic-tac-toe begins with x
  for (i = 0; i < boxVals.size(); ++i) {
    boxVals.at(i) = ' ';
  }
  cout << "enter Player 1's Name: ";
  cin >> player1Name;
  cout << "enter Player 2's Name: ";
  cin >> player2Name;
  gameBoard();
  for (k = 0; k < 10; k++) {
    for (l = 0; l < 10; l++) {
      cin >> userInput;
      if (boxVals.at(userInput != ' ')) {
        cout << "invalid move" << endl;
        continue;
        }
      else {
          boxVals.at(userInput)=currentChar;
          break;
          }
    }
    cout << " Reference:" << endl;
    cout << " | | "
         << " 1 2 3" << endl;
    cout << " " << boxVals.at(1) << " | " << boxVals.at(2) << " | "
         << boxVals.at(3) << " " << endl;
    cout << "______|________|______ " << endl;
    cout << " | | "
         << " 4 5 6" << endl;
    cout << " " << boxVals.at(4) << " | " << boxVals.at(5) << " | "
         << boxVals.at(6) << " " << endl;
    cout << "______|________|______ " << endl;
    cout << " | | "
         << " 7 8 9" << endl;
    cout << " " << boxVals.at(7) << " | " << boxVals.at(8) << " | "
         << boxVals.at(9) << " " << endl;
    if ((boxVals.at(1) == currentChar && boxVals.at(2) == currentChar &&
         boxVals.at(3) == currentChar ) ||
        (boxVals.at(4) == currentChar && boxVals.at(5) == currentChar &&
         boxVals.at(6) == currentChar ) ||
        (boxVals.at(7) == currentChar && boxVals.at(8) == currentChar &&
         boxVals.at(9) == currentChar ) ||
        (boxVals.at(1) == currentChar && boxVals.at(5) == currentChar &&
         boxVals.at(9) == currentChar ) ||
        (boxVals.at(3) == currentChar && boxVals.at(5) == currentChar &&
         boxVals.at(7) == currentChar ) ||
        (boxVals.at(1) == currentChar && boxVals.at(4) == currentChar &&
         boxVals.at(7) == currentChar ) ||
        (boxVals.at(2) == currentChar && boxVals.at(5) == currentChar &&
         boxVals.at(8) == currentChar )||
        (boxVals.at(3) == currentChar && boxVals.at(6) == currentChar &&
         boxVals.at(9) == currentChar )) {
      cout << ((currentChar=='x')?player1Name : player2Name)<< " Wins!!" << endl;
      break;
    }
  else currentChar = (currentChar == 'x')?'O':'x';
  }
  return 0;
}

Note that I did not change the general approach of your code, so you understand the changes.请注意,我没有更改您代码的一般方法,因此您了解更改。 But this is not the best you could find for this problem.但这不是你能找到的最好的解决这个问题的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM