我的 while 循环不会在条件下停止
My While Loop Won't Stop At Condition
我正在运行一个纸牌游戏,两个玩家都轮流选择卡,并且玩家向前迈进了许多空间。在这种情况下,一旦玩家达到25或更高的数字,他们就击中了,游戏应该停止。
//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);
}
}
我的wire循环在两个球员都达到25次后继续循环循环,尽管我设定了循环的条件,而玩家1的值小于25,或者播放器2的值小于25。
。#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
void play(int size, int &player1, int &player2, int cardPile[], int board[]);
void displayRules();
int takeTurn(int size, int &player, int cardPile[], int board[], int &opposingPlayer);
int shuffleDeck(int size, int cardPile[]);
int switchPlaces(int &player, int &opposingPlayer);
int obstacles(int &player, int board[]);
void showState(int &player1, int &player2);
void youWin(int &player1, int &player2);
int main()
{
const int size = 10;
int board[] = {0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0};
int cardPile[size] = {1, 1, 2, 2, 3, 3, 4, 4, 0, 5};
int player1 = 0;
int player2 = 0;
play(size, player1, player2, cardPile, board);
return 0;
}
//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);
}
}
//This function does a single turn for each player
int takeTurn(int size, int &player, int cardPile[], int board[],int &opposingPlayer){
shuffleDeck(size, cardPile);
int i = 0;
if(cardPile[i] == 0)
cout << "You drew a Lose a turn card! You lose a turn!" << endl;
else if(cardPile[i] == 5)
cout << "You drew a Switch Places card! You must switch places with the other player!" << endl,
switchPlaces(player, opposingPlayer);
else
cout << "You drew a " << cardPile[i] << "!";
switch(cardPile[i]){
case 1:
cout << " Move forward " << cardPile[i] << " space on the board!" << endl;
player += cardPile[i];
obstacles(player, board);
break;
case 2:
cout << " Move forward " << cardPile[i] << " spaces on the board!" << endl;
player += cardPile[i];
obstacles(player, board);
break;
case 3:
cout << " Move forward " << cardPile[i] << " spaces on the board!" << endl;
player += cardPile[i];
obstacles(player, board);
break;
case 4:
cout << " Move forward " << cardPile[i] << " spaces on the board!" << endl;
player += cardPile[i];
obstacles(player, board);
break;
}
}
//This function shuffles the deck of cards
int shuffleDeck(int size, int cardPile[]){
srand(time(0));
for(int i = 0; i < size; i++){
int index = rand() % size;
int temp = cardPile[i];
cardPile[i] = cardPile[index];
cardPile[index] = temp;
}
}
//This is the function that tells a player when they have ran into an
//obstacle and moves them back the appropriate number of spaces
int obstacles(int &player, int board[]){
if(player == 1)
player -= 1,
cout << "You ran into an obstacle! Move back 1 space!" << endl;
else if(player == 4)
player -= 1,
cout << "You ran into an obstacle! Move back 1 space!" << endl;
else if(player == 8)
player -= 2,
cout << "You ran into an obstacle! Move back 2 spaces!" << endl;
else if(player == 12)
player -= 3,
cout << "You ran into an obstacle! Move back 3 spaces!" << endl;
else if(player == 16)
player -= 2,
cout << "You ran into an obstacle! Move back 2 spaces!" << endl;
else if(player == 20)
player -= 1,
cout << "You ran into an obstacle! Move back 1 space!" << endl;
}
while循环条件我在哪里出错?
编辑:这是我运行的测试之一的一些输出。
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.
好吧,您的病情说"虽然player1小于25,否则player2小于25,但要做循环中的事情"。但这绝对不是您想要的。简单地更改:
while(player1 < 25 || player2 < 25){
to
while(player1 < 25 && player2 < 25){
我认为将完成这项工作。
这与De Morgan的定律有关。
另外,请记住,要使该程序在任何一个播放器达到25时终止,您必须在每个转弯之后询问,即每个taketurn呼叫后。
相关文章:
- 为什么简单的算术减法在"if"条件下不起作用?
- 在什么条件下使用 std::memcpy 在对象之间复制是安全的?
- 改进简单条件下C++样式
- 唤醒多个线程以在每个条件下工作一次
- "is defined"宏检查及其调用必须处于不同的条件下吗?
- 在if条件下,右或左改变值的相等性检查是否有任何区别
- C++代码在 for 循环的条件下给出运行时错误,而如果它被具有相同意义的代码替换,则编译正确
- 在 constexpr-if 条件下比较 constexpr 函数参数会导致错误
- 轮班操作在条件下使用的说明
- 在不同条件下无法访问我的向量变量
- 在某些条件下最大化 GCD
- 如何更改此空隙函数,以便在特殊条件下返回一个生成的字符串
- 在if-Statement条件下与分配有关的错误
- 如何正确使用“ sizeof”操作员在循环的条件下正确使用
- 我的 while 循环不会在条件下停止
- C++ while 循环不会在错误条件下退出
- 我需要在两个条件下实现while循环
- ||操作员在while条件下未按预期工作
- C++在两个不同的条件下执行/while循环
- c++在while条件下声明和测试变量