C ++中的井字游戏帮助,如何进行循环,以便井字游戏每次都能重复游戏板

时间:2019-10-30 05:00:37

标签: c++ for-loop

我是一个初学者,所以我的代码很乱。我还没有对游戏进行完整的评论,所以如果您需要澄清一些变量,我可以为您提供。

(顺便说一下,这是一个c ++项目,要求制作tic Tac Toe游戏)

我的主要问题是,我将如何重复我的董事会(每次有人在井字游戏中移动时都会更新)?我无法考虑这样做的方法,因此如果有人给我想法,而不是直接答案,将不胜感激。

下面的“我的代码”仅使您了解我在做什么,以及是否对如何修改我的代码有任何建议(组织或错误,有100%的机会发生)。

    #include <iostream>
    using namespace std;
    char a[3][3];//sets 3x3 matrix
    a[0][0]='1';//upper row left corner is 1
            a[0][1]='2';//upper row middle is 2
            a[0][2]='3';//upper row right corner is 3
            a[1][0]='4';//middle row left is 4
            a[1][1]='5';//middle row middle is 5
            a[1][2]='6';//middle row right is 6
            a[2][0]='7';//bottom row left is 7
            a[2][1]='8';//bottom row middle is 8
            a[2][2]='9';//bottom row right is 9




            cout << "     |     |     " << endl;//all these "shapes" make the board.

            cout << "  " << a[0][0] << "  |  " << a[0][1] << "  |  " << a[0][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[1][0] << "  |  " << a[1][1] << "  |  " << a[1][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[2][0] << "  |  " << a[2][1] << "  |  " << a[2][2] << endl;

            cout << "     |     |     " << endl;




    bool match = true;//this tells the consul the match has not ended

    bool checker;//checks if you actually chose X or O
    checker=true;

    cout << "play!play!play! you'll need two people" << endl;
    cout << "decide who takes X, then press 1 to take X" << endl;
    cout << "or press 2 to take O" << endl;


    cin >> player;//so, organize will be the thing (1 or 2) that the player will put in

    char XO;//helps make X and O

            if (player == 1)
            {
                cout << "you chose X" << endl;
                XO = 'X';
            }

            else if (player == 2)
            {
                cout << "you chose O" << endl;
                XO = 'O';
            }
            else
            {
                cout << "press 1 or 2 only please" << endl;
                checker=false;
            }




        bool invalid;//if you "accidentally" put your move in an illegal square, this will help you redo a move.
        bool gameover = true;//helps differentiate between draws and wins

        int nowwestart;//starts game
        cout << "player play your move" << endl;//tells you to move it
        cin >> nowwestart;
        invalid = true;//you always make a valid move first turn.

        if (nowwestart == 1 && a[0][0] == '1')//when you place your marker on square 1, i need to tell consul that your move equals a certain square

        {
            a[0][0]=XO;
            cout << "     |     |     " << endl;//all these "shapes" make the board.

            cout << "  " << a[0][0] << "  |  " << a[0][1] << "  |  " << a[0][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[1][0] << "  |  " << a[1][1] << "  |  " << a[1][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[2][0] << "  |  " << a[2][1] << "  |  " << a[2][2] << endl;

            cout << "     |     |     " << endl;


        }

        else if (nowwestart == 2 && a[0][1] == '2')
        {
            a[0][1]=XO;
            cout << "     |     |     " << endl;//all these "shapes" make the board.

            cout << "  " << a[0][0] << "  |  " << a[0][1] << "  |  " << a[0][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[1][0] << "  |  " << a[1][1] << "  |  " << a[1][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[2][0] << "  |  " << a[2][1] << "  |  " << a[2][2] << endl;

            cout << "     |     |     " << endl;

        }

        else if (nowwestart == 3 && a[0][2] == '3')
        {
            a[0][2]=XO;
            cout << "     |     |     " << endl;//all these "shapes" make the board.

            cout << "  " << a[0][0] << "  |  " << a[0][1] << "  |  " << a[0][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[1][0] << "  |  " << a[1][1] << "  |  " << a[1][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[2][0] << "  |  " << a[2][1] << "  |  " << a[2][2] << endl;

            cout << "     |     |     " << endl;

        }

        else if (nowwestart == 4 && a[1][0] == '4')
        {
            a[1][0]=XO;
            cout << "     |     |     " << endl;//all these "shapes" make the board.

            cout << "  " << a[0][0] << "  |  " << a[0][1] << "  |  " << a[0][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[1][0] << "  |  " << a[1][1] << "  |  " << a[1][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[2][0] << "  |  " << a[2][1] << "  |  " << a[2][2] << endl;

            cout << "     |     |     " << endl;


        }

        else if (nowwestart == 5 && a[1][1] == '5')
        {
            a[1][1]=XO;
            cout << "     |     |     " << endl;//all these "shapes" make the board.

            cout << "  " << a[0][0] << "  |  " << a[0][1] << "  |  " << a[0][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[1][0] << "  |  " << a[1][1] << "  |  " << a[1][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[2][0] << "  |  " << a[2][1] << "  |  " << a[2][2] << endl;

            cout << "     |     |     " << endl;

        }

        else if (nowwestart == 6 && a[1][2] == '6')
        {
            a[1][2]=XO;         cout << "     |     |     " << endl;//all these "shapes" make the board.

            cout << "  " << a[0][0] << "  |  " << a[0][1] << "  |  " << a[0][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[1][0] << "  |  " << a[1][1] << "  |  " << a[1][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[2][0] << "  |  " << a[2][1] << "  |  " << a[2][2] << endl;

            cout << "     |     |     " << endl;

        }

        else if (nowwestart == 7 && a[2][0] == '7')
        {
            a[2][0]=XO;
            cout << "     |     |     " << endl;//all these "shapes" make the board.

            cout << "  " << a[0][0] << "  |  " << a[0][1] << "  |  " << a[0][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[1][0] << "  |  " << a[1][1] << "  |  " << a[1][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[2][0] << "  |  " << a[2][1] << "  |  " << a[2][2] << endl;

            cout << "     |     |     " << endl;

        }

        else if (nowwestart == 8 && a[2][1] == '8')
        {
            a[2][1]=XO;
            cout << "     |     |     " << endl;//all these "shapes" make the board.

            cout << "  " << a[0][0] << "  |  " << a[0][1] << "  |  " << a[0][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[1][0] << "  |  " << a[1][1] << "  |  " << a[1][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[2][0] << "  |  " << a[2][1] << "  |  " << a[2][2] << endl;

            cout << "     |     |     " << endl;

        }

        else if (nowwestart == 9 && a[2][2] == '9')
        {
            a[2][2]=XO;
            cout << "     |     |     " << endl;//all these "shapes" make the board.

            cout << "  " << a[0][0] << "  |  " << a[0][1] << "  |  " << a[0][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[1][0] << "  |  " << a[1][1] << "  |  " << a[1][2] << endl;

            cout << "_____|_____|_____" << endl;

            cout << "     |     |     " << endl;

            cout << "  " << a[2][0] << "  |  " << a[2][1] << "  |  " << a[2][2] << endl;

            cout << "     |     |     " << endl;

        }

        else
        {
            cout << "you made an invalid move, please do it again" << endl;
            invalid=false;//you made an illegal move :(
        }


        while(!invalid);



        match = false;//when match has ended...
        if (a[0][0] != '1')//all possible wins through square 1
        {
            if (a[0][0] == a[1][0] && a[1][0] == a[2][0])
            {
                match = true;
            }

            else if (a[0][0] == a[0][1] && a[0][1] == a[0][2])
            {
                match = true;
            }

            else if (a[0][0] == a[1][1] && a[1][1] == a[2][2])
            {
                match= true;
            }
        }

        if (a[0][1] != '2')//all possible wins through square 2
        {
            if (a[0][1] == a[1][1] && a[1][1] == a[2][1])
            {
                match = true;
            }
        }

        if (a[0][2] != '3')//all possible wins through square 3
        {
            if (a[0][2] == a[1][2] && a[1][2] == a[2][2])
            {
                match = true;
            }

            else if (a[0][2] == a[1][1] && a[1][1] == a[2][0])
            {
                match = true;
            }
        }

        if (a[1][0] != '4')//all possible wins through square 4
        {
            if (a[1][0] == a[1][1] && a[1][1] == a[1][2])
            {
                match = true;
            }
        }

        if (a[2][0] != '7')//all possible wins through square 7
        {
            if (a[2][0] == a[2][1] && a[2][1] == a[2][2])
            {
                match = true;
            }
        }

        else//anything beside win is draw
        {
            gameover=false;//no one won...
            match=true;//but the match is done anyway
        }

        if (match==true)//if the match is done
        {
            if (gameover==true)//if someone won
            {
                cout << "player" << player << "won" << player << endl;
            }

            cout << "the game has ended. play again? 1-yes, 2-false (press 2 please)" << endl;
            if (1)
            {
                match = false;//dang it, you are still playing. the borad is below.
                char a[3][3];//sets 3x3 matrix
                a[0][0]='1';//upper row left corner is 1
                a[0][1]='2';//upper row middle is 2
                a[0][2]='3';//upper row right corner is 3
                a[1][0]='4';//middle row left is 4
                a[1][1]='5';//middle row middle is 5
                a[1][2]='6';//middle row right is 6
                a[2][0]='7';//bottom row left is 7
                a[2][1]='8';//bottom row middle is 8
                a[2][2]='9';//bottom row right is 9

            }
            player = 1;
        }

        else
        {
            if (player == 1)
            {
                player = 2;
            }
            else
            {
                player = 1;
            }
        }




        while (!match);

        cout << endl;
        return 0;
}




int main()
{
    dot();
}

2 个答案:

答案 0 :(得分:1)

您可以使用用户输入循环整个程序。 while (getline(std::cin, input)。并使用适当的符号更新2D数组。我建议您使用一个常数来表示X和O。这样代码就会很清楚。并且我还建议您将重复的代码移至函数,这将再次提高清晰度。

答案 1 :(得分:1)

您可以添加一个while循环,并检查条件是否未赢得且未平局,例如 while( !bWon && !bDraw )

您可以在循环中向其中添加功能

  1. 选择要玩的玩家(提示:循环中的每个奇数都是 X 甚至是 O 的播放。)
  2. 一旦播放器输入。您可以计算出他是否赢了。
  3. 如果他不赢,可以算是平局。 (提示:循环将运行多少次?)
  4. 清除旧木板并显示新木板。