C++ 我的While循环赢了';在这种情况下不能停止
我正在运行一个纸牌游戏,两个玩家轮流挑选纸牌,玩家向前移动许多空格。在这种情况下,一旦任何一个玩家达到25或更大的数字,他们击中了家,游戏应该停止C++ 我的While循环赢了';在这种情况下不能停止,c++,while-loop,C++,While Loop,我正在运行一个纸牌游戏,两个玩家轮流挑选纸牌,玩家向前移动许多空格。在这种情况下,一旦任何一个玩家达到25或更大的数字,他们击中了家,游戏应该停止 //This is the function that plays the entire game void play(int size, int &player1, int &player2, int cardPile[], int board[]){ displayRules(); whi
//This is the function that plays the entire game
void play(int size, int &player1, int &player2, int cardPile[], int board[]){
displayRules();
while(player1 < 25 || player2 < 25){
cout << "\nPlayer 1's turn!" << endl;
takeTurn(size, player1, cardPile, board, player2);
showState(player1, player2);
cout << "\nPlayer 2's turn!" << endl;
takeTurn(size, player2, cardPile, board, player1);
showState(player1, player2);
}
}
你的条件是“当player1小于25,或者player2小于25时,做循环中的事情”。但这绝对不是你想要的。简单更改:
while(player1 < 25 || player2 < 25){
while(播放器1<25 | |播放器2<25){
到
while(player1<25&&player2<25){
我想我会做这项工作的
这与逻辑有关
另外,请记住,如果程序在任一玩家达到25岁时终止,您必须在每回合后询问,即在每次轮换呼叫后询问。尝试将代码缩减到显示行为的最小位,您将得到更好的回答。谢谢。从逻辑上讲,&&操作员会这样做,以便只有一个玩家参与er 1和玩家2必须在家里才能结束游戏。而| |操作员则会让玩家1和玩家2都在家里才能结束游戏。我犯了一个大错误。我在第二次轮流呼叫开始前添加了一个if语句,因此如果玩家1大于或等于25,循环将中断或终止否则它将继续作用于玩家2。
Welcome to GoHome! The main objective of this game is to reach Home first.
The basic rules of the game are as follows:
-To begin the player with the shortest name goes first.
-Each player picks a card that has a number on it and the player must moves forward that many number of spaces.
-If a card says 'Lose A Turn', the player does nothing and theturn moves to the next player.
-If a card says 'Switch Places', that player is allowed to switch places with any player on the board.
-If a player lands on an obstacle, that player must move back that many number of spaces.
-If a player lands another obstacle while moving backwards, then it does not have to move backwards again.
0
0
Player 1's turn!
You drew a 3! Move forward 3 spaces on the board!
Player 1 is on spot 3 of the board.
Player 2 is at Start!
Player 2's turn!
You drew a 4! Move forward 4 spaces on the board!
You ran into an obstacle! Move back 1 space!
Player 1 is on spot 3 of the board.
Player 2 is on spot 3 of the board.
3
3
Player 1's turn!
You drew a 1! Move forward 1 space on the board!
You ran into an obstacle! Move back 1 space!
Player 1 is on spot 3 of the board.
Player 2 is on spot 3 of the board.
Player 2's turn!
You drew a 3! Move forward 3 spaces on the board!
Player 1 is on spot 3 of the board.
Player 2 is on spot 6 of the board.
3
6
Player 1's turn!
You drew a 4! Move forward 4 spaces on the board!
Player 1 is on spot 7 of the board.
Player 2 is on spot 6 of the board.
Player 2's turn!
You drew a 1! Move forward 1 space on the board!
Player 1 is on spot 7 of the board.
Player 2 is on spot 7 of the board.
7
7
Player 1's turn!
You drew a 3! Move forward 3 spaces on the board!
Player 1 is on spot 10 of the board.
Player 2 is on spot 7 of the board.
Player 2's turn!
You drew a 4! Move forward 4 spaces on the board!
Player 1 is on spot 10 of the board.
Player 2 is on spot 11 of the board.
10
11
Player 1's turn!
You drew a 1! Move forward 1 space on the board!
Player 1 is on spot 11 of the board.
Player 2 is on spot 11 of the board.
Player 2's turn!
You drew a 3! Move forward 3 spaces on the board!
Player 1 is on spot 11 of the board.
Player 2 is on spot 14 of the board.
11
14
Player 1's turn!
You drew a 4! Move forward 4 spaces on the board!
Player 1 is on spot 15 of the board.
Player 2 is on spot 14 of the board.
Player 2's turn!
You drew a 1! Move forward 1 space on the board!
Player 1 is on spot 15 of the board.
Player 2 is on spot 15 of the board.
15
15
Player 1's turn!
You drew a 3! Move forward 3 spaces on the board!
Player 1 is on spot 18 of the board.
Player 2 is on spot 15 of the board.
Player 2's turn!
You drew a 4! Move forward 4 spaces on the board!
Player 1 is on spot 18 of the board.
Player 2 is on spot 19 of the board.
18
19
Player 1's turn!
You drew a 1! Move forward 1 space on the board!
Player 1 is on spot 19 of the board.
Player 2 is on spot 19 of the board.
Player 2's turn!
You drew a 3! Move forward 3 spaces on the board!
Player 1 is on spot 19 of the board.
Player 2 is on spot 22 of the board.
19
22
Player 1's turn!
You drew a 4! Move forward 4 spaces on the board!
Player 1 is on spot 23 of the board.
Player 2 is on spot 22 of the board.
Player 2's turn!
You drew a 1! Move forward 1 space on the board!
Player 1 is on spot 23 of the board.
Player 2 is on spot 23 of the board.
23
23
Player 1's turn!
You drew a 3! Move forward 3 spaces on the board!
Player 1 is on spot 26 of the board.
Player 2 is on spot 23 of the board.
Player 2's turn!
You drew a 4! Move forward 4 spaces on the board!
Player 1 is on spot 26 of the board.
Player 2 is on spot 27 of the board.
while(player1 < 25 || player2 < 25){
while(player1 < 25 && player2 < 25){