gpt4 book ai didi

c++ - 变量未设置为正确的值

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:12 25 4
gpt4 key购买 nike

我正在制作井字游戏。在游戏结束时,如果两个人类玩家中的一个获胜,则应该说“玩家 [1 或 2] 赢了!”它通过检查获胜行/列/对角线的值(“X”或“O”)并将其对应于它是什么玩家(玩家 1 选择 X 或 O 作为他们的符号)来做到这一点。然而,它总是认为玩家 1 赢了,而不管实际上谁赢了。

奇怪的是,我可以让程序 cout playerwon 和 BSTAT.getsquarestatus("A1") 并且 player_won 将始终输出 'X' 而 BSTAT.getsquarestatus("A1") 将输出正确答案,无论它是 X或 O.

问题:为什么 player_won 总是设置为 X?它应该设置为获胜玩家的值,但由于某种原因它总是设置为 X,即使获胜玩家的值为“O”也是如此。

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

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!";
}
}

最佳答案

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

您需要 == 进行比较,而不是赋值。

关于c++ - 变量未设置为正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26915031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com