Repository navigation
Expand file tree
/
Copy pathDiceRoller.java
More file actions
110 lines (93 loc) · 3.89 KB
/
Copy pathDiceRoller.java
File metadata and controls
110 lines (93 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// A virtual dice roller program to roll dice with a specified number of sides.
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
public class DiceRoller {
// Sets of valid inputs for yes and no responses in portuguese and english
private static final Set<String> YES_OPTIONS = Set.of("y", "yes", "s", "sim");
private static final Set<String> NO_OPTIONS = Set.of("n", "no", "nop", "não");
public static void main(String[] args) {
// Using try with resources to ensure the scanner is closed automatically
try (Scanner scanner = new Scanner(System.in)) {
Random random = new Random();
// Main loop to roll dice
while (true) {
printDiceArtMenu();
int sides = readPositiveInt(scanner);
Dice dice = new Dice(sides, random);
int result = dice.roll();
System.out.println("You rolled a " + result + " on a " + sides + "-sided dice!!!");
printDiceArtFinal();
String input;
// Loop to get a valid response for rolling again
while (true) {
System.out.print("Do you want to roll again? (y/n): ");
input = scanner.next().trim().toLowerCase();
if (YES_OPTIONS.contains(input)) {
System.out.println("Great! Let's roll again.\n");
break; // sai desse loop e volta para o loop principal
} else if (NO_OPTIONS.contains(input)) {
System.out.println("Thank you for using the virtual Dice Roller :)\nGoodbye!");
return; // sai do main
} else {
System.out.println("Invalid input. Please type yes/y or no/n.");
}
}
}
}
}
// Prints the welcome menu with ASCII art
private static void printDiceArtMenu() {
System.out.println("""
############################################
Welcome to the virtual DICE roller!
############################################
_______.
______ | . . |\\
/ /\\ | . |.\\
/ ' / \\ | . . |.'|
/______/. . \\ |_______|.'|
\\ . . \\ / \\ ' . \\'|
\\ . . \\ / \\____'___\\|
\\______\\/
"""); // pritty asciis hihi :)
}
// Prints the final dice ASCII art
private static void printDiceArtFinal() {
System.out.println("""
\n .-------.
/ o /|
/_______/o|
| o | |
| o |o/
| o |/
'-------'
""");
}
// Reads a positive integer from the user
public static int readPositiveInt(Scanner scanner) {
int value;
while (true) {
System.out.print("Enter the number of sides for the dice: ");
if (scanner.hasNextInt()) {
value = scanner.nextInt();
if (value > 0) {
return value;
}
} else { scanner.next(); }
System.out.println("Invalid input. Please enter a number greater than 0.");
}
}
// Dice class representing a dice with a given number of sides
public static class Dice {
private final int sides;
private final Random random;
public Dice(int sides, Random random) {
this.sides = sides;
this.random = random;
}
public int roll() {
return random.nextInt(sides) + 1;
}
}
}