Java剪刀石头布 – 一切皆为平局

huangapple go评论68阅读模式
英文:

Java Rock Paper Scissors - Everything Is a Tie

问题

import java.util.Random;

public class StickWaterFireGame {

   private Random rand = new Random();
   private int numRounds = 0;
   private int playerScore = 0;
   private int computerScore = 0;
   private boolean playerWins = false;
   private boolean isTie = false;

   public StickWaterFireGame() {
       rand = new Random();
       numRounds = 0;
       playerScore = 0;
       computerScore = 0;
       playerWins = false;
       isTie = false;
   }

   public StickWaterFireGame(int seed) {
       rand = new Random(seed);
       numRounds = 0;
       playerScore = 0;
       computerScore = 0;
       playerWins = false;
       isTie = false;
   }

   public boolean isValidInput(String inputStr) {
       if (inputStr.equalsIgnoreCase("S") || inputStr.equalsIgnoreCase("W") || inputStr.equalsIgnoreCase("F")) {
           return true;
       } else {
           return false;
       }
   }

   public String getComputerChoice(){
       return getRandomChoice();
   }

   public boolean playerWins(){
       return playerWins;
   }

   public int getPlayerScore(){
       return playerScore;
   }

   public int getComputerScore(){
       return computerScore;
   }

   public int getNumRounds(){
       return numRounds;
   }

   public boolean isTie(){
       return isTie;
   }

   private String getRandomChoice() {
       String randomChoice = "";
       int randomInput = rand.nextInt(3) + 1;

       if (randomInput == 1) {
           randomChoice = "S";
       } else if (randomInput == 2) {
           randomChoice = "W";
       } else if (randomInput == 3) {
           randomChoice = "F";
       }

       return randomChoice;
   }

   public void playRound(String playerChoice) {
       String computerChoice = getRandomChoice();

       if (!isValidInput(playerChoice)) {
           computerScore++;
           numRounds++;
       } else {
           if (computerChoice.equalsIgnoreCase(playerChoice)) {
               numRounds++;
           } else if ((computerChoice.equals("S") && playerChoice.equalsIgnoreCase("W")) ||
                      (computerChoice.equals("W") && playerChoice.equalsIgnoreCase("F")) ||
                      (computerChoice.equals("F") && playerChoice.equalsIgnoreCase("S"))) {
               computerScore++;
               numRounds++;
           } else {
               playerWins = true;
               playerScore++;
               numRounds++;
           }
       }
   }
}
英文:

I am working on a project for an introductory CompSci course in java and I am stuck. We have to fill out the methods for a rock, paper, scissor game called stick, fire, water. I thought I filled it out correctly, but every time I run it, my program says it is a tie. It displays both my choice and the computer choice, but it says it is a tie and it doesn't increment the rounds played (or either my score or the computer's score, but since it is a tie that would not happen anyways, although I am sure it is still messed up).


/* 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;
}
}
}
} ```   
</details>
# 答案1
**得分**: 0
关于你的代码有许多可疑之处,但你描述的特定问题很可能与你的 `playRound()` 方法有关,该方法声明了局部变量,这些变量遮蔽了类中大部分实例变量:
>        int numRounds = 0;
>        int playerScore = 0;
>        int computerScore = 0;
>        boolean playerWins = false;
>        boolean isTie = false;
该方法操作这些局部变量而不是同名的实例变量,效果只能在该方法内部看到,仅在一次执行期间可见。只需移除这些局部变量声明即可继续前进。
<details>
<summary>英文:</summary>
There are numerous suspicious things about your code, but the specific issues you describe are likely related to the fact that your `playRound()` method declares local variables shadowing most of the class&#39;s instance variables:
&gt;        int numRounds = 0;
&gt;        int playerScore = 0;
&gt;        int computerScore = 0;
&gt;        boolean playerWins = false;
&gt;        boolean isTie = false;
That method manipulates those local variables rather than the like-named instance variables, and the effects can be seen only inside that method, for the duration of one execution of it.  Just removing those local-variable declarations should move you forward.
</details>
# 答案2
**得分**: 0
看这个方法。在你将它们设为0时,你重新声明了三个变量。我认为你只是想要赋值它们。
```java
public void playRound(String playerChoice) {
// TODO 12: 实现这个方法。
int numRounds = 0;
int playerScore = 0;
int computerScore = 0;
boolean playerWins = false;
boolean isTie = false;
String computerChoice = getRandomChoice();
英文:

Look at this method. You are redelaring three variables when you set them to 0. I think you just want to assign them.

  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();
</details>

huangapple
  • 本文由 发表于 2020年10月8日 06:22:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64253134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定