变量未设置为正确值

时间:2014-11-13 17:39:50

标签: c++ debugging variables

我正在制作Tic Tac Toe游戏。在游戏结束时,如果两个人类玩家中的一个获胜,则应该说“玩家[1或2]赢了!”它通过检查获胜行/列/对角线的值('X'或'O')并将其与播放器对应的播放器(播放器1选择X或O作为其符号)来实现此目的。然而,它总是认为球员1赢了,无论谁赢了。

奇怪的是,我可以让程序cout既是playerwon又是BSTAT.getsquarestatus(“A1”)而且player_won将总是输出'X'而BSTAT.getsquarestatus(“A1”)将输出正确答案,无论是X或者O.

问题:为什么player_won总是被设置为X?它应该被设置为赢得的玩家的价值,但由于某种原因,即使获胜玩家的值为“O”,它也始终设置为X.

如果您需要更多信息,请告诉我们。谢谢你的帮助!

char player1_mark;
char player_won;

class Boardstatus
{

char A1;

public:

    char getsquarestatus( string square )
{
  // enumerator basically changes A1-C3 to a number 1-9
  // not an actual enumerator, just a function
    int squarenumber = enumerator( square );
    switch ( squarenumber )
    {
    case 1:
        return A1;
        break;
    }
}
private:

    //This function acts similar to an enumerator, but instead returns a number 1-9 based
    //on what string x is entered so that the switch cases in get and setsquarestatus will
    //function properly (they require int or char to work)
    int enumerator( string x )
    {
       if ( x == "A1" )
       {
           return 1;
       }
    };

    bool checkwin( Boardstatus& BSTAT)
    {
        //Row 'A' checkwin
        if (BSTAT.getsquarestatus("A1")!= ' ' && BSTAT.getsquarestatus("A1")==BSTAT.getsquarestatus("A2") && BSTAT.getsquarestatus("A2")==BSTAT.getsquarestatus("A3"))
        {
            //****This is where something is wrong****
            player_won = BSTAT.getsquarestatus("A1");
            return true;
        }
    }

int main()
{         
Boardstatus BSTAT;  
if ( player_won = player1_mark )
    {
        cout << "Player 1 has won!";
    }
else
    {
        cout << "Player 2 has won!";
    }
}

1 个答案:

答案 0 :(得分:1)

这:if ( player_won = player1_mark )正在设置player_won

的值

您需要==进行比较,而非分配。