我制作了一个井字游戏,我还没有完成,但我这样做是为了如果我获得前三行,我就赢得了 X 游戏。
但由于某种原因它不起作用。它打印出我赢了,但它并没有结束 while 循环并中断并完成游戏。
我错过了什么?
#include <iostream>
using namespace std;
// Initialize Array to 9 elements.
// Set array elements 1 to 9 to specify locations for placement of X and O.
char ticTacBoard[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
// gameState will return 1 to 4. #1. PlayerXhaswon, #2. PlayerYhaswon, #3. Draw, #4. GameNotFinished.
int gameState = 0;
int choosePositionX = 0;
int choosePositionO = 0;
// Functions declared to run game.
// Checks for winners, draw and if game should continue.
int checkGameState (int gameState);
// Takes in users moves.
void makeMove ();
// Resets board when game finished.
void reset ();
// Prints the board to the user.
void printBoard ();
int main()
{
cout << "Welcome to Tic Tac Toe!" << endl << endl
<< "The goal of Tic Tac Toe is to be the first player to get three in a row on the grid." << endl << endl
<< "Please select 1 through 9 to select placement for your turn."<< endl << endl;
int gameState = 4;
while ( gameState == 4)
{
printBoard();
cout << "Player X please choose a position: ";
cin >> choosePositionX; makeMove();
printBoard();
gameState = checkGameState(gameState);
cout << "Player O please choose a position: ";
cin >> choosePositionO;
makeMove();
printBoard();
gameState = checkGameState(gameState);
}
cout << " END";
return 0;
}
int checkGameState(int gameState)
{
// Check Rows
if(ticTacBoard[0] == 'X' && ticTacBoard[1] == 'X' && ticTacBoard[2] == 'X')
{
cout << "YOU WIN!" << endl;
return 1;
}
if(ticTacBoard[3] == 'X' && ticTacBoard[4] == 'X' && ticTacBoard[5] == 'X')
{
cout << "YOU WIN!" << endl;
return gameState = 1;
}
if(ticTacBoard[6] == 'X' && ticTacBoard[7] == 'X' && ticTacBoard[8] == 'X')
{
cout << "YOU WIN!" << endl;
return gameState = 1;
}
else
return gameState = 4;
}
void makeMove()
{
if ( choosePositionX )
{
ticTacBoard[choosePositionX - 1] = 'X';
}
if ( choosePositionO )
{
ticTacBoard[choosePositionO - 1] = 'O';
}
}
void printBoard()
{
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++)
{
cout << ticTacBoard[3 * y + x] << " ";
}
cout << endl;
}
cout << endl;
}
最佳答案
您永远不会使用 checkGameState 的返回值。您可能想要通过引用传递 gameState 并返回 void,或者执行 gameState = checkGameState(gameState)。
如果你选择前者,那么你将它们全部更改为 gameState = 1; return;
如果你选择后者,那么你只需将它们更改为 return 1;
通过引用传递版本:
#include <iostream>
using namespace std;
// Initialize Array to 9 elements.
// Set array elements 1 to 9 to specify locations for placement of X and O.
char ticTacBoard[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
// gameState will return 1 to 4. #1. PlayerXhaswon, #2. PlayerYhaswon, #3. Draw, #4. GameNotFinished.
int gameState = 0;
int choosePositionX = 0;
int choosePositionO = 0;
// Functions declared to run game.
// Checks for winners, draw and if game should continue.
int checkGameState (int gameState);
// Takes in users moves.
void makeMove ();
// Resets board when game finished.
void reset ();
// Prints the board to the user.
void printBoard ();
int main()
{
cout << "Welcome to Tic Tac Toe!" << endl << endl
<< "The goal of Tic Tac Toe is to be the first player to get three in a row on the grid." << endl << endl
<< "Please select 1 through 9 to select placement for your turn."<< endl << endl;
int gameState = 4;
while ( gameState == 4)
{
printBoard();
cout << "Player X please choose a position: ";
cin >> choosePositionX; makeMove();
printBoard();
gameState = checkGameState(gameState);
if (gameState != 4) break;
cout << "Player O please choose a position: ";
cin >> choosePositionO;
makeMove();
printBoard();
gameState = checkGameState(gameState);
}
cout << " END";
return 0;
}
int checkGameState(int gameState)
{
// Check Rows
if(ticTacBoard[0] == 'X' && ticTacBoard[1] == 'X' && ticTacBoard[2] == 'X')
{
cout << "YOU WIN!" << endl;
return 1;
}
if(ticTacBoard[3] == 'X' && ticTacBoard[4] == 'X' && ticTacBoard[5] == 'X')
{
cout << "YOU WIN!" << endl;
return 1;
}
if(ticTacBoard[6] == 'X' && ticTacBoard[7] == 'X' && ticTacBoard[8] == 'X')
{
cout << "YOU WIN!" << endl;
return 1;
}
else
return 4;
}
关于c++ - 当第一行完成时井字游戏不会结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8044531/