Rock Paper Scissors Write a program that allows the user to

Rock Paper Scissors Write a program that allows the user to play \"Rock, Paper, Scissors\". Write the RPS class. All code should be in one file. main() will be part of the RPS class, fairly small, and contain only a loop that asks the user if they want to play \"Rock, Paper, Scissors\". If they say yes, it calls the static method play() of the RPS class, if not, the program ends. play() has no parameters, and no return value. (In this exercise, the class RPS does contain code that will input and output to the user.) Use GUI for all input and output. Note that since play() is a static method, it can be called directly on the RPS class -- main() should not instantiate an RPS object. play() will allow one full game of RPS to run. To win an RPS game, the player or the computer must win 2 out of 3 rounds. The following should all be done inside the RPS class: Decide how the user will input rock, paper or scissors: character? string? integer? Prompt the user to input rock, paper, or scissors. Use simple constants (static final) to represent ROCK, PAPER, and SCISSORS. Use enums to represent WIN, LOSE, TIE. (This is the result of each round.) Use the Random class to generate random computer choices. (Don\'t use Math.random). For each round, print out what the player threw, what the computer threw, and who won or if there was a tie. In RPS, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. Optimize your code so that you are testing as few cases as possible, but covering all the possible combinations. Keep track of the number of rounds that the player has won and the computer has won. When someone wins the game (wins 2 rounds), print out a message indicating whether the player or the computer has won, and return to main().

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

 Rock Paper Scissors Write a program that allows the user to play \
 Rock Paper Scissors Write a program that allows the user to play \
 Rock Paper Scissors Write a program that allows the user to play \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site