Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

There is a game that some children play called rock-scissors-paper. Two contesta

ID: 3708056 • Letter: T

Question

There is a game that some children play called rock-scissors-paper. Two contestants each select one of the items: rock, scissors or paper, make a symbol with their hands representing this object and then the two contestants simultaneously show their representation of the object to the other. The winner is decided as follows: Rock beats scissors.. Scissors beats paper. Paper beats rock Combinations of similar objects are considered a draw simulate two people playing this game by writing a program in Java that uses threads. Each player is to be represented by a thread; the same instance of each thread must be used for all "rounds" (see next item). Let the playing of the game by the two players be called a round The code is to simulate the playing of n-rounds of the game. For each round the values selected by the two players are to be compared; that is, the values selected by each player in a given round are to be compared -not values from different rounds. This comparison is to be performed as the values are generated. It is not acceptable to store the values in arrays and perform the comparisons at a later.time You must use the following Java keywords to help synchroaize your program: synchronized, wait, notify (or notifyAll in place of notify). Run your program for the processing of at least 1000 rounds. Display summary statistics to show that the program worked correctly. See the sample Sample output Round 997: Player 1 wins! Round 998: Player 1 selects rock Round 997: Player 2 loses Round 998: Player 2 selects scissors Round 998: Player 2 loses Round 999: Player 2 selects scissors Round 998: Player 1 wins! Round 999: Player 1 selects scissors Round 999: Player 1: Draw Round 999: Player 2: Draw Summary Statistics: Number of draws: 343 Number of times scissors won: 235 Number of times rock won: 310 Number of times paper won: 112 words D English Inited Stat

Explanation / Answer

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);

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

do {

String comp = computer.play();

String you = human.play();

System.out.printf("%s vs. %s", 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)) ) {

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

} else {

assert (("Rock".equals(comp) && "Scissors".equals(you)) ||

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

("Paper".equals(comp) && "Rock".equals(you)));

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];

}

}

RandomComputerPlayer.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)];

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote