In: Computer Science
You will develop code in the StickWaterFireGame class only. Your tasks are indicated by a TODO. You will also find more instructions in the form of comments that detail more specifically
what each part of the class is supposed to do, so make sure to
read them. Do not delete or modify the method headers in the
starter code!
Here is an overview of the TODOs in the class.
TODO 1: Declare the instance variables of the class. Instance variables are private variables that keep the state of the game. The recommended instance variables are:
1. A variable, “rand” that is the instance of the Random class.
It will be initialized in a constructor (either seeded or unseeded)
and will be used by the getRandomChoice() method.
2. A variable to keep the player’s cumulative score. This is the
count of the number of times the player (the user) has won a round.
Initial value: 0.
3. A variable to keep the computer’s cumulative score. This is the count of the number of times the computer has won a round. Initial value: 0.
4. A variable to keep the count of the number of rounds that have been played. Initial value: 0.
5. A variable to keep track of if the player has won the current round (round-specific): true or false. This would be determined by the playRound method. Initial value: false.
6. A variable to keep track of if the player and computer have tied in the current round (round-specific): true or false. This would be determined by the playRound method. Initial value: false.
The list above contains the minimum variables to make a working program. You may declare more instance variables as you wish.
TODOs 2 and 3: Implement the constructors. The constructors assign the instance variables to their initial values. In the case of the Random class instance variable, it is initialized to a new instance of the Random class in the constructors. If a constructor has a seed parameter, the seed is passed to the Random constructor. If the constructor does not have a seed parameter, the default (no parameter) Random constructor is called.
TODO 4: This method returns true if the inputStr passed in is one of the following: "S", "s", "W", "w", "F", "f", false otherwise. Note that the input can be upper or lower case.
TODOs 5, 6, 7, 8, 9, 10 These methods just return the values of their respective instance variables.
TODO 11: This is a private method that is called by the playRound method. It uses the instance variable of the Random class to generate an integer that can be “mapped” to one of the three Strings: "S", "W", "F", and then returns that String, which represents the computer’s choice.
TODO 12: The playRound method carries out a single round of play of the SWF game. This is the major, “high-level” method in the class. This method does many tasks including the following steps:
1. Reset the variables that keep round-specific data to their
initial values.
2. Assign the computer’s choice to be the output of the
getRandomChoice method.
3. Check that the player’s choice is valid by calling isValidInput.
If the player's input is not valid, the computer wins by default.
This counts as a round, so the number of rounds is incremented. 4.
If the player’s choice is valid, the computer's choice is compared
to the player's choice in a series of conditional statements to
determine if there is a tie, or who wins this round of play
according to the rules of the game:
S beats W
W beats F
F beats S
Both have the same choice- a tie.
The player and computer scores are updated depending on who wins. In the event of a tie, neither player has their score updated. Finally, the number of rounds of play is incremented.
Note: Do not duplicate the isValidInput or the getRandomChoice code in playRound. Call these methods from playRound. The point is that delegating tasks to other methods makes the code easier to read, modify and debug.
THE CODE:
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 // TODO 1: Declare private instance variables here:
29
30
31 /* This constructor assigns the member Random variable, rand,
to
32 * a new, unseeded Random object.
33 * It also initializes the instance variables to their default
values:
34 * rounds, player and computer scores will be 0, the playerWins
and isTie
35 * variables should be set to false.
36 */
37 public StickWaterFireGame() {
38 // TODO 2: Implement this method.
39
40 }
41
42 /* This constructor assigns the member Random variable, rand,
to
43 * a new Random object using the seed passed in.
44 * It also initializes the instance variables to their default
values:
45 * rounds, player and computer scores will be 0, the playerWins
and isTie
46 * variables should be set to false.
47 */
48 public StickWaterFireGame(int seed) {
49 // TODO 3: Implement this method.
50
51 }
52
53 /* This method returns true if the inputStr passed in is
54 * either "S", "W", or "F", false otherwise.
55 * Note that the input can be upper or lower case.
56 */
57 public boolean isValidInput(String inputStr) {
58 // TODO 4: Implement this method.
59 return false;
60 }
61
62 /* This method carries out a single round of play of the SWF
game.
63 * It calls the isValidInput method and the getRandomChoice
method.
64 * It implements the rules of the game and updates the instance
variables
65 * according to those rules.
66 */
67 public void playRound(String playerChoice) {
68 // TODO 12: Implement this method.
69 }
70
71 // Returns the choice of the computer for the most recent round
of play
72 public String getComputerChoice(){
73 // TODO 5: Implement this method.
74 return null;
75 }
76
77 // Returns true if the player has won the last round, false
otherwise.
78 public boolean playerWins(){
79 // TODO 6: Implement this method.
80 return false;
81 }
82
83 // Returns the player's cumulative score.
84 public int getPlayerScore(){
85 // TODO 7: Implement this method.
86 return 0;
87 }
88
89 // Returns the computer's cumulative score.
90 public int getComputerScore(){
91 // TODO 8: Implement this method.
92 return 0;
93 }
94
95 // Returns the total nuber of rounds played.
96 public int getNumRounds(){
97 // TODO 9: Implement this method.
98 return 0;
99 }
100
101 // Returns true if the player and computer have the same score
on the last round, false otherwise.
102 public boolean isTie(){
103 // TODO 10: Implement this method.
104 return false;
105 }
106
107 /* This "helper" method uses the instance variable of Random to
generate an integer
108 * which it then maps to a String: "S", "W", "F", which is
returned.
109 * This method is called by the playRound method.
110 */
111 private String getRandomChoice() {
112 // TODO 11: Implement this method.
113 return null;
114 }
115 }
116
i had solved this earlier too, code remains the same it is as below:
import java.util.Random;
/* 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.
*/
public class StickWaterFireGame {
private int playerScore;
private int computerScore;
private int rounds;
private boolean playerWins;
private boolean isTie;
private Random rand;
private String playerChoice;
private String computerChoice;
/*
* 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. 36
*/
public StickWaterFireGame() {
rand = new Random();
playerScore = 0;
computerScore = 0;
rounds = 0;
playerWins = false;
isTie = false;
playerChoice = "";
computerChoice = "";
}
/*
* 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) {
rand = new Random(seed);
playerScore = 0;
computerScore = 0;
rounds = 0;
playerWins = false;
isTie = false;
playerChoice = "";
computerChoice = "";
}
/*
* 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) {
if (inputStr.equalsIgnoreCase("s"))
{
return
true;
} else if
(inputStr.equalsIgnoreCase("w")) {
return
true;
} else if
(inputStr.equalsIgnoreCase("f")) {
return
true;
} else {
return
false;
}
}
/*returns computers choice*/
public String getComputerChoice() {
return computerChoice;
}
/*returns true if player beats computer*/
public boolean playerWins() {
if ((playerChoice.equals("S"))
&& (computerChoice.equals("W"))) {
return
true;
} else if
((playerChoice.equals("W")) &&
(computerChoice.equals("F"))) {
return
true;
} else if
((playerChoice.equals("F")) &&
(computerChoice.equals("S"))) {
return
true;
} else {
return
false;
}
}
/*return current score of the player*/
public int getPlayerScore() {
return playerScore;
}
/*returns the current computer score*/
public int getComputerScore() {
return computerScore;
}
/*returns number of rounds that have happened*/
public int getNumRounds() {
return rounds;
}
/*its a tie when both player and computer have same
choices*/
public boolean isTie() {
if
(playerChoice.equals(computerChoice)) {
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() {
int num;
num = rand.nextInt(3);
String computerChoice = "";
if (num == 0) {
computerChoice =
"S";
} else if (num == 1) {
computerChoice =
"W";
} else if (num == 2) {
computerChoice =
"F";
}
return computerChoice;
}
/*
* 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) {
if (isValidInput(playerChoice))
{
this.playerChoice = playerChoice.toUpperCase();
rounds++;
this.computerChoice = getRandomChoice();
if (playerWins()
== true) {
playerScore++;
playerWins = true;
} else if
(isTie() == true) {
isTie = true;
playerWins = false;
} else {
computerScore++;
playerWins = false;
}
} else {
rounds++;
computerScore++;
playerWins =
false;
isTie =
false;
}
}
}
Main class to test above code is:
import java.util.Scanner;
public class StickWaterFireGameMain {
public static void main(String[] args) {
// StickWaterFireGame game = new
StickWaterFireGame();
StickWaterFireGame game = new
StickWaterFireGame(1234);
Scanner scan = new
Scanner(System.in);
boolean keepGoing = true;
String playerChoice = "";
// Greet the user and state the
rules:
System.out.println("Welcome to
Stick-Water-Fire!\n");
System.out.println("Rules:");
System.out.println("\tYou will play
against the computer for the specified number of rounds.");
System.out.println("\tYou will make
a choice: 'S', 'W', or 'F' to represent 'Stick', 'Water', or
'Fire'.");
System.out.println("\tThe computer
will also make a choice, and the winner is chosen as
follows:");
System.out.println("\t\t Stick
beats Water (it floats on top)");
System.out.println("\t\t Water
beats Fire (extinguishes the fire)");
System.out.println("\t\t Fire beats
Stick (burns the stick)");
System.out.println("\tIn the event
of a tie, there is no winner.");
System.out.println("\tEach round,
the winner will have their score incremented.");
System.out.println("\tA report of
scores and number of rounds will be displayed at the end of each
round.");
System.out.println("\tEnter X to
quit.");
System.out.println("\tGood
luck!");
// begin the game loop.
while (keepGoing) {
System.out.println("Enter 'S' for Stick, 'W' for Water, 'F' for
Fire, or X to quit:");
playerChoice =
scan.nextLine();
if
(playerChoice.equalsIgnoreCase("X")) {
keepGoing = false;
}
else { // Handle
round of play
// validate input
if (!game.isValidInput(playerChoice)) {
System.out.println("\tInvalid
input entered: " + playerChoice + "\n");
}
else {
game.playRound(playerChoice);
String computerChoice =
game.getComputerChoice();
System.out.println("You chose
" + playerChoice + " and the computer chose " +
computerChoice);
// report winner
if (game.isTie()) {
System.out.println("You tied!");
} else if (game.playerWins())
{
System.out.println("You won! Nice job!\n");
} else { // Computer
won
System.out.println("You lost. Better luck next time!\n");
}
// Print report
System.out.println("Game
summary:");
System.out.println(getScoreReportStr(game));
System.out.println("
");
}
} // end of
round
} // end loop
System.out.println("Thanks for
playing!");
scan.close();
} // end main
public static String
getScoreReportStr(StickWaterFireGame game) {
return "Total plays: " +
game.getNumRounds() + "\nYour total score: " +
game.getPlayerScore()
+ ", computer total score: " +
game.getComputerScore();
}
}
output when you run above main class is:
please comment for any help!!