In the game Rock Paper Scissors, two players simultaneously choose one of three
ID: 3668972 • Letter: I
Question
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper, or scissors. if both players choose the same option, then the result is a tie. However, if they choose differently, the winner is determined as follows: Rock beats scissors; Scissors beats paper; Paper beats rock. Create a game in which the computer randomly chooses rock, paper or scissors. Let the user enter a number 1, 2, or 3, each representing one of the three choices. Then, determine the winner. Save the application as RockPaperScissors.java
Explanation / Answer
Program Plan: The program RockPaperScissor.java, User become one player and the System or program itself become the other playeHence r by generating the random option in rock,paper and scissor.
Program:
//importing the packges
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
import java.util.Random;
/* Name of the class has to be "Main" only if the class is public. */
public class RockPaperScissors {
public static void main(String[] args) throws java.lang.Exception {
Scanner s = new Scanner(System.in);
Random rn = new Random();
int input;
int n = 1;
System.out.println("Hi lets play the Game");
System.out.println("Pick 1,2, or 3 for:");
System.out.println("1-ROCK ,2-PAPER, or 3-SCISSOR (3)");
while (n != 0) {
int r = 1, p = 2, sc = 3; //r for Rock,p for paper,scc for scissors
input = s.nextInt(); //reading the user input
int rand = rn.nextInt(3) + 1; //generating the random number.method returns 0 or1 or 2.thats why we are adding //comparing the system option with the user option by using if..else structure
if (rand == Rock) {
if (input == r) {
System.out.println("Rock Vs. Rock :: TIE");
} else if (input == p) {
System.out.println("Paper Vs. Rock: YOU WIN");
} else if (input == sc) {
System.out.println("Scissor Vs. Rock: YOU LOSE");
}
} else if (rand == Paper) {
if (input == r) {
System.out.println("Rock Vs. Paper: YOU LOSE");
} else if (input == p) {
System.out.println("Paper Vs. Paper: TIE");
} else if (input == sc) {
System.out.println("Scissor Vs. Paper: YOU WIN");
}
} else if (rand == sc) {
if (input == r) {
System.out.println("Rock Vs. Scissor: YOU WIN");
} else if (input == p) {
System.out.println("Paper Vs. Scissor: YOU LOSE");
} else if (input == sc) {
System.out.println("Scissor Vs. Scissor: TIE");
}
}
//Asking the user to play again
char ch;
System.out.println("Do you want to play again? Y/N");
ch= s.next();
if(ch=='Y' ||ch='y'){
n=1;
System.out.println("Rock, Paper,SCISSOR");
}
else if(ch=='N' || ch=='n')
{System.exit(0);
System.out.println("GOODBYE");
}
}
}
}
Sample output:
C:/> javac RockPaperScissor.java
C:/>java RockPaperScissor
Hi lets play the Game
Pick 1,2 or 3 for:
1-ROCK ,2-PAPER, or 3-SCISSOR
1
Rock Vs.Rock::TIE
Do you want to play again?Y/N
n
GOODBYE
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.