为什么以下 while 循环使用 AND 而不是 OR
Why does the following while loop work with an AND, as opposed to an OR
所以我正在制作游戏,我只是设法通过将我的 OR 更改为 AND 来解决我遇到的问题,我不确定我是否理解为什么它会起作用而不是逻辑 ||我以前去过。
//while user doesn't press quit, hasn't crashed into a bomb or hasn't saved all bears the game continues
while (((!wantsToQuit(key)) && (hasLost == false)) && (bearsSaved !=3))
{
if (isArrowKey(key))
{
updateGameData(grid, bears, bombs, detonator, exit, key, message, bearsSaved, bombsActive, moves, hasLost); //move bear in that direction
updateGrid(grid, maze, bears, bombs, detonator, exit); //update grid information
}
else if (toupper(key) == CHEAT) //pressing c enables cheat mode; disables bombs and sets the users moves to 500
{
cheatMode(message, moves, cheatActive, bombsActive);
}
else
message = "INVALID KEY!"; //set 'Invalid key' message
paintGame(grid, message, bearsSaved, moves); //display game info, modified grid & messages
key = getKeyPress(); //display menu & read in next option
}
endProgram(); //display final message
return(0);
基本思路是,用户必须引导 3 只熊穿过迷宫并避开炸弹,从而拯救它们。
- 如果他们走进炸弹,hasLost 设置为 true,因此 endProgram 为 运行。
- 如果他们按 Q,while 循环不再为真,endProgram 为 运行。
- 但是,如果所有的熊都被救了,那么游戏也结束了。
我不明白的是为什么这个 while 循环在 while:
玩家不想退出并且玩家没有输掉并且玩家没有拯救所有 3 只熊。
相对于:
玩家不想退出并且玩家没有输掉或者玩家没有拯救所有 3 只熊。
对我来说,后者在逻辑上更有意义,因为人们会期望游戏继续 运行 而他们不想退出并且没有输,或者如果他们没有保存所有熊。
感谢您的澄清
The player does not want to quit AND the player has not lost OR the player has not saved all 3 bears
在这种情况下,您的 while 循环条件如下所示:
while (((!wantsToQuit(key)) && (hasLost == false)) || (bearsSaved !=3))
让我们假设玩家 not 输了,他 not 想退出游戏。现在,如果您的玩家救了 3 只熊,那么 bearsSaved !=3
条件将被伪造。但是,由于你使用了||
,即使第二个陈述被证伪,游戏也会继续,因为第一个陈述仍然是true。 这违背了你的逻辑,因为你想要:
However, if all bears are saved then the game also ends.
使用 ||
语句,您必须伪造两个条件(我将前两个带括号的条件作为一个主要条件)才能终止循环。
这就是为什么您现在使用的逻辑有意义;一旦用户获胜,AND 条件就会被证伪,一旦一个 AND 条件被证伪,循环就会终止。
所以我正在制作游戏,我只是设法通过将我的 OR 更改为 AND 来解决我遇到的问题,我不确定我是否理解为什么它会起作用而不是逻辑 ||我以前去过。
//while user doesn't press quit, hasn't crashed into a bomb or hasn't saved all bears the game continues
while (((!wantsToQuit(key)) && (hasLost == false)) && (bearsSaved !=3))
{
if (isArrowKey(key))
{
updateGameData(grid, bears, bombs, detonator, exit, key, message, bearsSaved, bombsActive, moves, hasLost); //move bear in that direction
updateGrid(grid, maze, bears, bombs, detonator, exit); //update grid information
}
else if (toupper(key) == CHEAT) //pressing c enables cheat mode; disables bombs and sets the users moves to 500
{
cheatMode(message, moves, cheatActive, bombsActive);
}
else
message = "INVALID KEY!"; //set 'Invalid key' message
paintGame(grid, message, bearsSaved, moves); //display game info, modified grid & messages
key = getKeyPress(); //display menu & read in next option
}
endProgram(); //display final message
return(0);
基本思路是,用户必须引导 3 只熊穿过迷宫并避开炸弹,从而拯救它们。
- 如果他们走进炸弹,hasLost 设置为 true,因此 endProgram 为 运行。
- 如果他们按 Q,while 循环不再为真,endProgram 为 运行。
- 但是,如果所有的熊都被救了,那么游戏也结束了。
我不明白的是为什么这个 while 循环在 while:
玩家不想退出并且玩家没有输掉并且玩家没有拯救所有 3 只熊。
相对于:
玩家不想退出并且玩家没有输掉或者玩家没有拯救所有 3 只熊。
对我来说,后者在逻辑上更有意义,因为人们会期望游戏继续 运行 而他们不想退出并且没有输,或者如果他们没有保存所有熊。
感谢您的澄清
The player does not want to quit AND the player has not lost OR the player has not saved all 3 bears
在这种情况下,您的 while 循环条件如下所示:
while (((!wantsToQuit(key)) && (hasLost == false)) || (bearsSaved !=3))
让我们假设玩家 not 输了,他 not 想退出游戏。现在,如果您的玩家救了 3 只熊,那么 bearsSaved !=3
条件将被伪造。但是,由于你使用了||
,即使第二个陈述被证伪,游戏也会继续,因为第一个陈述仍然是true。 这违背了你的逻辑,因为你想要:
However, if all bears are saved then the game also ends.
使用 ||
语句,您必须伪造两个条件(我将前两个带括号的条件作为一个主要条件)才能终止循环。
这就是为什么您现在使用的逻辑有意义;一旦用户获胜,AND 条件就会被证伪,一旦一个 AND 条件被证伪,循环就会终止。