You are on page 1of 3

Simple Java rock paper scissors program

Assignment :
Execute a Java program that plays a different version of Rock, Paper, and Scissors game. A more complex variant of
this well-known game will be what we do. The following are the specific rules for the game.
Rules of the game:
The game is made up of 2 players, one human player and a computer player. The players exchange turns, beginning
with the player who is human. During each turn, each player takes a sequence of "shoots". When a player "shoots" a
gesture, they automatically selects a movement that reflects one of the following:
FIRE melts SCISSORS, burns PAPER & SPONGE.
SPONGE soaks PAPER, uses AIR POCKETS, absorbs WATER.
SCISSORS splits through AIR, slices PAPER & SPONGE.
PAPER blows AIR away, covers ROCK, floats on WATER.
AIR blows out FIRE, erodes ROCK, evaporates WATER.
WATER erodes ROCK, smothers out FIRE, rusts SCISSORS.
ROCK puts out FIRE, smashes SCISSORS & SPONGE.
Points System:
1. With any winning "shoot" the player controlling the current turn gets one (1) point.
2. When a shot is missed by the player running the current turn then the control turns over to the opposing
player.
3. If both players "shoot" the same gesture, then the player controlling the current turn loses all the accrued
points during the turn (but not during the game) and hands control over to the opponent.
Write the methods in every of the attached java files, as specified. If you think it is appropriate, you may connect to
certain classes, but aside from including finals here and there, you should not apply instance data or public methods
to either of these groups. RPS7Game.java would need to be combined with private helper methods.
Specifications:
1. The game has to start with a greeting along with a quick description of the game.
2. The human player has to be named Human and the computer player must be named "Computer".
3. The first person to shoot has to be the human player.
4. The "shoot" gesture must be randomly selected by either the person or the computer player (do not ask the
user for a value).
5. A turn for both the human player and the computer player consist of "shots" (as mentioned above). When
both players shoot the same gesture, the turn will stop, and the current player will forfeit all points for the turn.

1. A blank line will be written at the beginning of each player's turn (both the person and the computer)
accompanied by a line containing the player's name and score. The line should be in the following form (but with the
appropriate name and score filled in).
What should be printed:
Print: Player: Human ( Total Points: # )
 A line equivalent to the one above will be printed at the conclusion of a player's turn.
Print: Take the shot ( Yes/No )?:
 The human should be prompted with this string before each human "shot" (preceded by a TAB character
and ending with a space, but not concluded by a newline).
 The program will check that the user has entered either "Yes" or "No" (upper or lower case). For example,
when the user enters "No" (or "no") the turn will stop. The turn will continue if the user enters "Yes" (or "yes") (Ignore
the white spaces).
 The program should not prompt after each "shot" that the computer player does. The computer player's turn
ends after it has accumulated 4 or more points during that turn.
Print: ROCK beats FIRE (This Turn: 2, Total: 5)
 The program will produce a line like this after a winning "shot" (for both players). This line should be
preceded by a TAB and ended by a "\n".
 (In the above case, during the current turn, the human has won a total of 2 points, including the current
"shoot" and 3 points in previous turns.)
Print: ROCK beaten by PAPER: Forfeit turn (This Turn: 2, Total: 5)
 The program will display a line like this after each losing "shot" (for both players) This line should be
preceded by a TAB and ended by a "\n".
 (In the above case, during the current turn, the human has won a total of 2 points, during prior shots during
this turn, and 3 points in previous turns.)
Print: SPONGE = SPONGE: Forfeit turn and points (This Turn: 0, Total: 3)
 If both players "shoot" the same gesture, a line like this will be generated by the program. This line should
be preceded by a TAB and ended by a "\n".
 (In the above case, during the current turn, the human has lost a total of 2 points, previously earned during
this turn, but keeps the 3 points from previous turns.)
Print: The winner is: Computer
 When a player wins, a line like this should be printed. This line should be preceded by a TAB and ended by
a "\n".

Rock, Paper, Scissors is a hand game played by two people. Both people would say "rock, paper, scissors" and then
simultaneously form one of three objects (rock, paper, or scissors) with an outstretched hand. The winner is
determined by the hand formations. Scissors beats paper, paper beats rock, and rock beats scissors. If both players
play the same hand formation, it is considered a tie. We will write a simple game in Java to simulates Rock, Paper,
Scissors where one player is the user and the other player is the computer.

Create the main class and call it RockPaperScissors. This will be the class where we will write the game. You
may choose to name it something else such as Game or Main. Write in method declarations for the constructor and
the main method.

Create a startGame() method in the RockPaperScissors class. This method will be the playing of the game.


Start out by putting a simple System.out.println in the method.

2
Create an enumeration for the hand gestures (rock, paper, or scissors). We could use strings to represent rock, paper,
or scissors, but an enumeration allows us to predefine our constants which means that using the enumeration is a
better design. We will call our enum type Move with the values ROCK, PAPER, and SCISSORS.

Create two private classes User and Computer. These classes will represent our players in the game. You may
choose to make these classes public. The User class will be the class that prompts the user for either rock, paper, or
scissors, so we will need to write a getMove() method. The Computer class will also need to have
a getMove() method so that the computer can also make a move. We will put placeholders in these methods and
implement them later. The User class will require a constructor that sets up the Scanner object to take in the user
input. We will put the Scanner as a private field for the user and then initiate it in the constructor. Since we are using
the Scanner class, we need to write an import statement for it at the top of our code. The Computer class does not
require a constructor, so we do not need to write one; when we initiate the Computer object, we will just be calling
the default constructor. Here is what our RockPaperScissors class looks like now:

Write the getMove() method for the Computer class. This method will return a random Move. We can get an
array of Move enumerations by calling the values() method: Move.values(). To choose a
random Move enumeration in this values array, we need to generate a random index that is an integer between 0 and
the length of our values array. To do this, we can use the nextInt() method of the Random class which we need to
import from java.util. After we have gotten the random index, we can return the Move of that index from our
values array.

Write the getMove() method for the User class. This method will return a Move corresponding to what the user
has input. We will expect the user to write either "rock", "paper", or "scissors". First, we need to prompt the user for an
input: System.out.print("Rock, paper, or scissors? "). Then use the nextLine() method of
the Scanner object to get the user input as a string. We need now need to check if the user has submitted a valid
move, but we can be lenient if the user has misspelled a word. So we will only check if the first letter of the user input
is either "R" (for rock), "P" (for paper), or "S" (for scissors), and we won't care about the case because we will first use
the toUpperCase() method of the String class to make the user input string all uppercase. If the user has not
entered a remotely correct input, we will prompt the user again. Then, depending on what the user has put in, we will
return a corresponding move.

Connect the User and Computer classes together in the RockPaperScissors class. Now that we have


finished writing the User and Computer classes, we can focus on working on our actual game. Create private fields
for the User and Computer classes in the RockPaperScissors class. We will need to access these fields to
access the getMove() methods when we're playing the game. In the constructor for
the RockPaperScissors class, initiate these fields. We will also need to keep track of the score
in userScore and computerScore fields, which we need to initiate as 0 in the constructor. We need to keep track
of the number of games as well, which will also be a field initiated as 0.

You might also like