首页 > 解决方案 > Java 石头剪刀布 - 一切都是平局

问题描述

我正在为 java 中的 CompSci 入门课程做一个项目,但我被卡住了。我们必须填写一个名为“棍、火、水”的石头、纸、剪刀游戏的方法。我以为我填写的正确,但是每次运行它时,我的程序都说是平局。它显示了我的选择和计算机的选择,但它说它是平局并且它不会增加所玩的回合数(或者我的分数或计算机的分数,但是因为它是一个无论如何都不会发生的平局,尽管我我确信它仍然一团糟)。


/* This class ecapsulates the state and logic required to play the 
   Stick, Water, Fire game. The game is played between a user and the computer. 
   A user enters their choice, either S for stick, F for fire, W for water, and 
   the computer generates one of these choices at random- all equally likely.
   The two choices are evaluated according to the rules of the game and the winner
   is declared.
   
   Rules of the game:
   S beats W
   W beats F
   F beats S
   no winner on a tie.
   
   Each round is executed by the playRound method. In addition to generating the computer 
   choice and evaluating the two choices, this class also keeps track of the user and computer
   scores, the number of wins, and the total number of rounds that have been played. In the case
   of a tie, neither score is updated, but the number of rounds is incremented.
   
   NOTE: Do not modify any of the code that is provided in the starter project. Additional instance variables and methods 
   are not required to make the program work correctly, but you may add them if you wish as long as
   you fulfill the project requirements.
   
*/
public class StickWaterFireGame {

      // TODO 1: Declare private instance variables here:
     
   Random rand = new Random();
   int numRounds = 0;
   int playerScore = 0;
   int computerScore = 0;
   boolean playerWins = false;
   boolean isTie = false;
   
  /*  This constructor assigns the member Random variable, rand, to 
   *  a new, unseeded Random object.
   *  It also initializes the instance variables to their default values:
   *  rounds, player and computer scores will be 0, the playerWins and isTie
   *  variables should be set to false.
   */ 
   public StickWaterFireGame() {
   // TODO 2: Implement this method.
  
   Random rand = new Random();
   numRounds = 0;
   playerScore = 0;
   computerScore = 0;
   playerWins = false;
   isTie = false;
  
   }
  
  /*  This constructor assigns the member Random variable, rand, to 
   *  a new Random object using the seed passed in.
   *  It also initializes the instance variables to their default values:
   *  rounds, player and computer scores will be 0, the playerWins and isTie
   *  variables should be set to false.
   */    
   public StickWaterFireGame(int seed) {
   // TODO 3: Implement this method.
  
   Random rand = new Random(seed);
   numRounds = 0;
   playerScore = 0;
   computerScore = 0;
   playerWins = false;
   isTie = false;
  
   }   

  /*  This method returns true if the inputStr passed in is
   *  either "S", "W", or "F", false otherwise.
   *  Note that the input can be upper or lower case.
   */ 
   public boolean isValidInput(String inputStr) {
   // TODO 4: Implement this method.
   
   if (inputStr.equalsIgnoreCase("S") || inputStr.equalsIgnoreCase("W") || inputStr.equalsIgnoreCase("F")) {
      return true;
      }
      
   else {
      return false; 
      
      }
   }
   
    // Returns the choice of the computer for the most recent round of play
   public String getComputerChoice(){
   // TODO 5: Implement this method.
             
    return getRandomChoice();
   }
            
   // Returns true if the player has won the last round, false otherwise.    
   public boolean playerWins(){
   // TODO 6: Implement this method.
      
      if (playerWins) {
      return true;
      }
      
      else {
      return false;
      }
   }
   
   // Returns the player's cumulative score.    
   public int getPlayerScore(){
   // TODO 7: Implement this method.
      return playerScore;
   }
   
   // Returns the computer's cumulative score.   
   public int getComputerScore(){
   // TODO 8: Implement this method.
      return computerScore;
   }
   
   // Returns the total nuber of rounds played.   
   public int getNumRounds(){
   // TODO 9: Implement this method.
      return numRounds;
   }

   // Returns true if the player and computer have the same score on the last round, false otherwise.    
   public boolean isTie(){
   // TODO 10: Implement this method.
      
      if (computerScore == playerScore) {
         return true;
         }
         
      else {
      return false;
      }
      
   }

  /*  This "helper" method uses the instance variable of Random to generate an integer
   *  which it then maps to a String: "S", "W", "F", which is returned.
   *  This method is called by the playRound method.
   */
   private String getRandomChoice() {
   // TODO 11: Implement this method.
    
    String randomChoice = "";
    Random rand = new Random();
    int randomInput = rand.nextInt(3) + 1;
    
    if (randomInput == 1) {
      randomChoice = "S";
      }
      
    if (randomInput == 2) {
      randomChoice = "W";
      }
      
    if (randomInput == 3) {
      randomChoice = "F";
      }
   
      return randomChoice;
   }

 /*  This method carries out a single round of play of the SWF game. 
   *  It calls the isValidInput method and the getRandomChoice method. 
   *  It implements the rules of the game and updates the instance variables
   *  according to those rules.
   */
   public void playRound(String playerChoice) {
   // TODO 12: Implement this method.
  
   int numRounds = 0;
   int playerScore = 0;
   int computerScore = 0;
   boolean playerWins = false;
   boolean isTie = false;
   String computerChoice = getRandomChoice();
   
   if (!isValidInput(playerChoice)) {
      ++computerScore;
      ++numRounds;
      }
      
      else {
         
         if (computerChoice.equalsIgnoreCase(playerChoice)) {
            ++numRounds;
            }
            
         if (computerChoice.equals("S") && playerChoice.equalsIgnoreCase("W")) {
            ++computerScore;
            ++numRounds;
            }
            
         if (computerChoice.equals("S") && playerChoice.equalsIgnoreCase("F")) {
            playerWins = true;
            ++playerScore;
            ++numRounds;
            }
           
         if (computerChoice.equals("W") && playerChoice.equalsIgnoreCase("S")) {
            playerWins = true;
            ++playerScore;
            ++numRounds;
            }
            
         if (computerChoice.equals("W") && playerChoice.equalsIgnoreCase("F")) {
            ++computerScore;
            ++numRounds;
            }
            
         if (computerChoice.equals("F") && playerChoice.equalsIgnoreCase("S")) {
            ++computerScore;
            ++numRounds;
            }
            
         if (computerChoice.equals("F") && playerChoice.equalsIgnoreCase("W")) {
            playerWins = true;
            ++playerScore;
            ++numRounds;
            }
          }
        }
      } ```   

标签: java

解决方案


您的代码有许多可疑之处,但您描述的具体问题可能与您的playRound()方法声明的局部变量遮蔽了大多数类的实例变量这一事实有关:

   int numRounds = 0;
   int playerScore = 0;
   int computerScore = 0;
   boolean playerWins = false;
   boolean isTie = false;

该方法操纵那些局部变量而不是类似名称的实例变量,并且在该方法的一次执行期间,只能在该方法内部看到效果。只需删除那些局部变量声明就可以推动您前进。


推荐阅读