【发布时间】:2018-04-08 05:19:26
【问题描述】:
我在创建模拟井字游戏的程序时遇到了这个 C++ 代码的问题。游戏运行“正常”、显示获胜者、验证输入等,但如果玩家“X”获胜,则仍然允许玩家“O”在宣布获胜者之前再采取行动。
do
{
int turn = 0;
if (turn % 2 == 0)
{
cout << "Player X, Row and Column: ";
cin >> row >> column;
while (array[(row-1)][(column-1)] != '*')
{
cout << "Invalid move try again \n";
cout << "Player X, Row and Column: ";
cin >> row >> column;
}
array[row-1][column-1] = 'X';
showArray(array);
results = checkWin(array);
}
if (turn % 2 != 0)
{
cout << "Player O, Row and Column: ";
cin >> row >> column;
while (array[row-1][column-1] != '*')
{
cout << "Invalid move try again \n";
cout << "Player O, Row and Column: ";
cin >> row >> column;
}
array[row-1][column-1] = 'O';
showArray(array);
results = checkWin(array);
}
turn++;
}while (results == 0);
我正在使用在两个玩家之间交替的 do-while 循环。当我放置递增的 'turn++;'两个 if 块之外的语句,程序只允许玩家“X”移动。当我放置'turn++;'播放器'X' if 块中的语句,它交替出现,但我遇到了上述问题。如果您有任何建议,请给他们。谢谢。
【问题讨论】:
-
抽奖条件在哪里?
-
阅读ericlippert.com/2014/03/05/how-to-debug-small-programs,了解如何调试程序的一些技巧。
标签: c++ increment do-while tic-tac-toe