2013-11-24 40 views
3

我有我的應用程序類的遊戲岩石,紙,剪刀。我的問題是,如果我贏了兩次,或者電腦贏了兩次,我需要遊戲停止生成,但它會一直持續下去。我該如何糾正這一點?岩石,紙,剪刀遊戲

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); 
    } 
} 
+3

'gamesPlayed = gamesPlayed + 0;'?真? :) – zbr

+0

你目前的循環沒有考慮到現場鎖定,玩家連續打球...... 3場比賽結束後沒有贏家。 –

+3

這段代碼有很多方法可以改進,以至於codereview.se會更好。 :) –

回答

4

你說do... while(gamesPlayed <= 3)這意味着經過3場激烈的比賽,該聲明仍然是正確的,所以它會開始重新循環。

若要解決此問題,您可以將該3更改爲2或將<=標誌更改爲<標誌。

7

更改while循環條件:

while(player1Wins < 2 && player2Wins < 2) 

另外,我建議不要使用幻數;這是更易於維護:

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

另外,考慮爲ROCK,PAPER,SCISORSEnum。然後考慮製作一個Comparator,其中包含兩個Enums並返回-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;