Question

Modify your Rock Paper Scissor (Lizard/Spock) game from Week 5 to generate a random number for...

Modify your Rock Paper Scissor (Lizard/Spock) game from Week 5 to generate a random number for Player 2 and assign that as Player 2's choice. The rest of your program should stay the same. You can use your program with the nested if statements or the switch statement. The implementation should use the Random class to get your random number. The assignment is worth 10 points. Upload your submission to the assignment.
code:

import java.util.Scanner;

public class RPSLSYourLastName {
   public static int findWinner(char p1Choice, char p2Choice) {
       int winner = 0;
       switch (p1Choice) {
       case 'r': {
           switch (p2Choice) {
           case 'r': {
               winner = 0;
               break;
           }
           case 'p': {
               winner = 2;
               break;
           }
           case 'c': {
               winner = 1;
               break;
           }
           case 'l': {
               winner = 1;
               break;
           }
           case 's': {
               winner = 2;
               break;
           }
           }
           break;
       }
       case 'p': {
           switch (p2Choice) {
           case 'r': {
               winner = 1;
               break;
           }
           case 'p': {
               winner = 0;
               break;
           }
           case 'c': {
               winner = 2;
               break;
           }
           case 'l': {
               winner = 2;
               break;
           }
           case 's': {
               winner = 1;
               break;
           }
           }
           break;
       }
       case 'c': {
           switch (p2Choice) {
           case 'r': {
               winner = 2;
               break;
           }
           case 'p': {
               winner = 1;
               break;
           }
           case 'c': {
               winner = 0;
               break;
           }
           case 'l': {
               winner = 1;
               break;
           }
           case 's': {
               winner = 2;
               break;
           }
           }
           break;
       }
       case 'l': {
           switch (p2Choice) {
           case 'r': {
               winner = 2;
               break;
           }
           case 'p': {
               winner = 1;
               break;
           }
           case 'c': {
               winner = 2;
               break;
           }
           case 'l': {
               winner = 0;
               break;
           }
           case 's': {
               winner = 1;
               break;
           }
           }
           break;
       }
       case 's': {
           switch (p2Choice) {
           case 'r': {
               winner = 1;
               break;
           }
           case 'p': {
               winner = 2;
               break;
           }
           case 'c': {
               winner = 1;
               break;
           }
           case 'l': {
               winner = 2;
               break;
           }
           case 's': {
               winner = 0;
               break;
           }

           }
           break;
       }

       }

       return winner;
   }

   public static void main(String[] args) {
       Scanner pick = new Scanner(System.in);
       String s1 = ""; // s1 is player 1's choice string
       String s2 = ""; // s2 is player 2's choice string
       System.out.println("Player 1: Choose r=rock, c=scissors, p=paper l=lizard s=spock:");
       s1 = pick.next();
       char c1 = s1.toLowerCase().charAt(0);
       System.out.println("Player 2: Choose r=rock, c=scissors, p=paper l=lizard s=spock:");
       s2 = pick.next();
       char c2 = s2.toLowerCase().charAt(0);
       int winner = findWinner(c1, c2);
       if (winner == 0) {
           System.out.println("It's a draw! You both chose the same object" + " which is: " + s1 + ".");
       } else {
           System.out.println("Player " + winner + " Wins.");
       }
   }
}

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

Answer:

Explanation:

Random object r is created and a choice is made using random.nextInt() from 0 to 4.

is 0 is generated, "r" is assigned to s2

is 1 is generated, "c" is assigned to s2

is 2 is generated, "p" is assigned to s2

is 3 is generated, "l" is assigned to s2

is 4 is generated, "s" is assigned to s2

here is the code:

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

public class Main {
public static int findWinner(char p1Choice, char p2Choice) {
int winner = 0;
switch (p1Choice) {
case 'r': {
switch (p2Choice) {
case 'r': {
winner = 0;
break;
}
case 'p': {
winner = 2;
break;
}
case 'c': {
winner = 1;
break;
}
case 'l': {
winner = 1;
break;
}
case 's': {
winner = 2;
break;
}
}
break;
}
case 'p': {
switch (p2Choice) {
case 'r': {
winner = 1;
break;
}
case 'p': {
winner = 0;
break;
}
case 'c': {
winner = 2;
break;
}
case 'l': {
winner = 2;
break;
}
case 's': {
winner = 1;
break;
}
}
break;
}
case 'c': {
switch (p2Choice) {
case 'r': {
winner = 2;
break;
}
case 'p': {
winner = 1;
break;
}
case 'c': {
winner = 0;
break;
}
case 'l': {
winner = 1;
break;
}
case 's': {
winner = 2;
break;
}
}
break;
}
case 'l': {
switch (p2Choice) {
case 'r': {
winner = 2;
break;
}
case 'p': {
winner = 1;
break;
}
case 'c': {
winner = 2;
break;
}
case 'l': {
winner = 0;
break;
}
case 's': {
winner = 1;
break;
}
}
break;
}
case 's': {
switch (p2Choice) {
case 'r': {
winner = 1;
break;
}
case 'p': {
winner = 2;
break;
}
case 'c': {
winner = 1;
break;
}
case 'l': {
winner = 2;
break;
}
case 's': {
winner = 0;
break;
}

}
break;
}

}

return winner;
}

public static void main(String[] args) {
Scanner pick = new Scanner(System.in);
Random r = new Random();
String s1 = ""; // s1 is player 1's choice string
String s2 = ""; // s2 is player 2's choice string
System.out.println("Player 1: Choose r=rock, c=scissors, p=paper l=lizard s=spock:");
s1 = pick.next();
char c1 = s1.toLowerCase().charAt(0);
int random_choice = r.nextInt(5);
if(random_choice==0)
s2 = "r";
else if(random_choice==1)
s2 = "c";
else if(random_choice==2)
s2 = "p";
else if(random_choice==3)
s2 = "l";
else if(random_choice==4)
s2 = "s";

char c2 = s2.toLowerCase().charAt(0);
int winner = findWinner(c1, c2);
if (winner == 0) {
System.out.println("It's a draw! You both chose the same object" + " which is: " + s1 + ".");
} else {
System.out.println("Player " + winner + " Wins.");
}
}
}

Output:

Player 1: Choose r-rock, c=scissors, p=paper l=lizard 3=spock: Player 1 Wins. ...Program finished with exit code o Press ENTE

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

Add a comment
Know the answer?
Add Answer to:
Modify your Rock Paper Scissor (Lizard/Spock) game from Week 5 to generate a random number for...
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
  • 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...

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

  • Write a program to score the paper-rock-scissor game. Each of two users types in either P,R...

    Write a program to score the paper-rock-scissor game. Each of two users types in either P,R or S. The program call function Find winner to announces the winner. For determining the winner: Paper covers rock, Rock breaks scissors, Scissors cut paper, or it is a tie. Be sure to allow the users to use lower case as well as uppercase letters. Your program should include a loop that lets the user play again until the user says she or he...

  • This program should be in c++. Rock Paper Scissors: This game is played by children and...

    This program should be in c++. Rock Paper Scissors: This game is played by children and adults and is popular all over the world. Apart from being a game played to pass time, the game is usually played in situations where something has to be chosen. It is similar in that way to other games like flipping the coin, throwing dice or drawing straws. There is no room for cheating or for knowing what the other person is going to...

  • Consider the following version of the Rock-Paper-Scissors game. The two players have to choose simultaneously between...

    Consider the following version of the Rock-Paper-Scissors game. The two players have to choose simultaneously between Rock(R), Paper(P) or Scissors(S). According to this game, R beats S, S beats P, P beats R. The winner gets 1 dollar from the other player. In case of a tie,the referee gives both players 2 dollars. Payoffs for all possible choices are summarized in the table below. Find all Nash Equilibria. 3) (25 points) Consider the following version of the Rock-Paper-Scissors game. The...

  • 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 language This assignment is to write a program to score the paper-rock-scissors game AGAIN. Each...

    c language This assignment is to write a program to score the paper-rock-scissors game AGAIN. Each of two players enters either P, R, or S. The program then announces the winner as well as the basis for determining the winner: “Paper covers rock”, “Rock breaks scissors”, “Scissors cut paper”, or “Draw, nobody wins”. The primary differences between this and previous two versions are described as below: (1) You MUST use a function to get the input value for the player’s...

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

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

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