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

Write a function called RPSLS that allows the user to play a game of Rock, Paper

ID: 3765259 • Letter: W

Question

Write a function called RPSLS that allows the user to play a game of Rock, Paper, Scissors, Lizard, Spock against the computer. Use some of the functions that you've developed before. Break this function into at least three smaller functions.

Here is the process:

Let the user pick one of the options: Rock, Paper, Scissors, Lizard, Spock.

Have the computer make a choice.

Decide who wins, or if there is a tie.

Allow the user to keep playing until they decide to quit. Keep a score of how many times the player wins, how many times the computer wins, and how many times you tie.

Here are the rules:

If both choices are the same, there is a tie.

Rock crushes Scissors

Rock crushes Lizard

Paper covers Rock

Spock vaporizes Rock

Scissors cuts Paper

Scissors decapitates Lizard

Spock smashes Scissors

Lizard eats Paper

Lizard poisons Spock

Paper disproves Spock

Explanation / Answer

Hi,

Below is the solution that you are looking for:

import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class RockPaperScissors {
public enum Choices {
ROCK("rock") {
@Override
public List<Choices> getWinsAgainst() {
if (winsAgainst.isEmpty()) {
winsAgainst.add(SCISSORS);
winsAgainst.add(LIZARD);
}
return winsAgainst;
}
},
PAPER("paper") {
@Override
public List<Choices> getWinsAgainst() {
if (winsAgainst.isEmpty()) {
winsAgainst.add(ROCK);
winsAgainst.add(SPOCK);
}
return winsAgainst;
}
},
SCISSORS("scissors") {
@Override
public List<Choices> getWinsAgainst() {
if (winsAgainst.isEmpty()) {
winsAgainst.add(PAPER);
winsAgainst.add(LIZARD);
}
return winsAgainst;
}
},
LIZARD("lizard") {
@Override
public List<Choices> getWinsAgainst() {
if (winsAgainst.isEmpty()) {
winsAgainst.add(SPOCK);
winsAgainst.add(PAPER);
}
return winsAgainst;
}
},
SPOCK("spock") {
@Override
public List<Choices> getWinsAgainst() {
if (winsAgainst.isEmpty()) {
winsAgainst.add(ROCK);
winsAgainst.add(SCISSORS);
}
return winsAgainst;
}
};

private String keyword;

protected List<Choices> winsAgainst;

private Choices(String keyword) {
this.keyword = keyword;
this.winsAgainst = new ArrayList<>();
}

public String getKeyword() {
return keyword;
}

public abstract List<Choices> getWinsAgainst();
}

public void printUserOptions() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Input your choice for one of the following:");
for (Choices choice : Choices.values()) {
stringBuilder.append(" ");
stringBuilder.append(choice.getKeyword());
}
System.out.println(stringBuilder.toString());
}

public Choices getUserChoice() {
boolean isUserChoiceValid = false;
BufferedReader bufferedReader = null;
Choices userChoice = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
do {
String userChoiceString = bufferedReader.readLine();
isUserChoiceValid = validateUserChoice(userChoiceString);
if (!isUserChoiceValid) {
System.out.println("Please enter one of the valid options.");
} else {
userChoice = Choices.valueOf(userChoiceString.toUpperCase());
}
} while (!isUserChoiceValid);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("There was an error while reading from input.", e);
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return userChoice;
}


public boolean validateUserChoice(String userChoice) {
for (Choices choice : Choices.values()) {
if (choice.getKeyword().equals(userChoice)) {
return true;
}
}
return false;
}

public Choices getComputerChoice() {
return Choices.values()[new Random().nextInt(Choices.values().length)];
}

public void evaluateResult(Choices userChoice, Choices computerChoice) {
if (userChoice == computerChoice) {
System.out.println("It's a tie!");
return;
}

if (userChoice.getWinsAgainst().contains(computerChoice)) {
System.out.println("You won!");
} else {
System.out.println("The computer won.");
}
}

public void execute() {
printUserOptions();
Choices userChoice = getUserChoice();
Choices computerChoice = getComputerChoice();

System.out.println("");
System.out.println("You picked: " + userChoice.getKeyword());
System.out.println("Computer picked: " + computerChoice.getKeyword());
System.out.println("");
evaluateResult(userChoice, computerChoice);
}

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

Output:

Input your choice for one of the following: rock paper scissors lizard spock
scissors

You picked: scissors
Computer picked: spock

The computer won.

Hope that helps...HAPPY ANSWERING!!!!!!!!!!!!!!!

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