C++ 中的 While 循环结束,但不会继续执行剩余的语句
While loop in C++ ends, but doesn't continue onto the remaining statements
这是我正在尝试制作的 TicTacToe 游戏的 while 循环,
while (result == 0)
{
game.player_move();
result = game.check_draw();
game.computer_move();
}
if (result == 1)
cout << "Tie.";
这些是 game.check_draw()
的条件。
int TicTacToe::check_draw()
{
if (move_count == 9)
return 1;
else
return 0;
}
此外,这段代码确实按照我希望的方式工作。 X 设置为 88,0 设置为 79。用户和计算机移动函数都将 move_count
值递增 1,如下所示:
void TicTacToe::player_move()
{
int position;
draw_table();
cout << endl << "Pick position[1-9]: ";
cin >> position;
while (pos[position - 1] == 79 || pos[position - 1] == 88)
{
system("CLS");
draw_table();
cout << endl << "Position taken.";
Sleep(500);
system("CLS");
draw_table();
cout << endl << "Pick position[1-9]: ";
cin >> position;
system("CLS");
}
position[pos - 1] = 88;
move_count++; // <-------------
system("CLS");
}
和
void TicTacToe::computer_move()
{
int position;
srand((unsigned int)time(0));
position = (rand() % 9) + 1;
while (pos[position - 1] == 79 || pos[position - 1] == 88)
position = (rand() % 9) + 1;
position[pos - 1] = 79;
move_count++; // <-------------
}
由于玩家先走,在第 9 轮后 move_count
设置为 9。然后程序继续执行下一条语句。
if (result == 1)
cout << "Tie.";
此代码完美运行,但 仅 如果删除 game.computer_move()
中的以下代码,
while (pos[position - 1] == 79 || pos[position - 1] == 88)
position = (rand() % 9) + 1;
删除此代码后,while 循环结束并继续到下一个 if 语句。但是,当我添加此代码时,它会结束 while 循环,但不会继续执行 if 语句并显示闪烁的光标。这段代码所做的一切,就是检查一个数组,看看 X a.k.a (88) 或 O a.k.a (79) 是否已经被放置在要放置 0 的位置。
我很困惑,因为这根本不会影响 move_count
,而且在第 9 个玩家移动后 move_count
设置为 9,这意味着循环应该正确终止并且甚至没有移动到 game.computer_move()
?
我的理解是,一旦不满足while条件,while循环中的其余代码将被忽略,循环应该结束。
我做了一个
cout << game.move_count;
在循环中,就在 game.check_draw()
之前检查它是否具有值 9,确实如此。
另外,补充一下。我的代码可读吗?它有什么明显的问题吗?
如果需要,我可以 post 完整的代码,感谢您提前回复。
My understanding is that once the while condition is not met, the rest of the code in the while loop is ignored and the loop should end.
这是不正确的。 while
循环只检查在循环的下一次迭代(结束时)之前是否满足条件。如果在循环中间不满足条件,它将一直持续到循环的当前迭代结束,然后跳出循环。如果您希望在满足条件后立即结束循环,您可以使用 break 语句来跳出循环。
来自cppreference:
This expression is evaluated before each iteration, and if it yields false, the loop is exited.
这是我正在尝试制作的 TicTacToe 游戏的 while 循环,
while (result == 0)
{
game.player_move();
result = game.check_draw();
game.computer_move();
}
if (result == 1)
cout << "Tie.";
这些是 game.check_draw()
的条件。
int TicTacToe::check_draw()
{
if (move_count == 9)
return 1;
else
return 0;
}
此外,这段代码确实按照我希望的方式工作。 X 设置为 88,0 设置为 79。用户和计算机移动函数都将 move_count
值递增 1,如下所示:
void TicTacToe::player_move()
{
int position;
draw_table();
cout << endl << "Pick position[1-9]: ";
cin >> position;
while (pos[position - 1] == 79 || pos[position - 1] == 88)
{
system("CLS");
draw_table();
cout << endl << "Position taken.";
Sleep(500);
system("CLS");
draw_table();
cout << endl << "Pick position[1-9]: ";
cin >> position;
system("CLS");
}
position[pos - 1] = 88;
move_count++; // <-------------
system("CLS");
}
和
void TicTacToe::computer_move()
{
int position;
srand((unsigned int)time(0));
position = (rand() % 9) + 1;
while (pos[position - 1] == 79 || pos[position - 1] == 88)
position = (rand() % 9) + 1;
position[pos - 1] = 79;
move_count++; // <-------------
}
由于玩家先走,在第 9 轮后 move_count
设置为 9。然后程序继续执行下一条语句。
if (result == 1)
cout << "Tie.";
此代码完美运行,但 仅 如果删除 game.computer_move()
中的以下代码,
while (pos[position - 1] == 79 || pos[position - 1] == 88)
position = (rand() % 9) + 1;
删除此代码后,while 循环结束并继续到下一个 if 语句。但是,当我添加此代码时,它会结束 while 循环,但不会继续执行 if 语句并显示闪烁的光标。这段代码所做的一切,就是检查一个数组,看看 X a.k.a (88) 或 O a.k.a (79) 是否已经被放置在要放置 0 的位置。
我很困惑,因为这根本不会影响 move_count
,而且在第 9 个玩家移动后 move_count
设置为 9,这意味着循环应该正确终止并且甚至没有移动到 game.computer_move()
?
我的理解是,一旦不满足while条件,while循环中的其余代码将被忽略,循环应该结束。
我做了一个
cout << game.move_count;
在循环中,就在 game.check_draw()
之前检查它是否具有值 9,确实如此。
另外,补充一下。我的代码可读吗?它有什么明显的问题吗?
如果需要,我可以 post 完整的代码,感谢您提前回复。
My understanding is that once the while condition is not met, the rest of the code in the while loop is ignored and the loop should end.
这是不正确的。 while
循环只检查在循环的下一次迭代(结束时)之前是否满足条件。如果在循环中间不满足条件,它将一直持续到循环的当前迭代结束,然后跳出循环。如果您希望在满足条件后立即结束循环,您可以使用 break 语句来跳出循环。
来自cppreference:
This expression is evaluated before each iteration, and if it yields false, the loop is exited.