Question



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 randomly selected integers to represent the gestures For each combination either the computer scores a point, the player scores a point, or they score neither on draws. o Rock vs Paper Paper wins o Paper vs Scissors-Scissors wins o Scissors vs Rock Rock wins After 3 rounds the winner or a tie is declared . The player is then asked whether or not they want to play again, and if they do the whole process starts over
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//RockPaperScissors.java

import java.util.Random;

import java.util.Scanner;

public class RockPaperScissors {

private static boolean playAgain(Scanner scanner) {

System.out.println("Play again? Y(8), N(9)?");

switch (scanner.nextInt()) {

case 8:

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

return true;

default:

System.out.println("Thanks for playing!");

return false;

}

}

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

RPSPlayer computer = new RandomComputerPlayer(new Random());

RPSPlayer human = new HumanPlayer(scanner);

// Count the number of wins

int winCount = 0;

System.out.println("Rock Paper Scissors");

do {

for (int i = 1; i <= 3; i++) {

String comp = computer.play();

String you = human.play();

System.out.printf("%s vs. %s\n", comp, you);

if (you.equals(comp)) {

System.out.println(", IT'S A TIE!");

} else if (("Rock".equals(you) && "Scissors".equals(comp))

|| ("Scissors".equals(you) && "Paper".equals(comp))

|| ("Paper".equals(you) && "Rock".equals(comp))) {

winCount++;

}

}

if (winCount >= 2)

System.out.println("! You win!");

else

System.out.println("! You lose!");

} while (playAgain(scanner));

}

}

//RPSPlayer.java

public interface RPSPlayer {

String[] CHOICES = new String[] { "Rock", "Paper", "Scissors" };

/**

* Returns one of "Rock", "Paper", or "Scissors".

*/

String play();

}

//HumanPlayer.java

import java.util.Scanner;

public class HumanPlayer implements RPSPlayer {

private final Scanner scanner;

public HumanPlayer(Scanner scanner) {

this.scanner = scanner;

}

public String play() {

System.out.println("Select 1, 2, or 3 for Rock, Paper, Scissors");

int choice = this.scanner.nextInt();

// Keeping things simple, not doing any validation here

return CHOICES[choice - 1];

}

}

//RandomComputer.java

import java.util.Random;

public class RandomComputerPlayer implements RPSPlayer {

private final Random random;

public RandomComputerPlayer(Random random) {

this.random = random;

}

public String play() {

return CHOICES[this.random.nextInt(CHOICES.length)];

}

}

Output:

File Edit Source Refactor Navigate Search Project Run Window Help Quick Access Problems @ JavadocDeclaration Console 8 «terminated> RockPaperScissors Java Application C Program Files Java yre1.80171 bin ave exe 29 Sep 2018 2:20:35 A Rock Select 1, 2, or 3 for Rock, Paper, Scissors Paper Scissors Paper vs. Rock Select 1, 2, or 3 for Rock, Paper, Scissors Scissors vs. Paper Select 1, 2, or 3 for Rock, Paper, Scissors Paper vs. Scissors ! You lose! Play again? Y(8), N(9)? 9 Thanks for playing! O Type here to search

Add a comment
Know the answer?
Add Answer to:
Objective: Write a program that simulates a game of rock, paper, scissors between a human and...
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
  • 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....

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

  • Write a computer game based on rock-paper- scissors in C++. The 2-player game will be interactive:...

    Write a computer game based on rock-paper- scissors in C++. The 2-player game will be interactive: human vs. computer! Making the game interactive presents a new issue for us -- how do we get the computer&#39;s choice? To solve this, we will use a &quot;random number generator&quot;, which is the basis for all computer gaming. This enables the properly programmed computer to act like it is actually &quot;thinking&quot; and making its own decisions. This is the largest, most involved program...

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

  • i have created a program for a game of rock, paper, scissors but i must make...

    i have created a program for a game of rock, paper, scissors but i must make it run more than once in some kind of loop. i want to add a function named runGame that will control the flow of a single game. The main function will be used to determine which game mode to initiate or exit program in Player vs. Computer, the user will be asked to enter their name and specify the number of rounds for this...

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

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

  • It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user...

    It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user selects 'p': 1. First the program should call a function named getComputerChoice to get the computer's choice in the game. The getComputerChoice function should generate a random number between 1 and 3. If the random number is 1 the computer has chosen Rock, if the random number is 2 the user has chosen Paper, and if the random number is 3 the computer has...

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