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

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

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

  • This program should be in c++. Rock Paper Scissors: This game is played by children and...

    This program should be in c++. Rock Paper Scissors: This game is played by children and adults and is popular all over the world. Apart from being a game played to pass time, the game is usually played in situations where something has to be chosen. It is similar in that way to other games like flipping the coin, throwing dice or drawing straws. There is no room for cheating or for knowing what the other person is going to...

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

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

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

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

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