Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that plays the RockPaper-Scissors game. When played between two

ID: 3682937 • Letter: W

Question

Write a program that plays the RockPaper-Scissors game. When played between two people, each person picks one of three options at the same time, and a winner is determined: Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should randomly choose one of the three options without revealing it to the user and then prompt for the user's selection. At that point the program reveals its choice and prints a statement indication if the computer won, the user won, or if it was a tie. Continue playing until the user chooses to stop, then print the number of user wins, losses, and ties. the player enters Q to quit. and P,R,S for paper, rock and, scissors.

Enter Q to quit the game

Enter your choice (R, P, S): R

Computer picks S User wins!

Enter your choice (R, P, S): R

Computer picks P

Computer wins!

Enter your choice (R, P, S): R

Computer picks R

Tie

Enter your choice (R, P, S): P

Computer picks S Computer wins!

Enter your choice (R, P, S): X

Choice must be R, P, or S

Enter your choice (R, P, S): R

Computer picks S User wins!

Enter your choice (R, P, S): S

Computer picks P

User wins!

Enter your choice (R, P, S): Q

Game over User won 2 games, Computer won 2 games, ties 1

Explanation / Answer

import java.util.Scanner;
import java.lang.String;
import java.util.Random;

public class HelloWorld
{
   public static void main(String[] args)
   {

       Scanner scan = new Scanner(System.in);
       Random rand = new Random();                
       int playerScore = 0;
       int computerScore = 0,tieScore=0;
       String playerHand="",computerHand="";
       boolean flag=true;
       System.out.println("Enter Q to quit the game");
       while (true)
       {
           System.out.println("Enter your choice(R,P,S): ");
           playerHand = scan.next();
           if ( !(   playerHand.equals("R")
                   || playerHand.equals("P")
                   || playerHand.equals("S")
                   || playerHand.equals("Q")))
           {
               flag = false;
           }
           while (!flag) {
            
               System.out.print("Enter your choice(R,P,S): ");

               playerHand = scan.next();
            
               // Check new input is valid
               if (      playerHand.equals("R")
                   || playerHand.equals("P")
                   || playerHand.equals("S")
                   || playerHand.equals("Q"))
               {
                   flag = true;
               }
           }
               if (playerHand.equals("Q"))
           {
               System.out.print("Game over");
               System.out.println("User won "+playerScore + " games, Computer won :"+computerScore +" games, ties "+ tieScore);
               return;
           }
           int tempHand = rand.nextInt(3);
           switch (tempHand)
           {
           case 0:
               computerHand = "R";
               break;
           case 1:
               computerHand = "P";
               break;
           case 2:
               computerHand = "S";
               break;
           }
           if (playerHand.equals(computerHand))
           {
               System.out.println("Tie");
               tieScore++;
           }
           else
           {
                switch (computerHand)
               {
               case "R":
                   if (playerHand.equals("P"))
                   {
                       System.out.println("Computer picks R.User Wins");
                       playerScore++;
                   }
                   else if (playerHand.equals("S"))
                   {
                       System.out.println("Computer picks R.Computer Wins");
                       computerScore++;
                   }
                   break;
                
               case "P":
                   if (playerHand.equals("R"))
                   {
                       System.out.println("Computer picks P.Computer Wins");
                       computerScore++;
                   }
                   else if (playerHand.equals("S"))
                   {
                       System.out.println("Computer picks P.User Wins");
                       playerScore++;
                   }
                   break;
                
               case "S":
                   if (playerHand.equals("R"))
                   {
                       System.out.println("Computer picks S.User Wins");
                       playerScore++;
                   }
                   else if (playerHand.equals("P"))
                   {
                        System.out.println("Computer picks S.Computer Wins");
                       computerScore++;
                   }
                   break;
               }
           }
        
           }
        
       }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote