岩石纸剪刀蜥蜴Spock - 当圈问题时

时间:2016-11-29 19:46:35

标签: java loops while-loop

我正在为摇滚,纸张,剪刀,蜥蜴,Spock制作一个程序,计算机可以同时播放两个玩家。我试图制作游戏,以便当player1或player2连续四次获胜时停止游戏。

我尝试使用while循环,但它似乎没有评估while循环的条件,因为当我运行它时游戏永远不会结束。为了看看发生了什么,我打算在每一轮之后打印consecutiveWins的数字,并且实际上保留了胜利的记录,

问题是当它达到4或-4时它才会终止。

有人能看出为什么会这样吗?

输出:

Player 1 chooses Spock.

Player 2 chooses scissors.

Spock melts scissors.

Player 1 wins.

-5706

Player 1 chooses lizard.

Player 2 chooses rock.

Rock crushes lizard.

Player 2 wins.

-5705

Player 1 chooses paper.


Player 2 chooses rock.

Paper covers rock.

Player 1 wins.

-5706

这是我的代码:

public class RockPaperScissorsLizardSpock {


private int lastWinner = 0;
public static final int ROCK = 1;
public static final int PAPER = 2;
public static final int SCISSORS = 3;
public static final int LIZARD = 4;
public static final int SPOCK = 5;
public static final int PLAYER1 = 1;
public static final int PLAYER2 = 2;

public void play(int player1, int player2) {


    int consecutiveWins =0;
    while ((consecutiveWins < 4) || (consecutiveWins > -4)) {
        String play1;
        String play2;

        player1 = random();
        player2 = random();
        play1 = convert(player1);
        play2 = convert(player2);

        switch (play1) {
        case "rock": // Player 1 chooses Rock
            System.out.println("Player 1 chooses rock.");
            if ("rock".equalsIgnoreCase(play2)) // Rock
            {
                System.out.println("Player 2 chooses rock.
\nTie.\n"+consecutiveWins);
            } else if ("paper".equalsIgnoreCase(play2)) // Paper
            {
                System.out.println("Player 2 chooses paper.
\nPaper covers rock.\nPlayer 2 wins.\n"+consecutiveWins);
                consecutiveWins=(consecutiveWins-1);
            } else if ("scissors".equalsIgnoreCase(play2)) // Scissors
            {
                System.out.println("Player 2 chooses scissors.
\nRock smashes scissors.\nPlayer 1 wins.\n"+consecutiveWins);
                consecutiveWins++;
            } else if ("lizard".equalsIgnoreCase(play2)) // Lizard
            {
                System.out.println("Player 2 chooses lizard.
\nRock crushes lizard.\nPlayer 1 wins.\n"+consecutiveWins);
                consecutiveWins++;
            } else if ("spock".equalsIgnoreCase(play2)) // Spock
            {
                System.out.println("Player 2 chooses Spock.
\nSpock vaporizes rock.\nPlayer 2 wins.\n"+consecutiveWins);
                consecutiveWins=(consecutiveWins-1);
            }
            break;

        case "paper": // Player 1 chooses paper
            System.out.println("Player 1 chooses paper.");
            if ("rock".equalsIgnoreCase(play2)) // Rock
            {
                System.out.println("Player 2 chooses rock.
\nPaper covers rock.\nPlayer 1 wins.\n"+consecutiveWins);
                consecutiveWins++;
            } else if ("paper".equalsIgnoreCase(play2)) // Paper
            {
                System.out.println("Player 2 chooses paper.
\nTie.\n"+consecutiveWins);
            } else if ("scissors".equalsIgnoreCase(play2)) // Scissors
            {
                System.out.println("Player 2 chooses scissors.
\nScissors cut paper.\nPlayer 2 wins.\n" +consecutiveWins);
                consecutiveWins=(consecutiveWins-1);
            } else if ("lizard".equalsIgnoreCase(play2)) // Lizard
            {
                System.out.println("Player 2 chooses lizard.
\nLizard eats paper.\nPlayer 2 wins.\n"+consecutiveWins);
                consecutiveWins=(consecutiveWins-1);
            } else if ("spock".equalsIgnoreCase(play2)) // Spock
            {
                System.out.println("Player 2 chooses Spock.
\nPaper disproves Spock.\nPlayer 1 wins.\n"+consecutiveWins);
                consecutiveWins++;
            }
            break;

        case "scissors": // User chooses scissors
            System.out.println("Player 1 chooses scissors.");
            if ("rock".equalsIgnoreCase(play2)) // Rock
            {
                System.out.println("Player 2 chooses rock.
\nRock smashes scissors.\nPlayer 2 wins.\n"+consecutiveWins);
                consecutiveWins=(consecutiveWins-1);
            } else if ("paper".equalsIgnoreCase(play2)) // Paper
            {
                System.out.println("Player 2 chooses paper.
\nPaper covers rock.\nPlayer 2 wins.\n"+consecutiveWins);
                consecutiveWins=(consecutiveWins-1);
            } else if ("scissors".equalsIgnoreCase(play2)) // Scissors
            {
                System.out.println("Player 2 chooses scissors.
\nTie.\n"+consecutiveWins);
            } else if ("lizard".equalsIgnoreCase(play2)) // Lizard
            {
                System.out.println("Player 2 chooses lizard.
\nRock smashes lizard.\nPlayer 1 wins.\n"+consecutiveWins);
                consecutiveWins++;
            } else if ("spock".equalsIgnoreCase(play2)) // Spock
            {
                System.out.println("Player 2 chooses Spock.
\nSpock melts scissors.\nPlayer 2 wins.\n"+consecutiveWins);
                consecutiveWins=(consecutiveWins-1);
            }
            break;

        case "lizard": // User chooses lizard
            System.out.println("Player 1 chooses lizard.");
            if ("rock".equalsIgnoreCase(play2)) // Rock
            {
                System.out.println("Player 2 chooses rock.
\nRock crushes lizard.\nPlayer 2 wins.\n"+consecutiveWins);
                consecutiveWins=(consecutiveWins-1);
            } else if ("paper".equalsIgnoreCase(play2)) // Paper
            {
                System.out.println("Player 2 chooses paper.
\nPaper covers rock.\nPlayer 2 wins.\n"+consecutiveWins);
                consecutiveWins=(consecutiveWins-1);
            } else if ("scissors".equalsIgnoreCase(play2)) // Scissors
            {
                System.out.println("Player 2 chooses scissors.
\nRock smashes scissors.\nPlayer 1 wins.\n"+consecutiveWins);
                consecutiveWins++;
            } else if ("lizard".equalsIgnoreCase(play2)) // Lizard
            {
                System.out.println("Player 2 chooses lizard.
\nTie.\n"+consecutiveWins);
            } else if ("spock".equalsIgnoreCase(play2)) // Spock
            {
                System.out.println("Player 2 chooses Spock.
\nLizard poisons Spock.\nPlayer 1 wins.\n"+consecutiveWins);
                consecutiveWins++;
            }
            break;

        case "spock": // User chooses spock
            System.out.println("Player 1 chooses Spock.");
            if ("rock".equalsIgnoreCase(play2)) // Rock
            {
                System.out.println("Player 2 chooses rock.
\nSpock evaporates rock.\nPlayer 1 wins.\n"+consecutiveWins);
                consecutiveWins++;
            } else if ("paper".equalsIgnoreCase(play2)) // Paper
            {
                System.out.println("Player 2 chooses paper.
\nPaper disproves Spock.\nPlayer 2 wins.\n"+consecutiveWins);
                consecutiveWins=(consecutiveWins-1);
            } else if ("scissors".equalsIgnoreCase(play2)) // Scissors
            {
                System.out.println("Player 2 chooses scissors.
\nSpock melts scissors.\nPlayer 1 wins.\n"+consecutiveWins);
                consecutiveWins++;
            } else if ("lizard".equalsIgnoreCase(play2)) // Lizard
            {
                System.out.println("Player 2 chooses lizard.
\nLizard poisons Spock.\nPlayer 2 wins.\n"+consecutiveWins);
                consecutiveWins=(consecutiveWins-1);
            } else if ("spock".equalsIgnoreCase(play2)) // Spock
            {
                System.out.println("Player 2 chooses Spock.
\nTie.\n"+consecutiveWins);
            }
            break;

        default:
            System.out.println("There was an error. Please try again.");
            break;
            }
        }
    }
public int random() {
        int random = (int) (Math.random() * 5) + 1;
        return random;
    }
public int getLastWinner() {
    return lastWinner;
    }

    // https://gist.github.com/vinothpandian/4216643#file-rpsls-in-java
    public static String convert(int n) {
        switch (n) {
        case 1:
            return "rock";
        case 2:
            return "paper";
    case 3:
        return "scissors";
    case 4:
                return "lizard";
        case 5:
        return "spock";
        default:
        return "Invalid choice";
    }
    }

}

1 个答案:

答案 0 :(得分:0)

我在您的代码中看到的问题:

while ((consecutiveWins < 4) || (consecutiveWins > -4))

每当连续的胜利变为4时,仍然满足第二个OR条件并继续执行。同样让我们来看看当球员1连续3次获胜时的情况,从那时起,球员2连续4次获胜,那么变量连续赢是否等于-1?解决问题的一个快速方法是简单地为每个玩家保存一个变量,并验证在while循环中哪个达到4。 只需将您的代码修改为我所暗示的内容:

   public class RockPaperScissorsLizardSpock {
     private int lastWinner = 0;
     public static final int ROCK = 1;
     public static final int PAPER = 2;
     public static final int SCISSORS = 3;
     public static final int LIZARD = 4;
     public static final int SPOCK = 5;
     public static final int PLAYER1 = 1;
     public static final int PLAYER2 = 2;

    public void play(int player1, int player2) {
     int consecutiveWins1 =0,consecutiveWins2=0;
     while ((consecutiveWins1 < 4) && (consecutiveWins2 < 4)) {
       String play1;
       String play2;

    player1 = random();
    player2 = random();
    play1 = convert(player1);
    play2 = convert(player2);

    switch (play1) {
    case "rock": // Player 1 chooses Rock
        System.out.println("Player 1 chooses rock.");
        if ("rock".equalsIgnoreCase(play2)) // Rock
        {   consecutiveWins1=0;
            consecutiveWins2=0;
            System.out.println("Player 2 chooses rock.\nTie.\n");
        } else if ("paper".equalsIgnoreCase(play2)) // Paper
        {
            System.out.println("Player 2 chooses paper.\nPaper covers rock.\nPlayer 2 wins.\n");
            consecutiveWins1=0;
            consecutiveWins2++;
        } else if ("scissors".equalsIgnoreCase(play2)) // Scissors
        {
            System.out.println("Player 2 chooses scissors.\nRock smashes scissors.\nPlayer 1 wins.\n");
            consecutiveWins1++;
            consecutiveWins2=0;
        } else if ("lizard".equalsIgnoreCase(play2)) // Lizard
        {
            System.out.println("Player 2 chooses lizard.\nRock crushes lizard.\nPlayer 1 wins.\n");
            consecutiveWins1++;
            consecutiveWins2=0;
        } else if ("spock".equalsIgnoreCase(play2)) // Spock
        {
            System.out.println("Player 2 chooses Spock.\nSpock vaporizes rock.\nPlayer 2 wins.\n");
            consecutiveWins1=0;
            consecutiveWins2++;
        }
        break;

    case "paper": // Player 1 chooses paper
        System.out.println("Player 1 chooses paper.");
        if ("rock".equalsIgnoreCase(play2)) // Rock
        {
            System.out.println("Player 2 chooses rock.\nPaper covers rock.\nPlayer 1 wins.\n");
            consecutiveWins1++;
            consecutiveWins2=0;
        } else if ("paper".equalsIgnoreCase(play2)) // Paper
        {
            System.out.println("Player 2 chooses paper.\nTie.\n");
            consecutiveWins1=consecutiveWins2=0;
        } else if ("scissors".equalsIgnoreCase(play2)) // Scissors
        {
            System.out.println("Player 2 chooses scissors.\nScissors cut paper.\nPlayer 2 wins.\n");
            consecutiveWins2++;
            consecutiveWins1=0;
        } else if ("lizard".equalsIgnoreCase(play2)) // Lizard
        {
            System.out.println("Player 2 chooses lizard.\nLizard eats paper.\nPlayer 2 wins.\n");
            consecutiveWins2++; consecutiveWins1=0;
        } else if ("spock".equalsIgnoreCase(play2)) // Spock
        {
            System.out.println("Player 2 chooses Spock.\nPaper disproves Spock.\nPlayer 1 wins.\n");
            consecutiveWins1++;
            consecutiveWins2=0;
        }
        break;

    case "scissors": // User chooses scissors
        System.out.println("Player 1 chooses scissors.");
        if ("rock".equalsIgnoreCase(play2)) // Rock
        {
            System.out.println("Player 2 chooses rock.\nRock smashes scissors.\nPlayer 2 wins.\n");
            consecutiveWins2++;
            consecutiveWins1=0;
        } else if ("paper".equalsIgnoreCase(play2)) // Paper
        {
            System.out.println("Player 2 chooses paper.\nPaper covers rock.\nPlayer 2 wins.\n");
            consecutiveWins2++;
            consecutiveWins1=0;
        } else if ("scissors".equalsIgnoreCase(play2)) // Scissors
        {
            System.out.println("Player 2 chooses scissors.\nTie.\n");
            consecutiveWins1=consecutiveWins2=0;
        } else if ("lizard".equalsIgnoreCase(play2)) // Lizard
        {
            System.out.println("Player 2 chooses lizard.\nRock smashes lizard.\nPlayer 1 wins.\n");
            consecutiveWins1++;
            consecutiveWins2=0;
        } else if ("spock".equalsIgnoreCase(play2)) // Spock
        {
            System.out.println("Player 2 chooses Spock.\nSpock melts scissors.\nPlayer 2 wins.\n");
            consecutiveWins2++;
            consecutiveWins1=0;
        }
        break;

    case "lizard": // User chooses lizard
        System.out.println("Player 1 chooses lizard.");
        if ("rock".equalsIgnoreCase(play2)) // Rock
        {
            System.out.println("Player 2 chooses rock.\nRock crushes lizard.\nPlayer 2 wins.\n");
            consecutiveWins2++;
            consecutiveWins1=0;
        } else if ("paper".equalsIgnoreCase(play2)) // Paper
        {
            System.out.println("Player 2 chooses paper.\nPaper covers rock.\nPlayer 2 wins.\n");
            consecutiveWins2++;
            consecutiveWins1=0;
        } else if ("scissors".equalsIgnoreCase(play2)) // Scissors
        {
            System.out.println("Player 2 chooses scissors.\nRock smashes scissors.\nPlayer 1 wins.\n");
            consecutiveWins1++;
            consecutiveWins2=0;
        } else if ("lizard".equalsIgnoreCase(play2)) // Lizard
        {
            System.out.println("Player 2 chooses lizard.\nTie.\n");
            consecutiveWins1=consecutiveWins2=0;
        } else if ("spock".equalsIgnoreCase(play2)) // Spock
        {
            System.out.println("Player 2 chooses Spock.\nLizard poisons Spock.\nPlayer 1 wins.\n");
            consecutiveWins1++;
            consecutiveWins2=0;
        }
        break;

    case "spock": // User chooses spock
        System.out.println("Player 1 chooses Spock.");
        if ("rock".equalsIgnoreCase(play2)) // Rock
        {
            System.out.println("Player 2 chooses rock.\nSpock evaporates rock.\nPlayer 1 wins.\n");
            consecutiveWins1++;
            consecutiveWins2=0;
        } else if ("paper".equalsIgnoreCase(play2)) // Paper
        {
            System.out.println("Player 2 chooses paper.\nPaper disproves Spock.\nPlayer 2 wins.\n");
            consecutiveWins2++;
            consecutiveWins1=0;
        } else if ("scissors".equalsIgnoreCase(play2)) // Scissors
        {
            System.out.println("Player 2 chooses scissors.\nSpock melts scissors.\nPlayer 1 wins.\n");
            consecutiveWins1++;
            consecutiveWins2=0;
        } else if ("lizard".equalsIgnoreCase(play2)) // Lizard
        {
            System.out.println("Player 2 chooses lizard.\nLizard poisons Spock.\nPlayer 2 wins.\n");
            consecutiveWins2++;
            consecutiveWins1=0;
        } else if ("spock".equalsIgnoreCase(play2)) // Spock
        {
            System.out.println("Player 2 chooses Spock.\nTie.\n");
            consecutiveWins1=consecutiveWins2=0;
        }
        break;

    default:
        System.out.println("There was an error. Please try again.");
        break;
        }
    }
}
 public int random() {
    int random = (int) (Math.random() * 5) + 1;
    return random;
}
public int getLastWinner() {
return lastWinner;
}

// https://gist.github.com/vinothpandian/4216643#file-rpsls-in-java
public static String convert(int n) {
    switch (n) {
    case 1:
        return "rock";
    case 2:
        return "paper";
case 3:
    return "scissors";
case 4:
            return "lizard";
    case 5:
    return "spock";
    default:
    return "Invalid choice";
}
}

  }