/*
@startuml
class SnakeEyes {
- MAX_ROLLS: int
- playAgain: boolean
+ main(args: String[]): void
}
class Die {
- MAX: int
- faceValue: int
+ Die()
+ roll(): int
+ getFaceValue(): int
+ setFaceValue(value: int): void
+ toString(): String
}
class Scanner {
+ nextLine(): String
+ nextInt(): int
+ close(): void
}
class Random {
+ nextInt(bound: int): int
}
' Dependencies
SnakeEyes ..> Scanner : uses
SnakeEyes ..> Random : uses
SnakeEyes --> Die : creates
Die ..> Random : uses
@enduml
*/
import java.util.Random;
import java.util.Scanner;
public class SnakeEyes {
private static final int MAX_ROLLS = 500;
public static void main(String[] args) {
boolean playAgain = true;
System.out.println("🎲 Welcome to the Snake Eyes Game! 🎲");
System.out.println("Try to roll two ones! You have a total of " + MAX_ROLLS + " rolls.");
System.out.println("Enter a number of rolls (or type 'bye' to exit).");
try (Scanner scan = new Scanner(System.in)) {
while (playAgain) {
int remainingRolls = MAX_ROLLS;
int snakeEyesCount = 0;
while (remainingRolls > 0) {
System.out.print("\nEnter number of rolls (remaining: " + remainingRolls + "): ");
String input = scan.nextLine().trim();
if (input.equalsIgnoreCase("bye")) {
System.out.println("👋 Thanks for playing! See you next time.");
return;
}
int rolls;
try {
rolls = Integer.parseInt(input);
if (rolls <= 0) {
System.out.println("❌ Please enter a positive number.");
continue;
}
if (rolls > remainingRolls) {
System.out.println("⚠️ You only have " + remainingRolls + " rolls left!");
continue;
}
} catch (NumberFormatException e) {
System.out.println("❌ Invalid input. Please enter a valid number or 'bye' to exit.");
continue;
}
Die die1 = new Die();
Die die2 = new Die();
int num1, num2;
for (int i = 0; i < rolls; i++) {
num1 = die1.roll();
num2 = die2.roll();
if (num1 == 1 && num2 == 1) {
snakeEyesCount++;
}
}
remainingRolls -= rolls;
System.out.println("🎲 You rolled " + rolls + " times.");
System.out.println("🐍 Snake Eyes count: " + snakeEyesCount);
System.out.println("📊 Hit rate: " + String.format("%.4f", (float) snakeEyesCount / (MAX_ROLLS - remainingRolls)));
if (remainingRolls == 0) {
System.out.println("\nGame over! 🎉");
System.out.println("Total Snake Eyes: " + snakeEyesCount);
System.out.println("Final Hit Rate: " + String.format("%.4f", (float) snakeEyesCount / MAX_ROLLS));
while (true) {
System.out.print("\nPlay again? (y/n): ");
String response = scan.nextLine().trim().toLowerCase();
// ✅ Updated: Use Java 14+ switch expression
switch (response) {
case "y" -> {
remainingRolls = MAX_ROLLS;
snakeEyesCount = 0;
}
case "n" -> {
System.out.println("👋 Thanks for playing! Goodbye!");
playAgain = false;
}
default -> System.out.println("❌ Invalid input. Please enter 'y' or 'n'.");
}
if (!playAgain || remainingRolls == MAX_ROLLS) {
break;
}
}
}
}
}
}
}
}
// Die class representing a six-sided die
class Die {
private static final int MAX = 6;
private int faceValue;
public Die() {
faceValue = 1;
}
public int roll() {
Random generator = new Random();
faceValue = generator.nextInt(MAX) + 1;
return faceValue;
}
public int getFaceValue() {
return faceValue;
}
public void setFaceValue(int value) {
if (value >= 1 && value <= MAX) {
faceValue = value;
}
}
@Override //
public String toString() {
return "Die: " + faceValue;
}
}