如果语句在不满足条件时触发?

时间:2014-04-04 18:23:19

标签: c++

我试图制作一个使用2D数组来播放Tic Tac Toe的程序。我设置了一个while循环,并在其中另外两个while循环来回传递,直到满足胜利条件。基本上,while循环继续前进,直到满足其中一个胜利条件(在完成一个完整的转弯后立即发生)。其中两个while循环表示转弯。因此,如果符合法律参数,则玩家1轮到他们,然后玩家2轮到他们,依此类推。如果没有满足参数,那就没问题了,它只会回到该回合的循环顶部,这样他们就可以再试一次。这是我的代码:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
   char TicTacToe [3][3] = {0}; // This initializes an array of "e's" for empty space.

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            TicTacToe [i][j] = 'e';
        }
    }

    for (int i = 0; i < 3; i++) // This outputs the game board so the user(s) can get a visual.
    {
        for (int j = 0; j < 3; j++)
        {
            cout << TicTacToe [i][j] << " ";
        }
        cout << "\n";
    }

    bool flag = true;
    bool turn1flag = true;
    bool turn2flag = true;
    int p1row, p1col, p2row, p2col = 0;

    cout << "\n";
    cout << "Welcome to Tic-Tac-Toe. The game board is shown above. " << endl;
    cout << "The rows are numbered one through three from top to bottom. " << endl;
    cout << "The columns are numbered one through three from left to right. " << endl;
    cout << "Player one's spaces are indicated by an 'o.'" << endl;
    cout << "Player two's spaces are indicated by an 'x.'" << endl;
    cout << "Empty spaces are indicated by an 'e.'" << endl << endl;

    while (flag == true) // This while loop starts the game
    {
        while (turn1flag == true)
        {
            cout << "It is player one's turn. Which row? " << endl;
            cin >> p1row;
            cout << "And which column? " << endl;
            cin >> p1col;

            if (TicTacToe[p1row-1][p1col-1] == 'e') // This checks if the selected spot is empty. If not, it's filled with the users symbol.
            {
                TicTacToe[p1row-1][p1col-1] = 'o';

                for (int i = 0; i < 3; i++) // This outputs the game board so the user(s) can see their progress.
                {
                    for (int j = 0; j < 3; j++)
                    {
                        cout << TicTacToe [i][j] << " ";
                    }
                    cout << "\n";
                }

                cout << "\n";

                turn1flag = false;
            }
            else // If a position is taken or out of bounds, we're returned to the top of the turn loop.
            {
                cout << "That position is already taken or out of bounds. Try again. " << endl;
            }
        }

        while (turn2flag == true)
        {
            cout << "It is player two's turn. Which row? " << endl;
            cin >> p2row;
            cout << "And which column? " << endl;
            cin >> p2col;

            if (TicTacToe[p2row-1][p2col-1] == 'e')
            {
                TicTacToe[p2row-1][p2col-1] = 'x';

                for (int i = 0; i < 3; i++) // This outputs the game board so the user(s) can see their progress.
                {
                    for (int j = 0; j < 3; j++)
                    {
                        cout << TicTacToe [i][j] << " ";
                    }
                    cout << "\n";
                }

                cout << "\n";

                turn2flag = false;
            }
            else
            {
                cout << "That position is already taken or out of bounds. Try again. " << endl;
            }
        }

        // All of the following are player one win conditions.

        if ((TicTacToe [0][0] = 'o') && (TicTacToe [0][1] = 'o') && (TicTacToe [0][2] = 'o'))
        {
            cout << "Congratulations player one, you've won!" << endl;
            flag = false;
        }
... // the other 15 winning conditions for either player 1 or 2.

3 个答案:

答案 0 :(得分:5)

if ((TicTacToe [0][0] = 'o') && (TicTacToe [0][1] = 'o') && (TicTacToe [0][2] = 'o'))

这些是作业。您需要==而不是=

这里发生的是'o'已分配,然后在布尔上下文中使用。每个可见字符都被视为true,因此条件被视为true && true && true,始终为true

答案 1 :(得分:0)

您永远不会将turn1flagturn2flag重置为true。正如它所写,看起来它会让每个玩家只有一个转弯,然后停止允许转弯。您需要更改这样的行:

turn1flag = false;
turn2flag = true;

turn2flag = false;
turn1flag = true;

或者只使用一个标志并让第一个while循环检查是否为真,第二个检查是否为假。然后第二个循环可以在移动后将其设置回true。

最重要的是,您需要在if语句中使用==,否则您将分配值,这将自动评估为true。

答案 2 :(得分:0)

您在if语句中使用赋值而不是比较。 =将返回赋值的值,只要该值不是0,它就会被计算为true。使用==代替比较两个值。