Problem: Write a program that lets the user to play the game of Rock, paper, Sci
ID: 3678810 • Letter: P
Question
Problem: Write a program that lets the user to play the game of Rock, paper, Scissors against the computer many times. Creating the Scanner object must be in the main method only. The program should work as follows: main method must only have two lines of code: Scanner kb = new Scanner(System.in) playGame(kb); lavGame method a. Ask the user how many times he/she wants to play the game. b. Call ComputerChoice method c. Call userChoice d. Call winner method e. Display the result computerChoice method Generate a random number in the range of one and three is generated. You must write a method that returns the computer choice as a string If the number is one, then the computer has chosen rock If the number is 2, the computer has chosen paper. If the number is 3, then the computer has chosen scissors. userChoice method The user enters her/his choice of "rock", "paper", "scissors" at the keyboard. You must use a menu so the user can select one of the options. Must write a method that displays the menu and then returns the user choice Winner method A winner is selected according to the following criteria. If two players make the same choice, the game must be played again. Must write a method that accepts the user's choice and the computer's choice and return the winner. Player 1 rock scissors paper Player 2 scissors paper rock winner Rock smashes the scissors Scissors cuts paper Paper wraps rockExplanation / Answer
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissorsGame
{
public static void main(String[] args)
{
String user, comp;
boolean again;
do {
comp = computerChoice();
user = userChoice();
System.out.printf("The computer's choice was %s. ", comp);
System.out.printf("The user's choice was %s. ", user);
determineWinner(comp, user);
if(comp.equalsIgnoreCase(user))
again = true;
else
again = playAgain();
} while(again);
}
public static String computerChoice()
{
Random rand = new Random();
int result = rand.nextInt(3) + 1;
if(result == 1)
return "rock";
else if(result == 2)
return "paper";
else
return "scissors";
}
public static String userChoice()
{
Scanner kb = new Scanner(System.in);
String input;
System.out.print("Enter rock, paper, or scissors: ");
input = kb.nextLine();
while(!isValidChoice(input))
{
System.out.print("Invalid input, enter rock, paper, or scissors: ");
input = kb.nextLine();
}
return input;
}
public static boolean isValidChoice(String choice)
{
if(choice.equalsIgnoreCase("rock") || choice.equalsIgnoreCase("paper")
|| choice.equalsIgnoreCase("scissors"))
return true;
else
return false;
}
public static void determineWinner(String computer, String user)
{
switch(user.charAt(0))
{
case 'r': case 'R':
switch(computer.charAt(0))
{
case 'r': case 'R':
System.out.println("The game is tied! " +
"Get ready to play again... ");
break;
case 's': case 'S':
System.out.println("Rock smashes scissors. " +
"The user wins! ");
break;
case 'p': case 'P':
System.out.println("Paper wraps rock. " +
"The computer wins! ");
break;
}
break;
case 'p': case 'P':
switch(computer.charAt(0))
{
case 'r': case 'R':
System.out.println("Paper wraps rock. " +
"The user wins! ");
break;
case 's': case 'S':
System.out.println("Scissors cut paper. " +
"The computer wins! ");
break;
case 'p': case 'P':
System.out.println("The game is tied! " +
"Get ready to play again... ");
break;
}
break;
case 's': case 'S':
switch(computer.charAt(0))
{
case 'r': case 'R':
System.out.println("Rock smashes scissors. " +
"The computer wins! ");
break;
case 's': case 'S':
System.out.println("The game is tied! " +
"Get ready to play again... ");
break;
case 'p': case 'P':
System.out.println("Scissors cut paper. " +
"The user wins! ");
break;
}
break;
}
}
public static boolean playAgain()
{
Scanner kb = new Scanner(System.in);
String result;
do{
System.out.print("Play again? (y/n) ");
result = kb.nextLine();
if(result.equalsIgnoreCase("y") || result.equalsIgnoreCase("yes"))
return true;
else if(result.equalsIgnoreCase("n") ||
result.equalsIgnoreCase("no"))
return false;
else
System.out.println("Invalid entry...");
}while(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.