gpt4 book ai didi

c++ - 无法结束 while 循环 (C++)

转载 作者:搜寻专家 更新时间:2023-10-31 01:35:06 24 4
gpt4 key购买 nike

我似乎无法结束这个 while 循环,我已经尝试了所有我知道的方法,但它仍然无法正常工作。即使我已经将其更改为“false”,变量“contGame”也会一直更改为“true”。你们能帮帮我吗!请注意,这在一个类中。这是我的代码:

while (contGame == true)
{
if (turn == 1)
{
cout << Board._player1Name <<"'s turn!!\n";
X = getMoveX();
Y = getMoveY();

Board.Board[X][Y] = player1Sign;
Board.printBoard();
turn = turn + 1;
Board.checkGame(contGame, player1Sign, player2Sign);
if (checkGame(contGame, player1Sign, player2Sign) == false)
{
bool contGame = false;
cout << contGame << endl;
}
}
cout << contGame << endl;
if(turn == 2)
{
cout << Board._player2Name <<"'s turn!!\n";
X = getMoveX();
Y = getMoveY();

Board.Board[X][Y] = player2Sign;
Board.printBoard();
turn = turn - 1;
Board.checkGame(contGame, player1Sign, player2Sign);
if (checkGame(contGame, player1Sign, player2Sign) == false)
{
int contGame = 1;
cout << contGame << endl;
}
}
}

这是 checkGame:

bool contGameA = true;
for (int k = 0; k < 3; k++)
{

if (Board[k][0] == Board[k][1] && Board[k][1] == Board[k][2] && Board[k][2] == player1Sign)
{
cout << "\\\\" << _player1Name <<" wins!////\n";
contGameA = false;
}
else if (Board[k][0] == Board[k][1] && Board[k][1] == Board[k][2] && Board[k][2] == player2Sign)
{
cout << "\\\\" << _player2Name << " wins!////\n";
contGameA = false;
}
}

for (int r = 0; r < 3; r++)
{
if (Board[0][r] == Board[1][r] && Board[1][r] == Board[2][r] && Board[2][r] == player1Sign)
{
cout << "\\\\" << _player1Name << " wins!////\n";
contGameA = false;
}
else if (Board[0][r] == Board[1][r] && Board[1][r] == Board[2][r] && Board[2][r] == player2Sign)
{
cout << "\\\\" << _player2Name << " wins!////\n";
contGameA = false;
}
}

if (Board[0][0] == Board[1][1] && Board[1][1] == Board[2][2] && Board[2][2] == player1Sign)
{
cout << "\\\\" << _player1Name << " wins!////\n";
contGameA = false;
} else if (Board[0][0] == Board[1][1] && Board[1][1] == Board[2][2] && Board[2][2] == player2Sign)
{
cout << "\\\\" << _player2Name << " wins!////\n";
contGameA = false;
}

if (Board[0][2] == Board[1][1] && Board[1][1] == Board[2][0] && Board[2][0] == player1Sign)
{
cout << "\\\\" << _player1Name << " wins!////\n";
contGameA = false;
} else if (Board[0][2] == Board[1][1] && Board[1][1] == Board[2][0] && Board[2][0] == player2Sign)
{
cout << "\\\\" << _player2Name << " wins!////\n";
contGameA = false;
}
return contGameA;

最佳答案

您实际上是在 if 语句的嵌套范围内创建了 contGame 变量,这隐藏了在外部范围内声明的 contGame 变量。因此,您永远不会更改外部 contGame 变量

例如

if (checkGame(contGame, player1Sign, player2Sign) == false)
{
// bool contGame = false; should be:
contGame = false;
cout << contGame << endl;
}

if (checkGame(contGame, player1Sign, player2Sign) == false)
{
// int contGame = 1; should be
contGame = true;
cout << contGame << endl;
}

关于c++ - 无法结束 while 循环 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37948037/

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