Question

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. getCPUChoice: Randomly selects Rock, Paper, or Scissors for the computer, and returns a String containing the computer choice. pickWinner: Is passed two Strings containing the user choice and the computer choice. Compares the two choices and selects a winner. Returns a String containing “User”, “Computer”, or “Tie” to indicate the winner. Your class should interact with my class to make the game function correctly. Sample session: Welcome to Rock, Paper, Scissors! How many rounds would you like to play?: 2 Sorry, you need to enter an odd number. Please try again: 3 Rock, Paper, or Scissors?: Monkey Sorry, “Monkey” is not a valid entry. Rock, Paper, or Scissors?: Rock Computer chooses Paper. Computer wins! Rock, Paper, or Scissors?: Rock Computer chooses Rock. It’s a tie. Play again. Rock, Paper, or Scissors?: Paper Computer chooses Rock. User wins! Rock, Paper or Scissors?: Rock Computer chooses Scissors. User wins! User wins: 2 Computer wins: 1 The user won! 


1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

RockPaperScissors.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class RockPaperScissors {
   private String userChoice;
   private String[] cpuChoice = { "Rock", "Paper", "Scissor" };//cpu choice

   public String getUserChoice() throws IOException {
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       boolean valid = true;

       while (valid) {//getting valid user input
           System.out.print("Rock, Paper or Scissor? ");
           userChoice = br.readLine();
           if (userChoice.equalsIgnoreCase("Paper")) {
               valid = false;
           } else if (userChoice.equalsIgnoreCase("Rock")) {
               valid = false;
           } else if (userChoice.equalsIgnoreCase("Scissor")) {
               valid = false;
           } else {
               System.out.println("Sorry " + userChoice + " is not a valid entry");
           }
       }
       return userChoice;
   }

   public String getCpuChoice() {//getting random cpu input
       int ran = (int) (Math.random() * 3);
       System.out.println("Computer chooses " + cpuChoice[ran]);
       return cpuChoice[ran];
   }

   public String pickWinner(int userChoice, int computerChoice) {
       if (userChoice == computerChoice) // tie cases
       {
           return "Tie";
       } else if (userChoice == 1 && computerChoice == 3) // user wins rock vs scissors
       {
           return "User";
       } else if (userChoice == 3 && computerChoice == 2) // user wins scissors vs paper
       {
           return "User";
       } else if (userChoice == 2 && computerChoice == 1) // user wins paper vs rock
       {
           return "User";
       } else // otherwise computer wins
       {
           return "CPU";
       }
   }
}

public class RockPaperSissorsDemo {
   public static void main(String[] args) throws IOException {
       // for taking console input
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       RockPaperScissors obj = new RockPaperScissors();
       int cpu = 0, user = 0, tie = 0, round = 0;
       boolean valid = true;
       while (valid) {//getting odd number of rounds
           System.out.print("How many rounds would you like to play? ");
           round = Integer.parseInt(br.readLine());
           if (round % 2 == 0) {
               System.out.println("Sorry, you need to enter an odd number");
           } else {
               valid = false;
           }
       }
       for (int i = 1; i <= round; i++) {//running till given rounds
           String userChoice = obj.getUserChoice();
           String cpuChoice = obj.getCpuChoice();
           int userNum = getChoiceNum(userChoice);
           int cpuNum = getChoiceNum(cpuChoice);
           String winner = obj.pickWinner(userNum, cpuNum);
           if(winner.equalsIgnoreCase("user")) {
               System.out.println("You Win!");
               user++;
           }
           else if(winner.equalsIgnoreCase("cpu")){
               System.out.println("Computer Wins!");
               cpu++;
           }
           else {
               System.out.println("Its a Tie!");
               tie++;
           }
       }
       //showing statistics
       System.out.println("*****************Game Statistics*****************");
       System.out.println("Player Won: "+user);
       System.out.println("Computer Won: "+cpu);
       System.out.println("Tie: "+tie);
       if(user>cpu) {
           System.out.println("You're the Winner");
       }
       else if(user            System.out.println("Computer is the Winner");
       }
       else {
           System.out.println("The game was a Tie");
       }
   }

   private static int getChoiceNum(String choice) {//converting rock, paper, scissor to numbers
       if (choice.equalsIgnoreCase("rock")) {
           return 1;
       } else if (choice.equalsIgnoreCase("paper")) {
           return 2;
       } else {
           return 3;
       }
   }
}

Program Screenshots

RockPaperSissorsDemo.java 1 import java.io. BufferedReader; 2 import java.io. IOException; 3import java.io. InputStreamReader

Output

How many rounds would you like to play? 5 Rock, Paper or Scissor? scissor Computer chooses Paper You Win! Rock, Paper or Scis

Add a comment
Know the answer?
Add Answer to:
For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

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

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

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

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

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

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

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

  • Create a python program to play rock paper scissors using a while loop and if statements....

    Create a python program to play rock paper scissors using a while loop and if statements. I have started but have gotten stuck in a continuous loop. Please look over my code and help me find where I went wrong. Here it is: import random #Choice weapons=['Rock' ,'Paper' ,'Scissors'] print('Rock, Paper, Scissors!') print('Rock, Paper, Scissors!') print('Shoot!') human=input('Choose Rock, Paper, Scissors, or Quit! ') print('')#Blank Line while human != 'Quit': human_choice=human computer=random.choice(weapons) print(computer) if human==computer: print("It's a tie!") elif human=='Rock': if...

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