我可以在 IF 语句中将比较链接在一起吗?

use*_*558 1 c++ syntax

我目前正在为大学制定一个零和交叉的计划。我已经完成了这项任务的基本内容,但是我在创建获胜条件来结束游戏时遇到了一些麻烦。以下是我迄今为止使用过的所有代码:

#include <iostream>
#include <string>

using namespace std;

class Player
{
private:
    char NorX;

public:

    char Choose(char InitialValue)
    {
        NorX = InitialValue;
        return InitialValue;
    }

    char GetNorX()
    {
        return NorX;
    }
};

int main()
{
    Player Player1;
    Player Player2;

    Player1.Choose('O');
    Player2.Choose('X'); 

    cout << "The board is being drawn please wait..." << endl;

    const int Rows = 4;
    const int Columns = 4;
    char Board[Rows][Columns] = { {' ', ' ', ' ', ' ' },
                                  {' ', '_', '_', '_' },
                                  {' ', '_', '_', '_' },
                                  {' ', '_', '_', '_' } };

    for (int i = 0; i < Rows; ++i)
    {
        for (int j = 0; j < Columns; ++j)
            cout << Board [i][j];
        cout << endl;
    }

    cout << endl << endl;

    int row;
    int column;

    do
    {
        do
        {
            cout << "Please enter the value of the row you would like to take ";
            cin >> row;
        }while (row != 0 && row != 1 && row != 2 && row != 3);


        do
        {
            cout << "Please enter the value of the column you would like to take ";
            cin >> column;
        }while (column != 0 && column != 1 && column != 2 && column != 3);


        Board [row][column] = Player1.GetNorX();

        for (int i = 0; i < Rows; ++i)
        {
            for (int j = 0; j < Columns; ++j)
                cout << Board [i][j];
            cout << endl;
        }

        /*int row;*/
        do
        {
            cout << "Please enter the value of the row you would like to take ";
            cin >> row;
        }while (row != 0 && row != 1 && row != 2 && row != 3);

        /*int column;*/
        do
        {
            cout << "Please enter the value of the column you would like to take ";
            cin >> column;
        }while (column != 0 && column != 1 && column != 2 && column != 3);

        Board [row][column] = Player2.GetNorX();

        for (int i = 0; i < Rows; ++i)
        {
            for (int j = 0; j < Columns; ++j)
                cout << Board [i][j];
            cout << endl;
        }

        if (Board[1][1] == Board[1][2] == Board[1][3] == 'O')
        {
            cout << endl << "Well done you win";
        }

    }while (column != 4 && row != 4);

    system("pause");
}
Run Code Online (Sandbox Code Playgroud)

问题出现在 if 语句中,因为它似乎对程序的运行没有任何影响。

Ola*_*che 5

C++ 中链接比较运算符的结果并不是人们所期望的。正确的方法是将它们用“逻辑与”连接起来&&

if (Board[1][1] == 'O' && Board[1][2] == 'O' && Board[1][3] == 'O')
Run Code Online (Sandbox Code Playgroud)

对于给定的例子

if (Board[1][1] == Board[1][2] == Board[1][3] == 'O')
Run Code Online (Sandbox Code Playgroud)

您必须考虑运算符优先级,即相等运算符从左到右==。这意味着,该示例与(注意附加括号)相同

if ((Board[1][1] == Board[1][2]) == Board[1][3]) == 'O')
Run Code Online (Sandbox Code Playgroud)

并按如下方式工作:

Board[1][1] == Board[1][2]
Run Code Online (Sandbox Code Playgroud)

给出truefalse。这将与下一部分进行比较

true == Board[1][3]
Run Code Online (Sandbox Code Playgroud)

给出false, 因为trueorfalse永远不等于字符。这将与字符零进行比较

false == '0'
Run Code Online (Sandbox Code Playgroud)

这又会导致false.