【问题标题】:Tic Tac Toe Game Doesnt end when row one complete井字游戏在第一行完成时不会结束
【发布时间】:2011-11-08 00:22:50
【问题描述】:

我制作了一个井字游戏,但我还没有完成,但我做到了,如果我获得前三排,我会为 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;
}

【问题讨论】:

  • 您也缺少检查垂直行和诊断行。

标签: c++ arrays function


【解决方案1】:

你永远不会使用 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;
}

【讨论】:

  • 虽然我想返回 gameState 的值
  • 我认为您对按引用传递和按值传递之间的区别感到困惑。如果你想按值传递,只需返回一个整数,然后在你的主函数中使用返回值。你现在做的是在函数内部设置一个局部变量,然后返回它,但从不使用它。
  • 你能给我举个例子吗?除非我看到一个例子,否则我不确定我是否真的明白。只是一个概念
  • 在 main() 中:printBoard();cout &lt;&lt; "Player X please choose a position: ";cin &gt;&gt; choosePositionX;makeMove();printBoard();gameState = checkGameState(gameState); 在 checkGameState:{cout &lt;&lt; "YOU WIN!" &lt;&lt; endl;@98765433@rm 你可以在 cmets 中不做大代码块?哇!
  • 请务必更改我没有提到的所有内容...即使您不需要,按照下面的@sarnold 评论!
【解决方案2】:

虽然我认为Danalog's answer is the bug you're tripping,但请注意,您应该在每一步之后检查棋盘的输赢,而不是在每步之后 - 如果 X 将三分之一放在行,让O也出手是没有意义的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 2015-01-08
    相关资源
    最近更新 更多