Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Rock Paper Scissors using Java(Please use Scanner,Random, if,while or for loop)

ID: 3689846 • Letter: R

Question

Rock Paper Scissors using Java(Please use Scanner,Random, if,while or for loop)

Write a Java program that plays the child's game rock/paper/scissors. The general rules of
the game are as follows. Two players simultaneously pick 'rock', 'paper' or 'scissors'. If both
players choose the same thing, it is a tie. Choosing 'rock' wins to 'scissors' but loses to
'paper'. Choosing 'paper' wins to 'rock' but loses to 'scissors'. Choosing 'scissors' wins to
'paper' but loses to 'rock'.
A 'computer' opponent will generate random choices of 'rock', 'paper' or 'scissors' by picking
numbers between 1 and 3. You can either have the user type in a number for their choice or
you can present them with a menu to choose from. Then determine who won and print a
message. Allow the user to play the computer as many times as they want by asking if they
want to continue. Keep track of wins, losses and ties for both the user and the computer and
print out statistics at the end of the program (see example output for what statistics to show).
The overall winner is the player with the highest number of wins. If both players have the
same number of wins, then print that there is no overall winner.
WHAT YOU'RE PROGRAM SHOULD DO:
1. Ask the user for a name.
2. Generate a move for the computer using random numbers.
3. Ask the user for a choice. Check to make sure they enter a valid response.
4. Determine the winner. Keep track of both the user's and computer's wins, losses and ties.
5. Ask the user if they want to continue. if not, print out statistics and exit program.
REQUIREMENTS:
Your program should have at least one loop (while, do/while or for loop). You should
always check for invalid input (the user's choice and the asking for whether they want to
continue). Keep reasking
until you get a valid response.
Your code should have good indention and spacing and not be repetitive (no large sections
of code copied elsewhere in your code). The while loops should have a conditional that
actually stops the loop (don't use break in your while loops; let the conditional go to false to
get out of the loop).

Explanation / Answer

import java.util.Random;
import java.util.Scanner;

public class RockPaperScissors {
private User user;
private Computer computer;
private int userScore;
private int computerScore;
private int numberOfGames;

private enum Move {
ROCK, PAPER, SCISSORS;

/**
* Compares this move with another move to determining a tie, a win, or
* a loss.
*
* @param otherMove
* move to compare to
* @return 1 if this move beats the other move, -1 if this move loses to
* the other move, 0 if these moves tie
*/
public int compareMoves(Move otherMove) {
// Tie
if (this == otherMove)
return 0;

switch (this) {
case ROCK:
return (otherMove == SCISSORS ? 1 : -1);
case PAPER:
return (otherMove == ROCK ? 1 : -1);
case SCISSORS:
return (otherMove == PAPER ? 1 : -1);
}

// Should never reach here
return 0;
}
}

private class User {
private Scanner inputScanner;

public User() {
inputScanner = new Scanner(System.in);
}

public Move getMove() {
// Prompt the user
System.out.print("Rock, paper, or scissors? ");

// Get the user input
String userInput = inputScanner.nextLine();
userInput = userInput.toUpperCase();
char firstLetter = userInput.charAt(0);
if (firstLetter == 'R' || firstLetter == 'P' || firstLetter == 'S') {
// User has entered a valid input
switch (firstLetter) {
case 'R':
return Move.ROCK;
case 'P':
return Move.PAPER;
case 'S':
return Move.SCISSORS;
}
}

// User has not entered a valid input. Prompt again.
return getMove();
}

public boolean playAgain() {
System.out.print("Do you want to play again? ");
String userInput = inputScanner.nextLine();
userInput = userInput.toUpperCase();
return userInput.charAt(0) == 'Y';
}
}

private class Computer {
public Move getMove() {
Move[] moves = Move.values();
Random random = new Random();
int index = random.nextInt(moves.length);
return moves[index];
}
}

public RockPaperScissors() {
user = new User();
computer = new Computer();
userScore = 0;
computerScore = 0;
numberOfGames = 0;
}

public void startGame() {
System.out.println("ROCK, PAPER, SCISSORS!");

// Get moves
Move userMove = user.getMove();
Move computerMove = computer.getMove();
System.out.println(" You played " + userMove + ".");
System.out.println("Computer played " + computerMove + ". ");

// Compare moves and determine winner
int compareMoves = userMove.compareMoves(computerMove);
switch (compareMoves) {
case 0: // Tie
System.out.println("Tie!");
break;
case 1: // User wins
System.out.println(userMove + " beats " + computerMove + ". You won!");
userScore++;
break;
case -1: // Computer wins
System.out.println(computerMove + " beats " + userMove + ". You lost.");
computerScore++;
break;
}
numberOfGames++;

// Ask the user to play again
if (user.playAgain()) {
System.out.println();
startGame();
} else {
printGameStats();
}
}

/**
* Prints out the statistics of the game. Calculates ties as 1/2 a win in
* percentage won.
*/
private void printGameStats() {
int wins = userScore;
int losses = computerScore;
int ties = numberOfGames - userScore - computerScore;
double percentageWon = (wins + ((double) ties) / 2) / numberOfGames;

// Line
System.out.print("+");
printDashes(68);
System.out.println("+");

// Print titles
System.out.printf("| %6s | %6s | %6s | %12s | %14s | ",
"WINS", "LOSSES", "TIES", "GAMES PLAYED", "PERCENTAGE WON");

// Line
System.out.print("|");
printDashes(10);
System.out.print("+");
printDashes(10);
System.out.print("+");
printDashes(10);
System.out.print("+");
printDashes(16);
System.out.print("+");
printDashes(18);
System.out.println("|");

// Print values
System.out.printf("| %6d | %6d | %6d | %12d | %13.2f%% | ",
wins, losses, ties, numberOfGames, percentageWon * 100);

// Line
System.out.print("+");
printDashes(68);
System.out.println("+");
}

private void printDashes(int numberOfDashes) {
for (int i = 0; i < numberOfDashes; i++) {
System.out.print("-");
}
}

public static void main(String[] args) {
RockPaperScissors game = new RockPaperScissors();
game.startGame();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote