我似乎無法結束此while循環,我已經嘗試了我知道的每一種方式,但它仍然無法正常工作。即使我已將其更改爲「false」,變量「contGame」也會更改爲「true」。你們能幫我嗎?請注意,這裏面的一個類。這裏是我的代碼:不能結束while循環(C++)
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;
雖然我沒有回答這個問題,因爲我碰巧發現錯誤在您發佈的代碼牆,請注意您的問題還遠遠沒有提供[MCVE]因爲「最小」的部分。在發佈之前,你應該儘可能地縮短代碼(當然,保持它重現錯誤)。 – Angew