QuestionQuestion

The Game Rock, Paper, Scissors

Overview
Write a program that will simulate the hand gesture game “Rock, Paper, Scissors” for 2 players.

Background: Game Rules
The rules to play the game are pretty simple.
The game is played where players deliver hand signals that will represent the elements of the game; rock, paper and scissors. The outcome of the game is determined by 3 simple rules:
- Rock wins against scissors
- Scissors win against paper
- Paper wins against rock
The Hand Signals
In order to ensure fair play, the game signals have been agreed upon to ensure the uniformity of delivery every single time. This ensures that there will be no room for error and these signals have been approved for all types of recreational and professional play.
The proper opening move will impact the overall success of the game. This means that special attention should be given to the way you move your hand initially before you attempt to make any signal so that other players are not aware of your intention. When you succeed to do so, you ensure that other players will not be able to make a signal that beats yours, at least not on purpose.
The Rock
The rock is recognized by a closed fist where the thumb is not concealed.
The Paper
Paper is delivered in the same way as the rock except that in this case all the fingers and the thumb are extended in a way that they all face the same direction.
The Scissors
Scissors are thrown in the same way as rock where the hand is clenched into a fist but the index and the middle finger are extended to the front in order to make an angle that is of 30 to 45 degrees in a way that would resemble a pair of scissors.


Execution and User Input
This program will interact with a single user; please review the sample input / output sessions for details; we describe them again here emphasizing input.
Game play will consist of 1 or more matches composed of 1 or more “best of” rounds. Each round is composed of “hands”, individual instances of hand gesture comparisons. The user will initially be prompted for a number (1-5) for a “best of” round.
The program will prompt for the target number of a best of round with a minimum of one and a maximum of 5.
Once a valid best of score has been entered the game will proceed to simulate two players randomly choosing one of the 3 possible options (described above), comparing the results of the two choices then displaying the result of the “hand”. This will be repeated until one of the players totals a number of wins equal to the chosen best of score. This completes a round.
The player will have the option of continuing or ending the current match with ‘y’ for Yes.
The match will continue until the user chooses not to go on with the current match. At this point the user will be asked if they would like to begin a new match. Choosing not to begin a new match terminates the program.
At the conclusion of each round the total match score for each player will be displayed as well as current match totals of rounds played and number of rounds won by each player. A match may not conclude if the two players each have the same number of rounds won. In other words, matches may not end in a tie.
When a match is concluded the user will be prompted to begin a new match with: ‘y’ for Yes.

Sample Input / Output Sessions

Session 1: Valid Input of Best Of value
Welcome to Rock, Paper, Scissors...
Play round to? (max 5): 6
Play round to? (max 5): 2



Session 2: Complete one round in one match.
Welcome to Rock, Paper, Scissors...
Play round to? (max 5): 4
P1: P - P2: S
P1: P - P2: R
P1: P - P2: S
P1: P - P2: P - Tie
P1: P - P2: R
P1: S - P2: R
P1: S - P2: P
P1: P - P2: P - Tie
P1: R - P2: P
-----
Player 2 won: 4 to 3
-----
After 1 rounds:
Player 1: 0 - Player 2: 1
Continue this match? (Y/N) n
**********
Match 1 complete:
-----
After 1 rounds:
Player 1: 0 - Player 2: 1
Would you like to play another match? (Y/N) n
---------------
Total matches: 1
Total rounds: 1
Player 1 matches won: 0
Player 2 matches won: 1
Player 1 hands won: 3
Player 2 hands won: 4
Goodbye...


Session 3: Multiple rounds, one match. Match cannot end in tie.
Welcome to Rock, Paper, Scissors...
Play round to? (max 5): 2
P1: P - P2: S
P1: S - P2: P
P1: S - P2: P
-----
Player 1 won: 2 to 1
-----
After 1 rounds:
Player 1: 1 - Player 2: 0
Continue this match? (Y/N) y
Play round to? (max 5): 2
P1: R - P2: P
P1: P - P2: R
P1: P - P2: P - Tie
P1: P - P2: P - Tie
P1: R - P2: R - Tie
P1: P - P2: R
-----
Player 1 won: 2 to 1
-----
After 2 rounds:
Player 1: 2 - Player 2: 0
Continue this match? (Y/N) y
Play round to? (max 5): 2
P1: P - P2: R
P1: R - P2: S
-----
Player 1 won: 2 to 0
-----
After 3 rounds:
Player 1: 3 - Player 2: 0
Continue this match? (Y/N) n
**********
Match 1 complete:
-----
After 3 rounds:
Player 1: 3 - Player 2: 0
Would you like to play another match? (Y/N) n
---------------
Total matches: 1
Total rounds: 3
Player 1 matches won: 3
Player 2 matches won: 0
Player 1 hands won: 6
Player 2 hands won: 2
Goodbye...

Session 4: Multiple matches.
Welcome to Rock, Paper, Scissors...
Play round to? (max 5): 2
P1: S - P2: S - Tie
P1: R - P2: R - Tie
P1: R - P2: P
P1: S - P2: R
-----
Player 2 won: 2 to 0
-----
After 1 rounds:
Player 1: 0 - Player 2: 1
Continue this match? (Y/N) n
**********
Match 1 complete:
-----
After 1 rounds:
Player 1: 0 - Player 2: 1
Would you like to play another match? (Y/N) y
Play round to? (max 5): 3
P1: R - P2: S
P1: P - P2: S
P1: R - P2: R - Tie
P1: S - P2: S - Tie
P1: P - P2: R
P1: R - P2: P
P1: P - P2: S
-----
Player 2 won: 3 to 2
-----
After 1 rounds:
Player 1: 0 - Player 2: 1
Continue this match? (Y/N) n
**********
Match 2 complete:
-----
After 1 rounds:
Player 1: 0 - Player 2: 1
Would you like to play another match? (Y/N) n
---------------
Total matches: 2
Total rounds: 2
Player 1 matches won: 0
Player 2 matches won: 2
Player 1 hands won: 2
Player 2 hands won: 5
Goodbye...

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.*;

public class RockPaperScissors
{
private final char _YES = 'Y';
private final int HIGH_BESTOF = 5;
//constant used for Random function Random.nextInt(n)
//returns random integer in range 0 - (n-1)
private final int MAX_RANDOM = 3;

//total Match counter
private int matchCounter = 0;
//round counter within single match
private int roundCounter = 0;
//rounds won by player1 in a single match
private int p1Rounds = 0;
//rounds won by player2 in a single match
private int p2Rounds = 0;
//total rounds played, matches won by each player
//and hands won by each player
private int totalRounds = 0;
private int player1MatchesWon = 0;
private int player2MatchesWon = 0;
private int totalP1Hands = 0;
private int totalP2Hands = 0;

//input from keyboard will be used for every input
private Scanner keyboard = new Scanner(System.in);

//display match summary
private void displayMatchSummary(){
printDashedLine();
System.out.println("After " + roundCounter + " rounds:");
System.out.println("Player 1: " + p1Rounds + " - Player 2: " + p2Rounds);

}

// display round results
private void displayRoundResult(int p1Score, int p2Score)
{
printDashedLine();
if (p1Score > p2Score){
System.out.println("Player 1 won: " + p1Score + " to " + p2Score);
}
if (p2Score > p1Score){
System.out.println("Player 2 won: " + p2Score + " to " + p1Score);
}
displayMatchSummary();
}

// display match results
private void displayMatchResult(int round, int p1Score, int p2Score)
{
printStarsLine();
System.out.println("Match " + matchCounter + " complete.");
printDashedLine();
System.out.println("After " + roundCounter + " rounds:");
System.out.println("Player 1: " + p1Rounds + " - Player 2: " + p2Rounds);
}

//print line (needed for formating only)
private void printDashedLine(){
System.out.println("-----------");
}

//print stars (needed for formating only)
private void printStarsLine(){
System.out.println("***********");
}...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$21.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats