Question

Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that...

Java

CSC252   Programming II   
Static Methods, Enums, Constants, Random

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().
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The java code is as follows :

import java.util.*;

enum State
{
   WIN, LOSE, TIE;
}

class RPS
{
   //Constants to represent rock, paper and scisoors
   protected static final int ROCK = 1;
   protected static final int PAPER = 2;
   protected static final int SCISSORS = 3;

   protected static State playerState;

   public static void play()
   {
       Random ran = new Random();
       Scanner in = new Scanner(System.in);

       int playerChoice;
       int CPUChoice;

       int playerWonCount = 0;
       int CPUWonCount = 0;

       System.out.println("Welcome to Rock, Paper, Scissors!!");

       for (int i = 0; i < 3; i++)
       {
           System.out.println("\nRound : " + (i + 1) + " - ");

           System.out.print("Enter " + RPS.ROCK + " for rock, " + RPS.PAPER + " for paper, and " + RPS.SCISSORS + " for scisoors :- ");
           playerChoice = in.nextInt();

           CPUChoice = ran.nextInt(3) + 1;

           System.out.print("\nYou choose : ");
           if (playerChoice == RPS.ROCK)
               System.out.println("Rock");
           else if (playerChoice == RPS.PAPER)
               System.out.println("Paper");
           else
               System.out.println("Scissors");

           System.out.print("CPU choose : ");
           if (CPUChoice == RPS.ROCK)
               System.out.println("Rock");
           else if (CPUChoice == RPS.PAPER)
               System.out.println("Paper");
           else
               System.out.println("Scissors");

           if (playerChoice == CPUChoice)
               System.out.println("\nIts a tie!!");
           else if ( (playerChoice == RPS.PAPER && CPUChoice == RPS.ROCK) || (playerChoice == RPS.ROCK && CPUChoice == RPS.SCISSORS)
               || (playerChoice == RPS.SCISSORS && CPUChoice == RPS.PAPER) )
           {
               System.out.println("\nYou won the round!!");
               playerWonCount++;
           }
           else
           {
               System.out.println("\nCPU won the round!!");
               CPUWonCount++;
           }
       }

       if (CPUWonCount == playerWonCount)
           RPS.playerState = State.TIE;
       else if (CPUWonCount > playerWonCount)
           RPS.playerState = State.LOSE;
       else
           RPS.playerState = State.WIN;

       switch(RPS.playerState)
       {
           case TIE :
               System.out.println("\n\nThe game was a tie!!\n");
               break;
           case WIN :
                   System.out.println("\n\nYou won the game!!\n");
               break;
           case LOSE :
                   System.out.println("\n\nCPU won the game\n");
               break;
       }
   }

   public static void main(String args[])
   {
       char ch = ' ';
       Scanner in = new Scanner(System.in);

       while(true)
       {
           System.out.print("Do you want to play(y/n) : ");
           ch = in.next().charAt(0);

           if (ch != 'n' && ch != 'N')
               RPS.play();
           else break;
       }

       System.out.println("\n\nGame Ended!!");
   }

}

OUTPUT :

Do you want to play y/n) : y Welcome to Rock, Paper, Scissors!! Round : 1 - Enter 1 for rock, 2 for paper, and 3 for scisoors

Add a comment
Know the answer?
Add Answer to:
Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that...
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
  • java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of...

    java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of the common game Rock Paper Scissors that is often used to pass time (or sometimes to make decisions.) The rules of the game are outlined below: • • Scissors cuts Paper Paper covers Rock Rock crushes Lizard Lizard poisons Spock Spock smashes Scissors Scissors decapitates Lizard Lizard eats Paper Paper disproves Spock Spock vaporizes Rock Rock crushes Scissors Write a program that simulates the...

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

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

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

  • Objective: Write a program that simulates a game of rock, paper, scissors between a human and...

    Objective: Write a program that simulates a game of rock, paper, scissors between a human and the computer in best 2 out of 3 rounds. Requirements: . The player can enter either "rock", "paper", or "scissors'". o If the player enters anything other than that the computer automatically gets a point . The computer randomly selects one of the gestures o Use the Random type to make this easier o Also make sure you import java.util.Random o You can use...

  • Write a Python program (rock_paper_scissors.py) that allows two players play the game rock paper scissors. Remember...

    Write a Python program (rock_paper_scissors.py) that allows two players play the game rock paper scissors. Remember the rules: 1. Rock beats scissors 2. Scissors beats paper 3. Paper beats rock The program should ask the users for their names, then ask them for their picks (rock, paper or scissors). After that, the program should print the winner's name. Note, the players may pick the same thing, in this case the program should say it's tie. when the user input an...

  • Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will...

    Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will pick a move (Rock Paper Scissors) from 3 radio buttons and click the play button to play the game. The application will use random number generator to pick a move for the computer. Then, the application display the computer's move and also display whether you won or lost the game, or the game is tie if both moves are the same. The application must...

  • Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game...

    Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game in Python 3 that a user plays against the computer with the ability to save and load a game and its associated play statistics. Purpose: The purpose of this challenge is to assess the developer’s ability to create an interactive application with data persistence in Python 3. Requirements: Create a Rock, Paper, Scissors game in Python named rps.py according to the requirements specified in...

  • In this problem, you’ll play a simple rock, paper, scissors game. First, you’ll ask the user...

    In this problem, you’ll play a simple rock, paper, scissors game. First, you’ll ask the user to pick rock, paper, or scissors. Then, you’ll have the computer randomly choose one of the options. After that, print out the winner! You should keep playing the game until the user hits enter. Note: You’ll need to implement a method called String getWinner(String user, String computer). Luckily, you just wrote that in an earlier program! Here is a sample run of the program....

  • In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play...

    In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play the game of Rock, Paper, Scissors against the computer.Your program should have the following: •Make the name of the project RockPaperScissors•Write a method that generates a random number in the range of 1 through 3. The randomly generated number will determine if the computer chooses rock, paper, or scissors. If the number is 1, then the computer has chosen rock. If the number is...

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