基本的井字游戏在玩家2没有移动的情况下不能获胜 我是一个初学C++的人,我在大学里修一门课程,我正在创建一个C++。[tic-tac-toe的程序,我有一些困难,我不知道如何在玩家1获胜时停止游戏,如果玩家1获胜,玩家2还需要下一轮才能使游戏输出玩家1获胜……我将包括下面的代码。我还需要一个用户定义的函数,想不出一个好用的函数除了显示一个空板,如果有人有任何想法,这将是伟大的。感谢您的帮助提前 #包括 #包括 #包括 #包括 使用名称空间std; void gameBoard(){ cout

基本的井字游戏在玩家2没有移动的情况下不能获胜 我是一个初学C++的人,我在大学里修一门课程,我正在创建一个C++。[tic-tac-toe的程序,我有一些困难,我不知道如何在玩家1获胜时停止游戏,如果玩家1获胜,玩家2还需要下一轮才能使游戏输出玩家1获胜……我将包括下面的代码。我还需要一个用户定义的函数,想不出一个好用的函数除了显示一个空板,如果有人有任何想法,这将是伟大的。感谢您的帮助提前 #包括 #包括 #包括 #包括 使用名称空间std; void gameBoard(){ cout,c++,C++,一个简单的解释是,在player2消失之前,你不会检查是否有赢的条件。你有你的循环来获取player1的输入,然后它看起来像是你显示了棋盘,然后你有循环来获取player2的输入,然后你检查一个玩家是否赢了 将win checking代码移动到一个函数,并在每个玩家轮到后调用该函数。它可能看起来像这样(伪代码): bool游戏结束了(char玩家){ 如果(玩家=='x'&&){ } 如果(玩家=='o'&&){ } 返回false; } 我建议将游戏的每个“阶段”分解为自己的功能。玩家输入是

一个简单的解释是,在
player2
消失之前,你不会检查是否有赢的条件。你有你的循环来获取
player1
的输入,然后它看起来像是你显示了棋盘,然后你有循环来获取
player2
的输入,然后你检查一个玩家是否赢了

将win checking代码移动到一个函数,并在每个玩家轮到后调用该函数。它可能看起来像这样(伪代码):

bool游戏结束了(char玩家){
如果(玩家=='x'&&){
}
如果(玩家=='o'&&){
}
返回false;
}

我建议将游戏的每个“阶段”分解为自己的功能。玩家输入是自己的功能,胜利检查是自己的功能,棋盘画是自己的功能,等等。这将使您的代码更易于调试和调整,并且您可以更容易地遵循整个游戏逻辑。

您必须合并两个循环,等待玩家输入通过对预期输出使用char变量,将ut转换为一个。它如下所示:


#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;
}

#包括
#包括
#包括
#包括
使用名称空间std;
void gameBoard(){

cout Hi!首先,欢迎使用stackoverflow!另外,在玩家1移动并且他们“赢”之后,您是否可以添加一个if语句,仅在玩家1尚未赢的情况下执行玩家2回合?