Open Simonefleek opened 6 months ago
import java.util.Random; import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
Random randGen = new Random();
// Step 0: Set up constants and seed random number generator
final int ROCK = 0;
final int PAPER = 1;
final int SCISSORS = 2;
int seedVal = scnr.nextInt(); // Read seed value for the Random object
randGen.setSeed(seedVal); // Set seed for random object
// Step 1: Read player names and number of rounds
String player1 = scnr.next();
String player2 = scnr.next();
int rounds = scnr.nextInt();
while (rounds < 1) { // Continue reading number of rounds if value is below 1
System.out.println("Rounds must be > 0");
rounds = scnr.nextInt();
}
System.out.println(player1 + " vs " + player2 + " for " + rounds + " rounds");
int player1Wins = 0;
int player2Wins = 0;
// Step 4: Loop for the number of rounds
for (int round = 0; round < rounds; ++round) {
// Step 2: Generate random values for players and handle ties
int player1Choice;
int player2Choice;
do {
player1Choice = randGen.nextInt(3); // Generate random value for player 1
player2Choice = randGen.nextInt(3); // Generate random value for player 2
if (player1Choice == player2Choice) { // Check for a tie
System.out.println("Tie");
}
} while (player1Choice == player2Choice); // Continue if it's a tie
// Step 3: Identify winner for this round and output a message
if ((player1Choice == ROCK && player2Choice == SCISSORS)
|| (player1Choice == SCISSORS && player2Choice == PAPER)
|| (player1Choice == PAPER && player2Choice == ROCK)) {
System.out.println(player1 + " wins with " + getName(player1Choice));
player1Wins++;
} else {
System.out.println(player2 + " wins with " + getName(player2Choice));
player2Wins++;
}
}
// Output total wins for each player after all rounds are complete
System.out.println(player1 + " wins " + player1Wins + " and " + player2 + " wins " + player2Wins);
scnr.close(); // Close scanner to prevent resource leak
}
// Method to get the name of the hand signal based on the integer value
public static String getName(int choice) {
switch (choice) {
case 0:
return "rock";
case 1:
return "paper";
case 2:
return "scissors";
default:
return "";
}
}
}
Program Specifications Write a program to play an automated game of Rock, Paper, Scissors. Two players make one of three hand signals at the same time. Hand signals represent a rock, a piece of paper, or a pair of scissors. Each combination results in a win for one of the players. Rock crushes scissors, paper covers rock, and scissors cut paper. A tie occurs if both players make the same signal. Use a random number generator of 0, 1, or 2 to represent the three signals.
Note: this program is designed for incremental development. Complete each step and submit for grading before starting the next step. Only a portion of tests pass after each step but confirm progress.
Step 0. Read starter template and do not change the provided code. Integer constants are defined for ROCK, PAPER, and SCISSORS. A Random object is created and a seed is read from input and passed to the Random object. This supports automated testing and creates predictable results that would otherwise be random.
Step 1 (2 pts). Read two player names from input (String). Read number of rounds from input. Continue reading number of rounds if value is below one and provide an error message. Output player names and number of rounds. Submit for grading to confirm 2 tests pass.
*Ex: If input is:Ex: If input is:
3 Anna Bert -3 -4 4
Sample output is:
Rounds must be > 0 Rounds must be > 0 Anna vs Bert for 4 rounds
Step 2 (2 pts). Generate random values (0 - 2) for player 1 followed by player 2 by calling rand.nextInt(3). Continue to generate random values for both players until both values do not match. Output "Tie" when the values match. Submit for grading to confirm 3 tests pass.
9 Anna Bert 1
Sample output is:
Anna vs Bert for 1 rounds Tie Tie
Step 3 (3 pts). Identify winner for this round and output a message. Rock crushes scissors, scissors cut paper, and paper covers rock. Submit for grading to confirm 6 tests pass. Ex: If input is:
17 Anna Bert 1 Sample output is:
Anna vs Bert for 1 rounds Tie Bert wins with scissors
Step 4 (3 pts). Add a loop to repeat steps 2 and 3 for the number of rounds. Output total wins for each player after all rounds are complete. Submit for grading to confirm all tests pass.
Ex: If input is:
82 Anna Bert 3
Sample output is:
Anna vs Bert for 3 rounds Anna wins with paper Anna wins with paper Tie Tie Anna wins with rock Anna wins 3 and Bert wins 0