java - 剪刀石头布游戏

标签 java

我的应用类是石头剪刀布游戏。我的问题是,如果我赢了两次或者计算机赢了两次,我需要游戏停止生成,但它会继续运行一次。我该如何纠正这个问题?

import javax.swing.JOptionPane;
public class RockPaperScissorsApp
{
    public static void main(String args[])
    {
        String player1, winner;
        int player2, gamesPlayed = 1, player1Wins = 0, player2Wins = 0;
        do
        {
            RockPaperScissors myRock = new RockPaperScissors();

            player1 = JOptionPane.showInputDialog(null,
                    "Please enter your choice, Rock, Paper or Scissors");
            myRock.setPlayer1(player1);

            myRock.compute();

            winner = myRock.getWinner();
            player2 = myRock.getPlayer2();

            if(winner.equals("Player 1"))
            {
                if(player2 == 1)
                {
                    JOptionPane
                            .showMessageDialog(null,
                                    "Congratulations, you have beaten the computer! The computer chose Rock");
                }
                else if(player2 == 2)
                {
                    JOptionPane
                            .showMessageDialog(null,
                                    "Congratulations, you have beaten the computer! The computer chose Paper");
                }
                else
                {
                    JOptionPane
                            .showMessageDialog(null,
                                    "Congratulations, you have beaten the computer! The computer chose Scissors");
                }
                player1Wins = player1Wins + 1;
            }

            else if(winner.equals("Player 2"))
            {
                if(player2 == 1)
                {
                    JOptionPane
                            .showMessageDialog(null,
                                    "Hard Luck, you have been beaten by the computer! The computer chose Rock");
                }
                else if(player2 == 2)
                {
                    JOptionPane
                            .showMessageDialog(null,
                                    "Hard Luck, you have been beaten by the computer!The computer chose Paper");
                }
                else
                {
                    JOptionPane
                            .showMessageDialog(null,
                                    "Hard Luck, you have been beaten by the computer! The computer chose Scissors");
                }
                player2Wins = player2Wins + 1;
            }

            else if(winner.equals("draw"))
            {
                if(player2 == 1)
                {
                    JOptionPane.showMessageDialog(null,
                            "It was a draw this time! The computer chose Rock");
                }
                else if(player2 == 2)
                {
                    JOptionPane
                            .showMessageDialog(null,
                                    "It was a draw this time! The computer chose Paper");
                }
                else
                {
                    JOptionPane
                            .showMessageDialog(null,
                                    "It was a draw this time! The computer chose Scissors");
                }
                gamesPlayed = gamesPlayed + 0;

            }
            else
            {
                JOptionPane.showMessageDialog(null,
                        "You have entered an invalid option");

                gamesPlayed = gamesPlayed - 1;
            }

            if(player1Wins == 2)
            {
                JOptionPane.showMessageDialog(null, "You win");
                gamesPlayed = gamesPlayed + 2;
            }
            else if(player2Wins == 2)
            {
                JOptionPane.showMessageDialog(null, "He wins");
                gamesPlayed = gamesPlayed + 2;
            }

            if((gamesPlayed == 2) || (gamesPlayed == 3))
            {
                JOptionPane.showMessageDialog(null, "The score is "
                        + player1Wins + " for player1 and " + player2Wins
                        + " for player2");
            }
            gamesPlayed = gamesPlayed + 1;
        }
        while(gamesPlayed <= 3);
    }
}

最佳答案

改变你的 while 循环条件:

while(player1Wins < 2 && player2Wins < 2)

此外,我建议不要使用魔数(Magic Number);这更易于维护:

public static final int VICTORY_THRESHOLD = 2;
.
.
.
while(player1Wins < VICTORY_THRESHOLD 
   && player2Wins < VICTORY_THRESHOLD)

另外,考虑为 ROCK,PAPER,SCISORS 创建一个 Enum。然后考虑制作一个比较器,它接受两个枚举并返回 -1 表示输,0 表示平局,1 表示赢。​​

public Enum RPS {
  ROCK(),PAPER(),SCISSORS();

  public int compare(RPS that) {
    if(this==that)
      return 0;

    switch(this) {
      case ROCK:     return that==RPS.PAPER    ? -1 : 1;
      case PAPER:    return that==RPS.SCISSORS ? -1 : 1;
      case SCISSORS: return that==RPS.ROCK     ? -1 : 1;
      default: return null; /* never reached */
    }
  }
  public String toString() {
    switch(this) {
      case ROCK:     return "Rock";
      case PAPER:    return "Paper";
      case SCISSORS: return "Scissors";
      default: return null; /* never reached */
    }
  }
}

使用 Enum 会让你的条件代码更简洁:

RPS player1Choice = RPS.ROCK;
RPS player2Choice = RPS.SCISSORS;
int result player1Choice.compare(player2Choice); /* 1 */

if(result == 0) 
  System.out.println("Draw, you both chose "+player1Choice);
else
  System.out.println("You "+ (result > 0 ? "won" : "lost") +
                     " with "+player1Choice+
                     "\nThe computer chose"+player2Choice);
if(result != 0)
   result >  0 ? ++player1Wins : ++player2Wins;
++gamesPlayed;

关于java - 剪刀石头布游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20177558/

相关文章:

java - android/java拆分问题

java - 使用 REST 传输数据

java - 定位器响应

java - 使用 Spring IoC 设置枚举值

java - AEM Assets API : Create intermediate directories as required

java - Android ListView 未更新

java - 为什么 Angular 不通过 POST 请求发送 cookie,即使我已将 withCredentials 设置为 true?

java - 接缝测试 NoSuchMethodError

java - 如何创建 XML header ?

java - 在不使用断言的情况下对此方法进行 Junit 测试?