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. You should use the Randomizer class that you created in the last exercise to help the computer make a choice. 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) that determines whether the user or computer won the game, and return the correct winner! Here is a sample run of the program. Enter your choice (rock, paper, or scissors): rock User: rock Computer: rock Tie Enter your choice (rock, paper, or scissors): paper User: paper Computer: rock User wins! Enter your choice (rock, paper, or scissors): Thanks for playing! import java.util.Scanner; public class RockPaperScissors { private static final String USER_PLAYER = “User wins!”; private static final String COMPUTER_PLAYER = “Computer wins!”; private static final String TIE = “Tie”; public static String getWinner(String user, String computer) { } public static void main(String[] args) { } } // Copy over your code from the Randomizer class exercise public class Randomizer { public static int nextInt() { // Implement this method to return a random number from 1-10 } public static int nextInt(int min, int max) { // Implement this method to return a random integer between min and max } }
The Correct Answer and Explanation is:
Here’s the complete implementation of the RockPaperScissors game using the provided structure, along with the Randomizer class:
java
import java.util.Scanner;
public class RockPaperScissors {
private static final String USER_PLAYER = "User wins!";
private static final String COMPUTER_PLAYER = "Computer wins!";
private static final String TIE = "Tie";
public static String getWinner(String user, String computer) {
if (user.equals(computer)) {
return TIE;
} else if ((user.equals("rock") && computer.equals("scissors")) ||
(user.equals("paper") && computer.equals("rock")) ||
(user.equals("scissors") && computer.equals("paper"))) {
return USER_PLAYER;
} else {
return COMPUTER_PLAYER;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter your choice (rock, paper, or scissors): ");
String userChoice = scanner.nextLine().toLowerCase();
if (userChoice.equals("")) {
System.out.println("Thanks for playing!");
break;
}
int computerNum = Randomizer.nextInt(1, 3);
String computerChoice = "";
if (computerNum == 1) {
computerChoice = "rock";
} else if (computerNum == 2) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
System.out.println("User: " + userChoice);
System.out.println("Computer: " + computerChoice);
System.out.println(getWinner(userChoice, computerChoice));
}
scanner.close();
}
}
class Randomizer {
public static int nextInt() {
return (int)(Math.random() * 10) + 1;
}
public static int nextInt(int min, int max) {
return (int)(Math.random() * (max - min + 1)) + min;
}
}
đź§ Explanation
This program simulates a rock-paper-scissors game between a user and the computer. The game continues in a loop until the user presses enter without typing a choice. The core logic is divided into two classes: RockPaperScissors for the game mechanics and Randomizer for generating random numbers.
The getWinner method determines the outcome of each round. It compares the user’s and computer’s choices. If both choices are the same, it returns “Tie”. If the user’s choice beats the computer’s according to the rules of the game, it returns “User wins!”. Otherwise, it returns “Computer wins!”. This method uses conditional logic to evaluate all possible outcomes.
In the main method, the program prompts the user for input. It reads the user’s choice and converts it to lowercase to ensure consistency. If the input is empty, the loop breaks and the game ends with a thank-you message.
The computer’s choice is generated using the Randomizer.nextInt(1, 3) method. This produces a random integer between 1 and 3, which is then mapped to “rock”, “paper”, or “scissors”. This ensures that the computer’s choice is unpredictable and fair.
The Randomizer class provides two methods. The first returns a random integer from 1 to 10. The second returns a random integer between any two specified bounds. These methods use Math.random() to generate pseudo-random values.
Overall, the program demonstrates basic control structures, user input handling, random number generation, and conditional logic. It is a simple yet effective example of interactive programming in Java.
