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

时间:2013-02-20 14:54:03

标签: 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");
}

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

5 个答案:

答案 0 :(得分:4)

链接比较运算符的结果并不是人们所期望的。正确的方法是将它们与&&

连接起来
if (Board[1][1] == 'O' && Board[1][2] == 'O' && Board[1][3] == 'O')

另一个如下工作

Board[1][1] == Board[1][2]

提供truefalse。这将与

进行比较
true == Board[1][3]

再次提供truefalse。这将与

进行比较
false == '0'

总是会产生false

答案 1 :(得分:2)

你无法将这种比较串联起来:

Board[1][1] == Board[1][2] == Board[1][3] == 'O'

所有这一切都将Board[1] == Board[1][2]首先评估为truefalse and then that boolean value is compared to Board [1] [3]`等等。

你想要的是:

Board[1][1] == 'O' && Board[1][2] == 'O' && Board[1][3] == 'O'

答案 2 :(得分:1)

你应该使用

if (Board[1][1] == Board[1][2] && 
    Board[1][1] == Board[1][3] && 
    Board[1][1] == 'O')

if (Board[1][1] == 'O' && 
    Board[1][2] == 'O' && 
    Board[1][3] == 'O')

例如,在当前形式中,您的陈述会将Board[1][2]Board[1][3]=='O'比较的结果进行比较,而不是Board[1][3]

答案 3 :(得分:1)

您的if条件有误。
您应该使用以下代码替换它:

if ((Board[1][1] == Board[1][2]) && 
    (Board[1][2] == Board[1][3]) && 
    (Board[1][3] == 'O'))

当您检查水平获胜条件时,水平线上的所有三个区块的值必须 0

答案 4 :(得分:-1)

您应该在调试器(如gdb)中运行它。谷歌“gdb cheatsheet”开始使用。您将确切地看到正在执行的代码行,特别是可以验证正在评估“if”。