gpt4 book ai didi

c++ - 井字游戏 - 如何在无效输入后不打印结果

转载 作者:行者123 更新时间:2023-11-28 02:52:42 26 4
gpt4 key购买 nike

现在,当我尝试确定有效输入以再次玩游戏时,它会在显示无效输入后立即再次显示游戏结果,然后要求用户再次输入“y”或“n”。我上传了一张照片给你看。我一辈子都弄不明白。

这是图片: http://imgur.com/SRsMo4P

// GAME OVER
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
while (bGameOver)
{
// Display game board - PRINT
for (int i = 0; i<ROW; ++i)
{
cout << " " << board[i][0] << " | " << board[i][1] << " | " << board[i][2] << endl;
if (i==0 || i==1)
{
cout << " - + - + -" << endl;
}
}

// Player wins - OUTPUT
if(bWinGame)
{
cout << endl;
cout << " Player "<< playerTurn << " wins! HOORAH! " << endl << endl;
cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
}

// Play again - OUTPUT & USER INPUT
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// VARIABLES
char again; // Play again = "Y" or "N"
cout << endl;
cout << " Want to play again? ( Y / N )" << endl << endl;
cout << " "; cin >> again; cout << endl;

// Play again - if YES
if (again == 'y' || again == 'Y')
{
bGameOver=false; // Reset game state
bWinGame=true; // Reset assumption

board[0][0] = '*'; // Rest game board - Array
board[0][1] = '*';
board[0][2] = '*';
board[1][0] = '*';
board[1][1] = '*';
board[1][2] = '*';
board[2][0] = '*';
board[2][1] = '*';
board[2][2] = '*';
}

// Play again - if NO
else if (again == 'n' || again == 'N')
{
cout << " Awe oh well, thanks for playing. " << endl << endl;
cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
break;
}

else
{
cout << " INVALID ENTRY: Please input \"Y\" or \"N\" " << endl << endl;
}
}

// Game play continue
if (!bGameOver)
{
// Switch player turn
if (playerTurn == 1)
{
playerTurn = 2;
}

else
{
playerTurn = 1;
}

}

}



cout << " "; return 0;
}

最佳答案

你在这个else语句中没有break:

else
{
cout << " INVALID ENTRY: Please input \"Y\" or \"N\" " << endl << endl;
}

代码将继续运行而不会中断,从而导致奇怪的行为。试试这个:

else
{
cout << " INVALID ENTRY: Please input \"Y\" or \"N\" " << endl << endl;
break;
}

关于c++ - 井字游戏 - 如何在无效输入后不打印结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22679175/

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