Design a simple rock-paper-scissors program. Your program will generate a random
ID: 441411 • Letter: D
Question
Design a simple rock-paper-scissors program.
Your program will generate a random choice of Rock, Paper or Scissors. It should then prompt the user for their choice. It should report both choices and indicate who wins the game. In addition, it should check the user's input and if the user inputs an invalid choice it should replace the user's choice with a valid default choice instead.
This is a sample transcript of what your program should do. Items inboldare user input and should not be put on the screen by your program.
Please select one of [R/P/S]:R
You chose: Rock
I chose: Paper
Paper beats rock - you lose!
Your code will behave differently based on the random value it selects and the choice taken by the user. Here is a second possible execution of this code:
Please select one of [R/P/S]: S
You chose: Scissors
I chose: Paper
Scissors beats paper - you win!
In the case of a tie, your code should report a tie:
Please select one of [R/P/S]:P
You chose: Paper
I chose: Paper
A Tie!
If the user enters an invalid choice, your code should default to a valid choice. This code defaults to Rock if the user doesn't enter a valid choice:
Please select one of [R/P/S]:q
Invalid choice! Defaulting to Rock.
I chose: Rock
A Tie!
For this assignment you should allow the user to enter either capital or lowercase letters to make their choice:
Please select one of [R/P/S]:r
You chose: Rock
I chose: Paper
Paper beats rock - you lose!
Explanation / Answer
Here is the code:
import java.util.Scanner;
public class RockPaperScissors {
/*
* Program allows user to play Rock, Paper and Scissors as many times as desired by entering Y until they enter N.
* Program will print amount of games played, amount lost, amount won and percentage won.
* User must enter "Y", "N", "Rock", "Paper" or "Scissors" with correct capitalization and spelling.
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int playerWins = 0;
int compWins = 0;
int gamesPlayed = 0;
while (true) {
System.out.println("Do you want to play Rock Paper Scissors (Y/N): ");
String play = input.nextLine();
// user terminates game and program prints number of wins, losses and percentage of wins.
if (play.equals("N")) {
System.out.println("You played a total of " + gamesPlayed + " matches against the computer");
System.out.println("The computer won " + compWins + " matches");
System.out.println("You won " + playerWins + " matches");
// 0% wins when no games are played.
if (gamesPlayed == 0) {
System.out.println("You won 0% of the time!");
break;
} else if (gamesPlayed > 0) {
double totalWins = (playerWins * 100.0) / gamesPlayed;
System.out.println("You won " + totalWins + "% of the time!");
break;
}
} else if ((!play.equals("N")) && (!play.equals("Y"))) {
System.out.println("Invalid entry");
} else {
System.out.println("Welcome to Rock, Paper and Scissors!");
System.out.print("Select "Paper", "Rock" or "Scissors": ");
String decision = input.nextLine();
System.out.println("Your selection: " + decision);
// random number generator producing integer values between 1 to 3 for computer's choices.
// 1 is for Rock, 2 is for Paper and 3 is for Scissors.
int num = (int)(Math.random() * (3-0) + 1);
switch (num) {
// Computer picks Rock
case 1:
if (decision.equals("Rock")) {
System.out.println("Tie, you and the computer selected rock");
gamesPlayed++;
} else if (decision.equals("Paper")) {
System.out.println("You win, paper beats rock!");
gamesPlayed++;
playerWins++;
} else if (decision.equals("Scissors")) {
System.out.println("Computer wins, rock beats scissors!");
gamesPlayed++;
compWins++;
} else {
System.out.println(decision + " is not a valid input");
}
break;
case 2:
// computer picks Paper
if (decision.equals("Rock")) {
System.out.println("Computer wins, rock beats paper!");
gamesPlayed++;
compWins++;
} else if (decision.equals("Paper")) {
System.out.println("Tie, you and the computer selected paper");
gamesPlayed++;
} else if (decision.equals("Scissors")) {
System.out.println("You win, scissors beats paper");
gamesPlayed++;
playerWins++;
} else {
System.out.println(decision + " is not a valid input");
}
break;
case 3:
// computer picks Scissors
if (decision.equals("Rock")) {
System.out.println("You win, rock beats scissors");
gamesPlayed++;
playerWins++;
} else if (decision.equals("Paper")) {
System.out.println("Computer wins, scissors beats paper");
gamesPlayed++;
compWins++;
} else if (decision.equals("Scissors")) {
System.out.println("Tie, you and the computer selected scissors");
gamesPlayed++;
} else {
System.out.println(decision + " is not a valid input");
}
break;
}
}
}
}
Cheers, mate!
Please rate 5 if it helped you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.