当前位置: 首页 > 知识库问答 >
问题:

我的While循环不会在条件下停止

罗学真
2023-03-14

我正在运行一个纸牌游戏,两个玩家轮流挑选纸牌,玩家向前移动许多空格。在这种情况下,一旦任何一个玩家达到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);
        }

    }

我的while循环在两个玩家都达到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;
}

当循环条件出了什么问题?

编辑:这是我运行的一个测试的一些输出。

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.

共有1个答案

后星河
2023-03-14

你的条件是“当player1小于25,或者player2小于25时,做循环中的事情”。但这绝对不是你想要的。简单更改:

while(player1 < 25 || player2 < 25){

while(player1 < 25 && player2 < 25){

我想我会做这项工作的。

这与德摩根定律有关,从逻辑上讲。

此外,请记住,如果要在任一玩家达到25岁时终止该程序,您必须在每次轮换后,即每次轮换呼叫后询问。

 类似资料:
  • 我写了一个代码,获取用户对开始日期的输入

  • 问题内容: 我已经编写了一个包含循环的“ Hangman”游戏,不断询问用户字母,直到他们解决单词或耗尽生命。奇怪的是,在满足条件时循环不会停止。 这是包含循环的游戏逻辑的实现: 可变结果是从方法中获取返回值。这是实现。 难道是我返回导致循环不断循环的值的方式吗?我这样声明了这些值: 问题答案: 逻辑上,根据德摩根定律, 是相同的 这永远是一个真实的表达。 条件可能应该是 当应用与上述相同的法律时

  • 我有一个项目,我的方法得到两个日期,它不断地向方法添加一天,直到两个日期相等,然后您可以通过查看一天添加了多少次来查看日期相距有多远。我的问题是,当满足日条件时,即使日、月和年必须都相同才能停止工作,我的while循环也会退出

  • 编辑:重新措辞 我有一个账户列表:帐户 当我总结这份清单时,我得到了账户数量和账户总价值的概述 名称摘要 我添加了马克,因为他现在将是一个帐户所有者,但他目前没有任何帐户。 第一步是查看每个人应该拥有的账户的平均数。有25个账户,5个所有者,所以每个人都应该有5个账户。总价值为15600英镑,共有5名所有者,因此每个人的账户价值应该在3120英镑左右。这是一个估计值,所以我对它稍微偏离一点没意见。

  • Q.while循环条件是作为一个整体进行计算,还是在决定是否进入循环之前一次计算一个条件?

  • 我正在尝试制作一个简单的程序,它使用扫描器进行输入,并有一个while循环,该循环继续输入,直到输入结束字符为止。我想让扫描器接受输入并将一个字符串添加到堆栈列表中。我试图弄清楚为什么这段代码在键入空格时不终止while循环。