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

  • (c++ only)Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (c++ only)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, the user enters his or her choice of “rock”, “paper”, or “scissors” at the keyboard using a menu in a function, userChoice, that returns a character. Next, there should be a function, computerChoice, that generates the computer’s play. A random number in the range of 1 through 3 is generated. If 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...

  • C++ Write a program that plays the rock, paper, scissors game. Rock beats scissors; scissors beats...

    C++ Write a program that plays the rock, paper, scissors game. Rock beats scissors; scissors beats paper; paper beats rock. You must use these specific functions in your program. These are the prototypes: char generateP2toss(); int checkThrow(char, char); void printStatistics(int, int, int, int); void finalStatistics(int, int, int, int); void welcome(); Notes on these functions: generateP2toss() will generate the computer's toss (the computer is player2). It returns either an "r", "p", or "s/" checkThrow(char char) will check the throw by passing...

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

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

  • Write a program in python that lets the user play the game Rock, Paper, Scissors against...

    Write a program in python that lets the user play the game 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. 2 corresponds to paper, and 3 corresponds to scissors. To set up the random number library, write the following at the top of your code: import random random.seed(300) Use...

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