In: Computer Science
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
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.