Question

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 is done.
The function prototype is:
public static int FindWinner (char p1, char p2);

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

Please find the code below:

RockPaperScissorsChoice.java

package classes4;

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

//This class asks the user to write a method doing the comparison
//of two hands for the game Rock, Paper, Scissors.
public class RockPaperScissorsChoice {
   //Create enumeration constants for the types of hands.

   //Create enumeration constants for the outcomes of who wins.
   public enum Winner {
       USER, TIE, USER2
   }

   //Additional private member variables used in the game playing class.
   private Scanner scan = new Scanner(System.in);

   //Constructor that takes the command line arguments to look
   //for an integer random seed to start the computer's hand.
   public RockPaperScissorsChoice() {}

   //Method to play a round of RPS.
   void play() {
       //TODO: Get a hand from the computer by calling getComputerHand();
       String choice;
       while(true){
           char userHand1 = getUserHand("User1 : ");
           //TODO: Get a hand from the user by calling getUserHand();
           char userHand2 = getUserHand("User2 : ");
           //TODO: Pass the return values from those two methods to printWinner().
           printWinner(userHand1, userHand2);
           System.out.print("Continue [yes/no] : ");
           choice = scan.nextLine();
           if(choice.length()!=0 && choice.toLowerCase().startsWith("y")){
               continue;
           }else{
               break;
           }
       }
   }

   //Compute and print the winner of the match, given a hand for each
   //of computer and user. Handle the error case of indeterminate winner.
   void printWinner(char computerHand, char userHand) {
       int winner = FindWinner(computerHand, userHand);
       System.out.println("Player 1 : " + computerHand + " vs Player 2 : " + userHand);
       switch (winner) {
       case 'P':
           System.out.println("Player-2 wins.");
           break;
       case 'T':
           System.out.println("It's a tie.");
           break;
       case 'U':
           System.out.println("Player-1 wins.");
           break;
       default:
           System.out.println("Error, no one wins!");
           break;
       }
   }

   //Evaluate a pair of hands to see which wins. Use the typical ordering of
   //rock > scissors > paper > rock.
   public static int FindWinner (char userHand, char userHand2){
       if(userHand=='S' && userHand2=='P' || userHand=='P' && userHand2=='R'
               || userHand == 'R' && userHand2=='S'
               ){
           return 'U';
       }else if(userHand==userHand2){
           return 'T';
       }else{
           return 'P';
       }

   }


   //Get a user hand from the scanner.
   private char getUserHand(String user) {
       System.out.print(user+"What's your hand, rock/paper/scissors : ");
       String userInput = scan.nextLine();
       if (userInput.equalsIgnoreCase("r")) {
           return 'R';
       } else if (userInput.equalsIgnoreCase("p")) {
           return 'P';
       } else if(userInput.equalsIgnoreCase("s")){
           return 'S';
       }else{
           System.out.println("Invalid entry. Setting hand to paper");
           return 'P';
       }
   }

   //Method to set up the Main object, apply a computer hand,
   //prompt for user hand, evaluate the hands, and print result.
   //Run from the command line with no arguments, or with a single
   //positive long integer to seed the random number generator.
   public static void main(String[] args) {
       RockPaperScissorsChoice rpsPlayer = new RockPaperScissorsChoice();
       rpsPlayer.play();
   }
}

output:

Add a comment
Know the answer?
Add Answer to:
Write a program to score the paper-rock-scissor game. Each of two users types in either P,R...
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
  • Write a program that plays the popular scissor-rock-paper game. (A scissor can cut a paper, a...

    Write a program that plays the popular scissor-rock-paper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws. Here are sample runs: Enter your selection: scissor (0),...

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

  • Write a java program that plays the popular scissor-rock-paper game. (A scissor can cut a paper,...

    Write a java program that plays the popular scissor-rock-paper game. (A scissor can cut a paper, a rock can break a scissor, and a paper can cover a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws. Allow the user to continue playing or quit.

  • Rock, Paper, Scissors (also known by several other names, see http://en.wikipedia.org/wiki/Rock p...

    Help needed in Perl language program! Note : Perl Language Rock, Paper, Scissors (also known by several other names, see http://en.wikipedia.org/wiki/Rock paper scissors) is an extremely popular hand game most often played by children. Often, it is used as a method of selection similar to flipping a coin or throwing dice to randomly select a person for some purpose. Of course, this game is not truly random since a skilled player can often recognize and exploit the non-random behavior of...

  • JAVA Beginnings of a paper-rock-scissors game. Paper-rock-scissors is a game often used to make decisions. There...

    JAVA Beginnings of a paper-rock-scissors game. Paper-rock-scissors is a game often used to make decisions. There are two players. Each player secretly chooses either, paper, rock or scissors. At the count of three, each player reveals what they have chosen. The winner of the game is determined this way: paper beats rock (because paper covers rock) rock beats scissors (because rock crushes scissors) scissors beat paper (because scissors cut paper) If the two players choose the same item, it is...

  • In python language Write a program that lets the user play the game of Rock, Paper,...

    In python language Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 and 3 is generated. 1 = Computer has chosen Rock 2 = Computer has chosen Paper 3 = Computer has chosen Scissors (Dont display the computer's choice yet) 2. The user enters his or her choice of “Rock”, “Paper", “Scissors" at...

  • C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this...

    C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this game against the computer. The program should work as follows: When the program begins, a random number between 1 and 3 is generated. If the number is 1, the computer has chosen rock. If the number is 2, the computer has chosen paper. If the number is 3, the computer has chosen scissors. Don't display the computer's choice yet. Use a menu to display...

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

  • For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-...

    For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-Oriented Programming. You will be working with me on a team to build the program. I have already written my part of the program, and the Game.java file is attached. Your task will be to write a RockPaperScissors class that contains the following methods: getUserChoice: Has the user choose Rock, Paper, or Scissors. After validating the input, the method returns a String containing the user choice....

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