Rock Paper Scissors Write a program that allows the user to
Solution
import java.util.Random;
import java.util.Scanner;
/**
*
*/
/**
* @author JKishore
*
*/
enum RESULT{
WIN,LOOSE,TIE
}
public class RPS {
public static Scanner sc=new Scanner(System.in); // scanner class to take input from the user
public static final int ROCK=1;
public static final int PAPER=2;
public static final int SCISSORS=3;
public static RESULT result;
/**
* main method to start the application
*/
public static void main(String[] args) {
String answer=\"N\";
System.out.println(\"Do you want to play ROCK PAPER SCISSORSs game?Y/N\");
answer=sc.next();
if(answer.equals(\"Y\")){
play();
}else{
System.exit(0);
}
}
// method to play the game
public static void play(){
int userWin=0; // user win count
int computerWin=0; // computer win count
Random rnd = new Random(); // random class to generate random number
do{
int input;
System.out.println(\"Pick 1,2, or 3 for : \ ROCK (1), PAPER(2), or SCISSORS (3)\");
input=sc.nextInt();
int randomNumber = rnd.nextInt(3 - 1 + 1) + 1; //generating the random number between 1-3
if (randomNumber == ROCK) { // checking the conditions
if (input == ROCK) {
System.out.println(\"ROCK beats ROCK\");
result=RESULT.TIE;
} else if (input == PAPER) {
System.out.println(\"PAPER beats ROCK\");
result=RESULT.WIN;
userWin++;
} else if (input == SCISSORS) {
System.out.println(\"ROCK beats SCISSORS\");
computerWin++;
result=RESULT.LOOSE;
}
} else if (randomNumber == PAPER) {
if (input == ROCK) {
System.out.println(\"PAPER beats ROCK\");
result=RESULT.LOOSE;
computerWin++;
} else if (input == PAPER) {
System.out.println(\"PAPER beats PAPER\");
result=RESULT.TIE;
} else if (input == SCISSORS) {
System.out.println(\"SCISSORS beats PAPER\");
result=RESULT.WIN;
userWin++;
}
} else if (randomNumber == SCISSORS) {
if (input == ROCK) {
System.out.println(\"ROCK beats SCISSORS\");
result=RESULT.WIN;
userWin++;
} else if (input == PAPER) {
System.out.println(\"SCISSORS beats PAPER\");
result=RESULT.LOOSE;
computerWin++;
} else if (input == SCISSORS) {
System.out.println(\"SCISSORS beats SCISSORS\");
result=RESULT.TIE;
}
}
System.out.println(\"You \"+result+\" this round\");
}while(userWin<2 && computerWin<2); // this loop will exit when user or computer win the game atleast 2 times
if(userWin==2){
System.out.println(\"You win the game\");
}else{
System.out.println(\"Computer wins the game\");
}
}
}
---------------------output-----------------
Do you want to play ROCK PAPER SCISSORSs game?Y/N
Y
Pick 1,2, or 3 for :
ROCK (1), PAPER(2), or SCISSORS (3)
1
ROCK beats SCISSORS
You WIN this round
Pick 1,2, or 3 for :
ROCK (1), PAPER(2), or SCISSORS (3)
2
PAPER beats PAPER
You TIE this round
Pick 1,2, or 3 for :
ROCK (1), PAPER(2), or SCISSORS (3)
3
SCISSORS beats SCISSORS
You TIE this round
Pick 1,2, or 3 for :
ROCK (1), PAPER(2), or SCISSORS (3)
1
ROCK beats ROCK
You TIE this round
Pick 1,2, or 3 for :
ROCK (1), PAPER(2), or SCISSORS (3)
3
SCISSORS beats SCISSORS
You TIE this round
Pick 1,2, or 3 for :
ROCK (1), PAPER(2), or SCISSORS (3)
2
SCISSORS beats PAPER
You LOOSE this round
Pick 1,2, or 3 for :
ROCK (1), PAPER(2), or SCISSORS (3)
1
PAPER beats ROCK
You LOOSE this round
Computer wins the game


