Question

In: Computer Science

1 import java.util.Random; 2 3 /* This class ecapsulates the state and logic required to play...

1 import java.util.Random;
2
3 /* This class ecapsulates the state and logic required to play the
4 Stick, Water, Fire game. The game is played between a user and the computer.
5 A user enters their choice, either S for stick, F for fire, W for water, and
6 the computer generates one of these choices at random- all equally likely.
7 The two choices are evaluated according to the rules of the game and the winner
8 is declared.
9
10 Rules of the game:
11 S beats W
12 W beats F
13 F beats S
14 no winner on a tie.
15
16 Each round is executed by the playRound method. In addition to generating the computer
17 choice and evaluating the two choices, this class also keeps track of the user and computer
18 scores, the number of wins, and the total number of rounds that have been played. In the case
19 of a tie, neither score is updated, but the number of rounds is incremented.
20
21 NOTE: Do not modify any of the code that is provided in the starter project. Additional instance variables and methods
22 are not required to make the program work correctly, but you may add them if you wish as long as
23 you fulfill the project requirements.
24
25 */
26 public class StickWaterFireGame {
27
28
29 // TODO 1: Declare private instance variables here:
30
31
32 /* This constructor assigns the member Random variable, rand, to
33 * a new, unseeded Random object.
34 * It also initializes the instance variables to their default values:
35 * rounds, player and computer scores will be 0, the playerWins and isTie
36 * variables should be set to false.
37 */
38 public StickWaterFireGame() {
39 // TODO 2: Implement this method.
40
41 }
42
43 /* This constructor assigns the member Random variable, rand, to
44 * a new Random object using the seed passed in.
45 * It also initializes the instance variables to their default values:
46 * rounds, player and computer scores will be 0, the playerWins and isTie
47 * variables should be set to false.
48 */
49 public StickWaterFireGame(int seed) {
50 // TODO 3: Implement this method.
51
52 }
53
54 /* This method returns true if the inputStr passed in is
55 * either "S", "W", or "F", false otherwise.
56 * Note that the input can be upper or lower case.
57 */
58 public boolean isValidInput(String inputStr) {
59 // TODO 4: Implement this method.
60 return false;
61 }
62
63 /* This method carries out a single round of play of the SWF game.
64 * It calls the isValidInput method and the getRandomChoice method.
65 * It implements the rules of the game and updates the instance variables
66 * according to those rules.
67 */
68 public void playRound(String playerChoice) {
69 // TODO 12: Implement this method.
70 }
71
72 // Returns the choice of the computer for the most recent round of play
73 public String getComputerChoice(){
74 // TODO 5: Implement this method.
75 return null;
76 }
77
78 // Returns true if the player has won the last round, false otherwise.
79 public boolean playerWins(){
80 // TODO 6: Implement this method.
81 return false;
82 }
83
84 // Returns the player's cumulative score.
85 public int getPlayerScore(){
86 // TODO 7: Implement this method.
87 return 0;
88 }
89
90 // Returns the computer's cumulative score.
91 public int getComputerScore(){
92 // TODO 8: Implement this method.
93 return 0;
94 }
95
96 // Returns the total nuber of rounds played.
97 public int getNumRounds(){
98 // TODO 9: Implement this method.
99 return 0;
100 }
101
102 // Returns true if the player and computer have the same score on the last round, false otherwise.
103 public boolean isTie(){
104 // TODO 10: Implement this method.
105 return false;
106 }
107
108 /* This "helper" method uses the instance variable of Random to generate an integer
109 * which it then maps to a String: "S", "W", "F", which is returned.
110 * This method is called by the playRound method.
111 */
112 private String getRandomChoice() {
113 // TODO 11: Implement this method.
114 return null;
115 }
116 }
117

Solutions

Expert Solution

Code--

import java.util.Random;
public class StickWaterFireGame
{
   // TODO 1: Declare private instance variables here:
   private boolean playerWins;
   private boolean isTie;
   private int rounds;
   private int playerScore;
   private int computerScore;
   private Random rand;
   private String playerChoice;
   private String computerChoice;
  
   public StickWaterFireGame()
   {
       // TODO 2: Implement this method.
       rand=new Random();
       this.rounds=0;
       this.playerScore=0;
       this.computerScore=0;
       this.playerWins=false;
       this.isTie=false;
       this.playerChoice=null;
       this.computerChoice=null;
   }
  
  
   public StickWaterFireGame(int seed)
   {
       // TODO 3: Implement this method.
       rand=new Random(seed);
       this.rounds=0;
       this.playerScore=0;
       this.computerScore=0;
       this.playerWins=false;
       this.isTie=false;
       this.playerChoice=null;
       this.computerChoice=null;
   }
  
   public boolean isValidInput(String inputStr)
   {
       // TODO 4: Implement this method.
       if(inputStr.equals("S") || inputStr.equals("W") || inputStr.equals("F"))
           return true;
       return false;
   }
  
   public void playRound(String playerChoice)
   {
       // TODO 12: Implement this method.
       if(!this.isValidInput(playerChoice))
       {
           return;
       }
       this.playerChoice=playerChoice;
       this.computerChoice=this.getRandomChoice();
       if(this.playerChoice.equals("S")&&computerChoice.equals("W"))
       {
           this.playerWins=true;
           this.isTie=false;
       }
       else if(this.playerChoice.equals("W")&&computerChoice.equals("S"))
       {
           this.playerWins=false;
           this.isTie=false;
       }
       else if(this.playerChoice.equals("W")&&computerChoice.equals("F"))
       {
           this.playerWins=true;
           this.isTie=false;
       }
       else if(this.playerChoice.equals("F")&&computerChoice.equals("W"))
       {
           this.playerWins=false;
           this.isTie=false;
       }
       else if(this.playerChoice.equals("F")&&computerChoice.equals("S"))
       {
           this.playerWins=true;
           this.isTie=false;
       }
       else if(this.playerChoice.equals("S")&&computerChoice.equals("F"))
       {
           this.playerWins=false;
           this.isTie=false;
       }
       else
       {
           this.playerWins=false;
           this.isTie=true;
       }
       this.rounds++;
       //add scores
       if(this.playerWins==true && this.isTie==false)
       {
           this.playerScore++;
       }
       else if(this.playerWins==false && this.isTie==false)
       {
           this.computerScore++;
       }
   }
  
   // Returns the choice of the computer for the most recent round of play
   public String getComputerChoice()
   {
       // TODO 5: Implement this method.
       return this.computerChoice;
   }
  
   // Returns true if the player has won the last round, false otherwise.
   public boolean playerWins()
   {
       // TODO 6: Implement this method.
       return this.playerWins;
   }
  
   // Returns the player's cumulative score.
   public int getPlayerScore()
   {
       // TODO 7: Implement this method.
       return this.playerScore;
   }
  
   // Returns the computer's cumulative score.
   public int getComputerScore()
   {
       // TODO 8: Implement this method.
       return this.computerScore;
   }
  
   // Returns the total number of rounds played.
   public int getNumRounds()
   {
       // TODO 9: Implement this method.
       return this.rounds;
   }
  
   // 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.
       return this.isTie;
   }
  
   private String getRandomChoice()
   {
       // TODO 11: Implement this method.
       int choice=this.rand.nextInt(2);
       if(choice==0)
           return "S";
       else if(choice==1)
           return "W";
       else
           return "F";
   }
}


I have given a main function implementation so that you understand the code better.

Main Function--

import java.util.*;
public class Program
{
   public static void main(String[] args)
   {
       Scanner sc=new Scanner(System.in);
       StickWaterFireGame game=new StickWaterFireGame();
       String ch;
       while(true)
       {
           System.out.print("Enter your choice: ");
           ch=sc.nextLine();
           game.playRound(ch);
           System.out.println("Computers Choice: "+game.getComputerChoice());
           if(game.isTie())
               System.out.println("It's a TIE..!");
           else if(game.playerWins())
               System.out.println("Player wins..!");
           else
               System.out.println("Computer wins..!");
           System.out.println("Total rounds: "+game.getNumRounds());
           System.out.println("Computer Score: "+game.getComputerScore());
           System.out.println("Players score: "+game.getPlayerScore());
           System.out.println();
       }
   }
}

Sample test case output--

Note--

Please upvote if you like the effort.


Related Solutions

import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following:...
import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following: // // 1.) Define a private instance variable which can // hold a reference to a Random object. // // 2.) Define a constructor which takes a seed value. // This seed will be used to initialize the // aforementioned Random instance variable. // // 3.) Define a static method named numberToDirection // which takes a direction number and returns a String // representing...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
I need a pseudocode and UML for this program. import java.util.Random; public class SortandSwapCount {   ...
I need a pseudocode and UML for this program. import java.util.Random; public class SortandSwapCount {    public static void main(String[] args) {        //Generate random array for test and copy that into 3 arrays        int[] arr1=new int[20];        int[] arr2=new int[20];        int[] arr3=new int[20];        int[] arr4=new int[20];        Random rand = new Random();        for (int i = 0; i < arr1.length; i++) {            //Generate random array...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r =...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r = new Random(seed); }    public static String numberToDirection(int a){ if(a==0) return "North";    if(a==1) return "NorthEast"; if(a==2) return "East"; if(a==3) return "Southeast"; if(a==4) return "South"; if(a==5) return "Southwest"; if(a==6) return "West";    if(a==7) return "Northwest";    return "Invalid Direction" ; } public String randomDirection(){ return numberToDirection(r.nextInt()% 4 + 1); } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter seed: ");...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) {...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) { int N; Scanner keybd = new Scanner(System.in); int[] counts = new int[12];    System.out.print("Enter the number of trials: "); N = keybd.nextInt();    Random die1 = new Random(); Random die2 = new Random(); int value1, value2, sum; for(int i = 1; i <= N; i++) { value1 = die1.nextInt(6) + 1; value2 = die2.nextInt(6) + 1; sum = value1 + value2; counts[sum-1]++; }   ...
Let A = {1, 2, 3}. For each of the following relations state (no proofs required)...
Let A = {1, 2, 3}. For each of the following relations state (no proofs required) whether it is (i) both a function and an equivalence relation (ii) a function but not an equivalence relation (iii) an equivalence relation but not a function (iv) neither a function nor an equivalence relation (a) {(1, 1),(2, 2),(3, 3)} ⊆ A × A (b) {(1, 1),(2, 2)} ⊆ A × A (c) {(1, 1),(2, 2),(3, 2)} ⊆ A × A (d) {(1, 1),(2,...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts)...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts) { return quarts * 0.25; } public static double milesToFeet(double miles) { return miles * 5280; } public static double milesToInches(double miles) { return miles * 63360; } public static double milesToYards(double miles) { return miles * 1760; } public static double milesToMeters(double miles) { return miles * 1609.34; } public static double milesToKilometer(double miles) { return milesToMeters(miles) / 1000.0; } public static double...
needed asap !!! 1. import java.utility.Random; 2.   3. public class Sequen 4.   5. private int stand  ...
needed asap !!! 1. import java.utility.Random; 2.   3. public class Sequen 4.   5. private int stand   6. private int calc;   7.   8. public Sequen(int numStand) 9. { 10.         stand = numStand; 11.         skip; 12.         } 13.           14.         public void skip() 15.         { 16.         Random sit = new Random(); 17.         calc = sit.nextInt(stand) + 1; 18.         } 19.           20.         public getStand() 21.         { 22.         return stand; 23.         } 24.           25.         int getCalc() 26.         { 27.         return calc; 28.         } 29.         } One error is already identified for you as shown below: Line Number: 5 Error description: Missing semicolon...
*// 1- Add JavaDoc to This classes 2- Mak a UML */ import java.util.*; public class...
*// 1- Add JavaDoc to This classes 2- Mak a UML */ import java.util.*; public class Display { public static void main(String[] args) { altEnerCar car1 = new altEnerCar(20000, 2001, 20000); altEnerCar car2 = new HydrogenCar(0, 2012, 50000, 100, false); altEnerCar car3 = new ElectricCar(0, 2014, 30000, 10, 50); altEnerCar car4 = new NaturalGasCar(0, 2000, 60000, 5, 20); altEnerCar car5 = new PropaneCar(0, 2011, 45000, 10, true); ArrayList<altEnerCar> cars = new ArrayList<altEnerCar>(); cars.add(car1); cars.add(car2); cars.add(car3); cars.add(car4); cars.add(car5); Collections.sort(cars); System.out.println(cars); }...
1) Only a single main class is required 2) The main class must contain a static...
1) Only a single main class is required 2) The main class must contain a static method called "getInputWord" that prompts a user to input a word from the keyboard and returns that word as a String. This method must use "JOptionPane.showInputDialog" to get the input from the user (you can find info about JOptionPane in JavaDocs) 3) The main class must contain a static method called "palindromeCheck" that takes a String as an input argument and returns a boolean...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT