Question

Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of the common game Rock Paper Scissors that
• The program then determines according to the rules above who wins the game. o Use nested if else statements to determine th
Here is a sample of the solution (with extra credit parts included): Lets play Rock, Paper, Scissors, Lizard, Spock! Enter y
Lets play Rock, Paper, Scissors, Lizard, Spock! Enter your choice: Paper Computer chooses Lizard Lizard eats paper. Computer
momo Computer chooses Paper Lizard eats Paper. You win!! Play again? Enter yes or no: yes Lets play Rock, Paper, Scissors, L

java pls
0 0

> How would you write a Junit 5 test to check uppercase, funky case, same plays, all options, bad input and determine who the winner is method.

John Ord Mon, Oct 11, 2021 12:40 PM

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



Executable Code :


// RockPaperScissorsSpockLizard.java

import java.util.Random;
import java.util.Scanner;

public class RockPaperScissorsSpockLizard {
   public static void main(String[] args) {
       // Declaring variables
       String userChoice, computerChoice;
       String winner = "";
       int computerWinsCnt = 0, playerWinsCnt = 0, tie = 0;
       // Scanner class object is used to read the inputs entered by the user
       Scanner sc = new Scanner(System.in);

       /*
       * This while loop continues to execute until the user enters a valid
       * string
       */
       while (true) {
           System.out
                   .println("Let's play Rock, Paper, Scissors, Lizard, Spock!");
           // Getting the input entered by the user
           System.out.print("Enter your Choice :");
           while(true) {
      
               userChoice = sc.next();
               if((!userChoice.equalsIgnoreCase("rock")
                       && !userChoice.equalsIgnoreCase("paper")
                       && !userChoice.equalsIgnoreCase("scissors")
                       && !userChoice.equalsIgnoreCase("spock")
                       && !userChoice.equalsIgnoreCase("lizard")))
               {
                   System.out.println("Illegal choice: "+userChoice);
                   // Getting the input entered by the user
                   System.out.print("Re-enter your play: ");              
               }
               else
                   break;
           }

           // Calling the computer Selection method
           computerChoice = ComputerSelection();

           if (userChoice.equalsIgnoreCase("rock")
                   && computerChoice.equalsIgnoreCase("rock")) {
               winner = "It’s a tie";
               tie++;

           } else if (userChoice.equalsIgnoreCase("rock")
                   && computerChoice.equalsIgnoreCase("paper")) {
               winner = "Paper covers rock, Computer Wins!";
               computerWinsCnt++;

           } else if (userChoice.equalsIgnoreCase("rock")
                   && computerChoice.equalsIgnoreCase("scissors")) {
               winner = "Rock smashes scissors, You Wins.";
               playerWinsCnt++;

           } else if (userChoice.equalsIgnoreCase("rock")
                   && computerChoice.equalsIgnoreCase("lizard")) {
               winner = "Rock crushes Lizard, You Wins.";
               playerWinsCnt++;

           } else if (userChoice.equalsIgnoreCase("rock")
                   && computerChoice.equalsIgnoreCase("spock")) {
               winner = "Spock vaporizes Rock,Computer Wins!.";
               computerWinsCnt++;
               break;
           } else if (userChoice.equalsIgnoreCase("paper")
                   && computerChoice.equalsIgnoreCase("rock")) {
               winner = "Paper covers rock, you win!";
               playerWinsCnt++;

           } else if (userChoice.equalsIgnoreCase("paper")
                   && computerChoice.equalsIgnoreCase("paper")) {
               winner = "It’s a tie";
               tie++;

           } else if (userChoice.equalsIgnoreCase("paper")
                   && computerChoice.equalsIgnoreCase("scissors")) {
               winner = "Scissors cut paper,Computer Wins!";
               computerWinsCnt++;

           } else if (userChoice.equalsIgnoreCase("paper")
                   && computerChoice.equalsIgnoreCase("lizard")) {
               winner = "Lizard eats Paper,Computer Wins.";
               computerWinsCnt++;

           } else if (userChoice.equalsIgnoreCase("paper")
                   && computerChoice.equalsIgnoreCase("spock")) {
               winner = "Paper disproves Spock,You Wins!.";
               playerWinsCnt++;
           } else if (userChoice.equalsIgnoreCase("scissors")
                   && computerChoice.equalsIgnoreCase("rock")) {
               winner = "Rock smashes scissors,Computer Wins.";
               computerWinsCnt++;
               break;
           } else if (userChoice.equalsIgnoreCase("scissors")
                   && computerChoice.equalsIgnoreCase("paper")) {
               winner = "Scissors cut paper,You Wins!";
               playerWinsCnt++;

           } else if (userChoice.equalsIgnoreCase("scissors")
                   && computerChoice.equalsIgnoreCase("scissors")) {
               winner = "It’s a tie";
               tie++;

           } else if (userChoice.equalsIgnoreCase("scissors")
                   && computerChoice.equalsIgnoreCase("lizard")) {
               winner = "Scissors decapitates Lizard,You Wins.";
               playerWinsCnt++;
           } else if (userChoice.equalsIgnoreCase("scissors")
                   && computerChoice.equalsIgnoreCase("spock")) {
               winner = "Spock smashes Scissors,Computer Wins!.";
               computerWinsCnt++;

           } else if (userChoice.equalsIgnoreCase("lizard")
                   && computerChoice.equalsIgnoreCase("rock")) {
               winner = "Rock crushes Lizard,Computer Wins.";
               computerWinsCnt++;
               break;
           } else if (userChoice.equalsIgnoreCase("lizard")
                   && computerChoice.equalsIgnoreCase("paper")) {
               winner = "Lizard eats Paper,You Wins.";
               playerWinsCnt++;
           } else if (userChoice.equalsIgnoreCase("lizard")
                   && computerChoice.equalsIgnoreCase("scissors")) {
               winner = "Scissors decapitates Lizard,Computer Wins.";
               computerWinsCnt++;
           } else if (userChoice.equalsIgnoreCase("lizard")
                   && computerChoice.equalsIgnoreCase("lizard")) {
               winner = "It's a tie.";
               tie++;

           } else if (userChoice.equalsIgnoreCase("lizard")
                   && computerChoice.equalsIgnoreCase("spock")) {
               winner = "Lizard poisons Spock,You Wins!.";
               playerWinsCnt++;

           } else if (userChoice.equalsIgnoreCase("spock")
                   && computerChoice.equalsIgnoreCase("rock")) {
               winner = "Spock vaporizes Rock,You Wins.";
               playerWinsCnt++;

           } else if (userChoice.equalsIgnoreCase("spock")
                   && computerChoice.equalsIgnoreCase("paper")) {
               winner = "Paper disproves Spock,Computer Wins.";
               computerWinsCnt++;

           } else if (userChoice.equalsIgnoreCase("spock")
                   && computerChoice.equalsIgnoreCase("scissors")) {
               winner = "Spock smashes Scissors,You Wins.";
               playerWinsCnt++;

           } else if (userChoice.equalsIgnoreCase("spock")
                   && computerChoice.equalsIgnoreCase("lizard")) {
               winner = "Lizard poisons Spock,Computer Wins.";
               computerWinsCnt++;

           } else if (userChoice.equalsIgnoreCase("spock")
                   && computerChoice.equalsIgnoreCase("spock")) {
               winner = "It's a tie.";
               tie++;

           }

           System.out.println(winner);

           System.out.print("Enter yes or no:");
           String s = sc.next();
           if (s.equalsIgnoreCase("yes"))
               continue;
           else {
               // Displaying the no of times user won and computer won or tie
               System.out.println("You won: " + playerWinsCnt + " times!");
               System.out.println("Computer won: " + computerWinsCnt
                       + " times!");
               System.out.println("There were: " + tie + " tie games!");
               System.out.println("Thanks for playing!");

               break;
           }
       }
   }

   // This method will chooses the computer selection randomly
   private static String ComputerSelection() {
       String str = null;
       Random rand = new Random();
       int computerInt = rand.nextInt(5) + 1;
       System.out.print("Computer Choice: ");
       switch (computerInt) {
       case 1:
           str = "rock";
           System.out.println(str);
           break;
       case 2:
           str = "paper";
           System.out.println(str);
           break;
       case 3:
           str = "scissors";
           System.out.println(str);
           break;
       case 4:
           str = "lizard";
           System.out.println(str);
           break;
       case 5:
           str = "spock";
           System.out.println(str);
           break;
       }
       return str;
   }
}

====================================

output:

Let's play Rock, Paper, Scissors, Lizard, Spock!
Enter your Choice :towel
Illegal choice: towel
Re-enter your play: rock
Computer Choice: lizard
Rock crushes Lizard, You Wins.
Enter yes or no:yes
Let's play Rock, Paper, Scissors, Lizard, Spock!
Enter your Choice :spock
Computer Choice: scissors
Spock smashes Scissors,You Wins.
Enter yes or no:no
You won: 2 times!
Computer won: 0 times!
There were: 0 tie games!
Thanks for playing!


Please don't hesitate to contact me if you have any queries. :)

Add a comment
Know the answer?
Add Answer to:
java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Its Simple spock smashes scissors scissors cuts paper scissors decapitates lizard paper disproves spock spock vaporiz0S...

    Its Simple spock smashes scissors scissors cuts paper scissors decapitates lizard paper disproves spock spock vaporiz0S rock rock crushes scissors lizard poisons spock paper covers rock lizard eats paper rock crushes lizard

  • “Oh I know, let’s play Rock, Paper, Scissors, Lizard, Spock. It’s very simple.” What better way...

    “Oh I know, let’s play Rock, Paper, Scissors, Lizard, Spock. It’s very simple.” What better way to expand the classic Rock, Paper, Scissors game than to add Lizard and Spock! Sheldon and Raj brought Sam Kass’s game to life on The Big Bang Theory in “The Lizard-Spock Expansion” episode.   Assignment & Discussion Your task in Programming Assignment 8 is to create a C# console app-version of Rock, Paper, Scissors, Lizard, Spock (RPSLS). Use any of the programming tools and techniques...

  • Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game...

    Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game in Python 3 that a user plays against the computer with the ability to save and load a game and its associated play statistics. Purpose: The purpose of this challenge is to assess the developer’s ability to create an interactive application with data persistence in Python 3. Requirements: Create a Rock, Paper, Scissors game in Python named rps.py according to the requirements specified in...

  • 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....

  • 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...

  • 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....

  • In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play...

    In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play the game of Rock, Paper, Scissors against the computer.Your program should have the following: •Make the name of the project RockPaperScissors•Write a method that generates a random number in the range of 1 through 3. The randomly generated number will determine if the computer chooses rock, paper, or scissors. If the number is 1, then the computer has chosen rock. If the number is...

  • Rock, Paper, Scissors - Write a MATLAB script file to play Rock, Paper, Scissors with the...

    Rock, Paper, Scissors - Write a MATLAB script file to play Rock, Paper, Scissors with the computer. x=rand; if x< 1/3 then it is Rock, if l/3<= x < 2/3 it is Paper else it is Scissors end if This is how the game should look like on the screen: Enter "r" for Rock, "p" for Paper, or "s" for Scissors", or enter "q" to Quit the game r leftarrow say, this is what you entered Computer choice: Scissors RESULT:...

  • 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...

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