Assignments Assignment List Assignment Notes Unit Menu Previous Item Next Item R
ID: 3607412 • Letter: A
Question
Assignments
Assignment List
Assignment Notes
Unit Menu
Previous Item
Next Item
Redo Question
Communication
Send Message
View Messages
Miscellaneous
Appendix
Units of Measure
Interactive Java
Lectures
Java API
Programming Project: Rock, Paper, Scissors Game.
Rock beats ("crushes") Scissors.
Paper beats ("covers") Rock.
Scissors beats ("cuts") Paper.
Objectives:
Work with a multiple-class program.
Gain experience implementing "stub" code
Work with a seeded random number generator.
Write helper methods to support a somewhat complex operation.
Be able to write a class definition that works according to a specification.
Gain experience developing code in an incremental fashion.
Examples and Resources:
import java.util.Scanner;
import java.util.Random;
/* This class manages the playing of human vs. computer games. In this version, only the RPS game is
used (although imagine that other games could be implemented in the future).
The GameMain class handles all of the communication between the player and the game instance.
There are two possible instances of the Random class that can be passed to the game instance,
one seeded for debugging and one unseeded for random (normal) play.
The code below first prints a welcome message.
Then it creates a Scanner instance for console input as well as an instance of the RPS game (passing an
instance of Random to its constructor). It then enters a loop as long as the player wants to play a round.
When the player is done they enter an "X" and the loop stops. A good bye message is printed.
*/
public class GameMain {
public static void main(String[] args){
System.out.println("Welcome to Rock, Paper, Scissors. You will play against the computer ;-) ");
/* Create the Scanner, Random, and RPSGame instances.
You will also want variables to keep the player and computer choice
and any other variables needed.
*/
Scanner scan = new Scanner(System.in);
boolean keepPlaying = true;
String playerStr = "", computerStr = "";
// using a seed means the same sequence of numbers will be generated.
int seed = 123456;
Random rnd = new Random(seed);
// use this for truly random play:
//Random rnd = new Random();
RPSGame game = new RPSGame(rnd);
// The looping section
while(keepPlaying) {
System.out.println("Enter R for rock, P for paper, or S for scissors. Enter X to quit.");
playerStr = scan.nextLine();
System.out.println("You entered: "+playerStr);
if(playerStr.equalsIgnoreCase("X"))
keepPlaying = false;
else if (game.isValidInput(playerStr)){
// play a round
game.playRound(playerStr);
computerStr = game.getComputerChoice();
System.out.println("The computer chose: "+computerStr);
if(game.playerWins())
System.out.println("You win!");
else if(game.computerWins())
System.out.println("Computer wins!");
else
System.out.println("It's a tie!");
System.out.println(game.getScoreReportStr());
}
else // invalid input
System.out.println("Your input should be R, P, or S. Please enter again.");
}
System.out.println("Bye for now.");
}
}
import java.util.Random;
/* This class ecapsulates the state and logic required to play the
Rock, Paper, Scissors (R, P, S)game.
Rules of the game:
R beats S
S beats P
P beats R
no winner on a tie.
A member variable, rand, is an instance of the Random class. This instance is created
either with or without a seed value and passed in to the RPSGame constructor. The seeded
instance will allow for testing and debugging of the game as the same sequence of numbers
will be generated. The unseeded version will allow for normal play as the numbers are
randomly generated.
The state of the game is represented by the following member variables:
1, 2- The player and computer scores. These are initially 0 and are incremented by
the playRound method when either the player or computer wins a round.
3- The number of rounds played. This is initially 0 and is incremented for
each round of play (even in the case of a tie).
4- The computer's choice. This String is initially the empty string. It will be assigned to
one of the three choices: "R", "P", "S", during a round of play.
5, 6- playerWins and computerWins: these boolean variables are initially false. The playRound method will
assign one of them to true if one of them wins the round, otherwise they remain false.
The two private methods are called by the playRound method to help play a round.
*/
public class RPSGame {
Random rand;
int playerScore;
int computerScore;
int rounds;
String computerChoice;
boolean playerWins;
boolean computerWins;
/* The constructor assigns the member Random variable, rand, to
the instance passed in (either a seeded or non-seeded instance).
It also initializes the member variables to their default values:
rounds, player and computer scores are 0, the playerWins and computerWins
variables are set to false. The computer choice is initialized to the empty string.
*/
public RPSGame(Random r) {
}
/* This method returns true if the inputStr passed in is
either "R", "P", or "S", false otherwise.
*/
public boolean isValidInput(String inputStr) {
return false;
}
/* This method carries out a round of play of the RPS game. First, it calls resetRound so
that all variables from the last round are reset. Then, it gets the computer's choice
(by calling the getRandomChoice method), compares it to the player's choice and determines
who wins a round of play according to the rules of the game.
The player and computer scores are updated accordingly.
The computerWins and playerWins variables are updated accordingly.
The number of rounds of play is incremented.
*/
public void playRound(String playerChoice) {
}
// Returns the choice made by the computer for the current round of play.
public String getComputerChoice(){
return null;
}
// Returns true if the player has won the current round, false otherwise.
public boolean playerWins(){
return false;
}
// Returns true if the computer has won the current round, false otherwise.
public boolean computerWins() {
return false;
}
// Returns true if neither player nor computer has won the current round, false otherwise.
public boolean isTie(){
return false;
}
// Returns the number of rounds, the player's current score, and the computer's current score in a String format.
public String getScoreReportStr(){
return null;
}
/* This "helper" method uses the instance of Random to generate an integer
which it then maps to a String: "R", "P", "S", which is returned.
This method is called by the playRound method.
*/
private String getRandomChoice() {
return null;
}
/* Resets the variables that record state about each round:
computerChoice to the empty string and the playerWins and computerWins to false.
This method is called by the playRound method.
*/
private void resetRound(){
}
}
Assignments
Assignment List
Assignment Notes
Unit Menu
Previous Item
Next Item
Redo Question
Communication
Send Message
View Messages
Miscellaneous
Appendix
Units of Measure
Interactive Java
Lectures
Java API
QuestionStatus : 1 6:57 PM This question will be manually graded. You may edit and resubmit your response up until the assignment is due. Your score will be available after your submission has been graded.
Programming Project: Rock, Paper, Scissors Game.
In this project you will complete the implementation of a Java program that plays the game of Rock, Paper, Scissors. The human user of the program plays against the computer. The program uses a random number generator for the computer’s choices. The program decides on the winner of each round (or tie) and keeps track of the human and computer’s scores as well as the number of rounds played.
The game (https://en.wikipedia.org/wiki/Rock-paper-scissors) is played as follows:
There can be any number of rounds played.
For each round, each player chooses Rock or Paper or Scissors.
The two choices are compared and scored according to these rules:
Rock beats ("crushes") Scissors.
Paper beats ("covers") Rock.
Scissors beats ("cuts") Paper.
When two players make the same choice it is a tie. Another round may be played.
In our version, the winner of a round is awarded one point, the looser zero points. Ties result in no points awarded to either player. The total scores of the human and computer as well as the number of rounds played is recorded.
Objectives:
Work with a multiple-class program.
Gain experience implementing "stub" code
Work with a seeded random number generator.
Write helper methods to support a somewhat complex operation.
Be able to write a class definition that works according to a specification.
Gain experience developing code in an incremental fashion.
Examples and Resources:
The following is an example of the player interaction with a completed game:
You are given two Java source files: GameMain.java and RPSGame.java.
RPSGame starter code
The GameMain.java file is given to you. Do not modify it. You will work in and submit the RPSGame.java file only.
The RPSGame.java file contains "stub" or "skeleton" code. This code contains the "bare bones" of the program; the variable declarations and method headers with the minimum body code so that the class compiles. Your job is to implement each method (including the constructor) in the RPSGame.java file so that the program works as specified.
import java.util.Scanner;
import java.util.Random;
/* This class manages the playing of human vs. computer games. In this version, only the RPS game is
used (although imagine that other games could be implemented in the future).
The GameMain class handles all of the communication between the player and the game instance.
There are two possible instances of the Random class that can be passed to the game instance,
one seeded for debugging and one unseeded for random (normal) play.
The code below first prints a welcome message.
Then it creates a Scanner instance for console input as well as an instance of the RPS game (passing an
instance of Random to its constructor). It then enters a loop as long as the player wants to play a round.
When the player is done they enter an "X" and the loop stops. A good bye message is printed.
*/
public class GameMain {
public static void main(String[] args){
System.out.println("Welcome to Rock, Paper, Scissors. You will play against the computer ;-) ");
/* Create the Scanner, Random, and RPSGame instances.
You will also want variables to keep the player and computer choice
and any other variables needed.
*/
Scanner scan = new Scanner(System.in);
boolean keepPlaying = true;
String playerStr = "", computerStr = "";
// using a seed means the same sequence of numbers will be generated.
int seed = 123456;
Random rnd = new Random(seed);
// use this for truly random play:
//Random rnd = new Random();
RPSGame game = new RPSGame(rnd);
// The looping section
while(keepPlaying) {
System.out.println("Enter R for rock, P for paper, or S for scissors. Enter X to quit.");
playerStr = scan.nextLine();
System.out.println("You entered: "+playerStr);
if(playerStr.equalsIgnoreCase("X"))
keepPlaying = false;
else if (game.isValidInput(playerStr)){
// play a round
game.playRound(playerStr);
computerStr = game.getComputerChoice();
System.out.println("The computer chose: "+computerStr);
if(game.playerWins())
System.out.println("You win!");
else if(game.computerWins())
System.out.println("Computer wins!");
else
System.out.println("It's a tie!");
System.out.println(game.getScoreReportStr());
}
else // invalid input
System.out.println("Your input should be R, P, or S. Please enter again.");
}
System.out.println("Bye for now.");
}
}
import java.util.Random;
/* This class ecapsulates the state and logic required to play the
Rock, Paper, Scissors (R, P, S)game.
Rules of the game:
R beats S
S beats P
P beats R
no winner on a tie.
A member variable, rand, is an instance of the Random class. This instance is created
either with or without a seed value and passed in to the RPSGame constructor. The seeded
instance will allow for testing and debugging of the game as the same sequence of numbers
will be generated. The unseeded version will allow for normal play as the numbers are
randomly generated.
The state of the game is represented by the following member variables:
1, 2- The player and computer scores. These are initially 0 and are incremented by
the playRound method when either the player or computer wins a round.
3- The number of rounds played. This is initially 0 and is incremented for
each round of play (even in the case of a tie).
4- The computer's choice. This String is initially the empty string. It will be assigned to
one of the three choices: "R", "P", "S", during a round of play.
5, 6- playerWins and computerWins: these boolean variables are initially false. The playRound method will
assign one of them to true if one of them wins the round, otherwise they remain false.
The two private methods are called by the playRound method to help play a round.
*/
public class RPSGame {
Random rand;
int playerScore;
int computerScore;
int rounds;
String computerChoice;
boolean playerWins;
boolean computerWins;
/* The constructor assigns the member Random variable, rand, to
the instance passed in (either a seeded or non-seeded instance).
It also initializes the member variables to their default values:
rounds, player and computer scores are 0, the playerWins and computerWins
variables are set to false. The computer choice is initialized to the empty string.
*/
public RPSGame(Random r) {
}
/* This method returns true if the inputStr passed in is
either "R", "P", or "S", false otherwise.
*/
public boolean isValidInput(String inputStr) {
return false;
}
/* This method carries out a round of play of the RPS game. First, it calls resetRound so
that all variables from the last round are reset. Then, it gets the computer's choice
(by calling the getRandomChoice method), compares it to the player's choice and determines
who wins a round of play according to the rules of the game.
The player and computer scores are updated accordingly.
The computerWins and playerWins variables are updated accordingly.
The number of rounds of play is incremented.
*/
public void playRound(String playerChoice) {
}
// Returns the choice made by the computer for the current round of play.
public String getComputerChoice(){
return null;
}
// Returns true if the player has won the current round, false otherwise.
public boolean playerWins(){
return false;
}
// Returns true if the computer has won the current round, false otherwise.
public boolean computerWins() {
return false;
}
// Returns true if neither player nor computer has won the current round, false otherwise.
public boolean isTie(){
return false;
}
// Returns the number of rounds, the player's current score, and the computer's current score in a String format.
public String getScoreReportStr(){
return null;
}
/* This "helper" method uses the instance of Random to generate an integer
which it then maps to a String: "R", "P", "S", which is returned.
This method is called by the playRound method.
*/
private String getRandomChoice() {
return null;
}
/* Resets the variables that record state about each round:
computerChoice to the empty string and the playerWins and computerWins to false.
This method is called by the playRound method.
*/
private void resetRound(){
}
}
Explanation / Answer
Hi,
Here is the complete java class for the rock paper scissor game!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.