我有一个井字游戏,但还没有完成,但是我做到了,如果我获得前三行,我将赢得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;
}

10-07 12:34