0

我目前正在为大学做一个零零碎碎的课程。我已经完成了这项任务的基本内容,但是我在创建获胜条件以结束游戏时遇到了一些麻烦。以下是我到目前为止使用的所有代码:

#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 语句中,因为它似乎对程序的运行没有任何影响。

4

5 回答 5

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]

这又给出了trueor false。这将与

false == '0'

这将始终导致false.

于 2013-02-20T14:58:07.063 回答
3

您不能将比较串在一起:

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

将发生的所有事情都是Board[1] == Board[1][2]首先评估到truefalse and then that boolean value is compared toBoard[1][3]` 等等。

你想要的是:

Board[1][1] == 'O' && Board[1][2] == 'O' && Board[1][3] == 'O'
于 2013-02-20T14:58:47.323 回答
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].

于 2013-02-20T14:57:55.707 回答
1

你的if条件不对。
您应该将其替换为以下代码:

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

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

于 2013-02-20T14:58:33.500 回答
-1

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

于 2013-02-20T15:00:12.370 回答