Question

In this problem, you’ll play a simple rock, paper, scissors game. First, you’ll ask the user...

In this problem, you’ll play a simple rock, paper, scissors
game. First, you’ll ask the user to pick rock, paper, or
scissors.

Then, you’ll have the computer randomly choose one of the
options.

After that, print out the winner! You should keep playing the game until the user hits enter.

Note: You’ll need to implement a method called
String getWinner(String user, String computer). Luckily,
you just wrote that in an earlier program!

Here is a sample run of the program.

Enter your choice (rock, paper, or scissors):rock
User: rock
Computer: paper
Computer wins!
Enter your choice (rock, paper, or scissors):paper
User: paper
Computer: scissors
Computer wins!
Enter your choice (rock, paper, or scissors):scissors
User: scissors
Computer: paper
User wins!
Enter your choice (rock, paper, or scissors):rock
User: rock
Computer: rock
Tie
Enter your choice (rock, paper, or scissors):
Thanks for playing!

public class RockPaperScissors extends ConsoleProgram
{
private static final String USER_PLAYER = "User wins!";
private static final String COMPUTER_PLAYER = "Computer wins!";
private static final String TIE = "Tie";
  
private String getWinner(String user, String computer)
{

}
  
public void run()
{
  
}
}

1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

Here is code:

ConsoleProgram.java:

import java.util.*;

public class ConsoleProgram {

private Scanner scanner;

public static void main(String[] args) {

// Assume the class name is passed in as the first argument.

if (args.length == 0) {

System.out.println("Please provide the name of the main class as an argument.");

return;

}

String mainClassName = args[0];

try {

Class mainClass = Class.forName(mainClassName);

Object obj = mainClass.newInstance();

ConsoleProgram program = (ConsoleProgram) obj;

program.run();

} catch (IllegalAccessException ex) {

System.out.println("Error in program. Make sure you extend ConsoleProgram");

} catch (InstantiationException ex) {

System.out.println("Error in program. Make sure you extend ConsoleProgram");

} catch (ClassNotFoundException ex) {

System.out.println("Error in program. Make sure you extend ConsoleProgram");

}

}

public void run() {

/* Overridden by subclass */

}

public ConsoleProgram() {

scanner = new Scanner(System.in);

}

public String readLine(String prompt) {

System.out.print(prompt);

return scanner.nextLine();

}

public double readDouble(String prompt) {

while (true) {

String input = readLine(prompt);

try {

double n = Double.valueOf(input).doubleValue();

return n;

} catch (NumberFormatException e) {

}

}

}

public int readInt(String prompt) {

while (true) {

String input = readLine(prompt);

try {

int n = Integer.parseInt(input);

return n;

} catch (NumberFormatException e) {

}

}

}

}

RockPaperScissors.java:

import java.util.Random;

public class RockPaperScissors extends ConsoleProgram {

private static final String USER_PLAYER = "User wins!";

private static final String COMPUTER_PLAYER = "Computer wins!";

private static final String TIE = "Tie";

private String getWinner(String user, String computer) {

String winner = "";

if (user.equalsIgnoreCase("scissors") && computer.equalsIgnoreCase("paper"))

winner = USER_PLAYER;

else if (user.equalsIgnoreCase("paper") && computer.equalsIgnoreCase("rock"))

winner = USER_PLAYER;

else if (user.equalsIgnoreCase("rock") && computer.equalsIgnoreCase("scissors"))

winner = USER_PLAYER;

else if (user.equalsIgnoreCase("rock") && computer.equalsIgnoreCase("rock"))

winner = TIE;

else if (user.equalsIgnoreCase("scissors") && computer.equalsIgnoreCase("scissors"))

winner = TIE;

else if (user.equalsIgnoreCase("paper") && computer.equalsIgnoreCase("paper"))

winner = TIE;

else

winner = COMPUTER_PLAYER;

return winner;

}

public void run() {

String choice, compChoice = "";

while (true) {

choice = readLine("Enter your choice (rock, paper, or scissors): ");

if (choice.length() == 0) {

break;

}

Random r = new Random();

int i = r.nextInt(3);

if (i == 0)

compChoice = "rock";

else if (i == 1)

compChoice = "paper";

else if (i == 2)

compChoice = "scissors";

System.out.println("User: " + choice);

System.out.println("Computer: " + compChoice);

System.out.println(getWinner(choice, compChoice));

}

System.out.println("Thanks for playing!");

}

}

Output:

Enter your choice (rock, paper, or scissors): rock User: rock Computer: paper Computer wins! Enter your choice (rock, paper,

Add a comment
Know the answer?
Add Answer to:
In this problem, you’ll play a simple rock, paper, scissors game. First, you’ll ask the user...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

  • C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this...

    C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this game against the computer. The program should work as follows: When the program begins, a random number between 1 and 3 is generated. If the number is 1, the computer has chosen rock. If the number is 2, the computer has chosen paper. If the number is 3, the computer has chosen scissors. Don't display the computer's choice yet. Use a menu to display...

  • java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of...

    java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of the common game Rock Paper Scissors that is often used to pass time (or sometimes to make decisions.) The rules of the game are outlined below: • • Scissors cuts Paper Paper covers Rock Rock crushes Lizard Lizard poisons Spock Spock smashes Scissors Scissors decapitates Lizard Lizard eats Paper Paper disproves Spock Spock vaporizes Rock Rock crushes Scissors Write a program that simulates the...

  • For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-...

    For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-Oriented Programming. You will be working with me on a team to build the program. I have already written my part of the program, and the Game.java file is attached. Your task will be to write a RockPaperScissors class that contains the following methods: getUserChoice: Has the user choose Rock, Paper, or Scissors. After validating the input, the method returns a String containing the user choice....

  • It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user...

    It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user selects 'p': 1. First the program should call a function named getComputerChoice to get the computer's choice in the game. The getComputerChoice function should generate a random number between 1 and 3. If the random number is 1 the computer has chosen Rock, if the random number is 2 the user has chosen Paper, and if the random number is 3 the computer has...

  • You are asked to implement scissors-rock-paper game where the players are a computer and a user....

    You are asked to implement scissors-rock-paper game where the players are a computer and a user. The player that wins 3 hands successively is the winner of the game. Your program should prompt the user with a message at the beginning of each hand to enter one of scissors, rock or paper. If the user's entry is not valid, i.e. entry is neither scissors, rock, nor paper, your program throws a message to the user to enter a valid entry...

  • In python language Write a program that lets the user play the game of Rock, Paper,...

    In python language Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 and 3 is generated. 1 = Computer has chosen Rock 2 = Computer has chosen Paper 3 = Computer has chosen Scissors (Dont display the computer's choice yet) 2. The user enters his or her choice of “Rock”, “Paper", “Scissors" at...

  • C++ You are asked to implement scissors-rock-paper game where the players are a computer and a...

    C++ You are asked to implement scissors-rock-paper game where the players are a computer and a user. The player that wins 3 hands successively is the winner of the game. Your program should prompt the user with a message at the beginning of each hand to enter one of scissors, rock or paper. If the user’s entry is not valid, i.e. entry is neither scissors, rock, nor paper, your program throws a message to the user to enter a valid...

  • Write a Python program (using python 3.7.2) that lets the user play the game of Rock,...

    Write a Python program (using python 3.7.2) that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. 1. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Don’t display the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT