Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// RockPaperScissors.java // plays rock paper scissors with the computer import java.util.Scanner; public class RockPaperScissors { // static class constants static final int RAND_LIMIT =

// RockPaperScissors.java // plays rock paper scissors with the computer import java.util.Scanner; public class RockPaperScissors { // static class constants static final int RAND_LIMIT = 3; static final int NUM_ERROR = 0; static final int NUM_ROCK = 1; static final int NUM_PAPER = 2; static final int NUM_SCISSORS = 3; static final String STR_ERROR = "error"; static final String STR_ROCK = "rock"; static final String STR_PAPER = "paper"; static final String STR_SCISSORS = "scissors"; static final int PLAYER_TIE = 0; static final int PLAYER_1 = 1; static final int PLAYER_2 = 2; public static void main(String[] args) { String inputStr; // general purpose string for input boolean continuing = false; int player1Choice = 0, player2Choice = 0; int winner = 0; Scanner input = new Scanner(System.in); // main loop - ask the user if they want to play // and continue if confirmed do { System.out.print("Rock, Paper Scissors: play? (y or n): "); inputStr = input.nextLine(); if (!inputStr.equals("y")) continuing = false; else { continuing = true; System.out.println("OK, let's play!"); // player 1 by default is the computer, we may want // want to enhance in the future to allow two humans // (or two computers!) player1Choice = getComputerChoice(); //System.out.println("Player 1 chooses " + player1Choice); // debug player2Choice = getChoice(input); //System.out.println("Player 2 chooses " + player2Choice); // debug // echo choice and determine winner System.out.println("Computer chose " + choiceNumToString(player1Choice) + ", you chose " + choiceNumToString(player2Choice)); winner = determineWinner(player1Choice, player2Choice); // show winner if (winner == PLAYER_1) System.out.println("Computer wins!"); else if (winner == PLAYER_2) System.out.println("You win!"); else System.out.println("Tie game!"); } } while(continuing == true); System.out.println("Bye!"); } // get, validate, and return the human user's choice public static int getChoice(Scanner input) { int choice = NUM_ERROR; boolean valid = false; while (!valid) { System.out.print("Please enter your choice: " + "\t1 for ROCK " + "\t2 for PAPER " + "\t3 for SCISSORS" + ": "); choice = input.nextInt(); input.nextLine(); // clear the buffer valid = isValid(choice); if (!valid) System.out.println("Invalid choice; please enter either 1, 2, or 3"); } return choice; } }

my RockPaperScissors.java file contains an incomplete implementation of a program which plays the game rock, paper, scissors against the computer. Winners are mapped using the following combinations:

  • Rock smashes scissors
  • Paper smothers rock
  • Scissors cut paper
  • A tie occurs if the same object is chosen by both players.

The file contains a fully functional main method, various useful constant definitions, and an implementation of the getChoice() method which obtains the game choice from the human user.

For this assignment you need to complete the implementation by coding the following methods, for which I have provided the headers, so that the program runs successfully. Use the commented method descriptions to guide your implementation.

// convert a numeric choice to the corresponding string, // e.g. NUM_ROCK returns STR_ROCK public static String choiceNumToString(int choiceNum) // get a random value between 1 and 3 (LIMIT) for the computer's choice public static int getComputerChoice() // return true if ch is valid choice of NUM_ROCK, NUM_PAPER, or NUM_SCISSORS public static boolean isValid(int ch)

// get, validate, and return the human user's choice

public static int getChoice(Scanner input)

// determine the winner - returns PLAYER_1, PLAYER_2, or PLAYER_TIE

public static int determineWinner(int choice1, int choice2)

Do not modify any constant definition, function header, or the main method (but read the hint provided below). Note that I have declared the program constants outside of the main method (above it, actually). Doing it this way means that they are available for use outside of the main method, including in the methods you write (this are similar to "global constants").

Hint: you may want to start out by commenting code in the main method and bring the commented lines back in piece-by-piece as you implement the functions. Remember to restore the entire main method back to it's original state when complete so as not to break the above rule about modifying the main method (what I don't see won't hurt me...).

Sample output of the game is shown below:

Rock, Paper Scissors: play? (y or n): y

OK, let's play!

Please enter your choice:

1 for ROCK

2 for PAPER

3 for SCISSORS: 1

Computer chose rock, you chose rock

Tie game!

Rock, Paper Scissors: play? (y or n): y

OK, let's play!

Please enter your choice:

1 for ROCK

2 for PAPER

3 for SCISSORS: 1

Computer chose paper, you chose rock

Computer wins!

Rock, Paper Scissors: play? (y or n): y

OK, let's play!

Please enter your choice:

1 for ROCK

2 for PAPER

3 for SCISSORS: 1

Computer chose scissors, you chose rock

You win!

Rock, Paper Scissors: play? (y or n): n

Bye!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

4. Explain the strengths and weaknesses of each approach.

Answered: 1 week ago