Repository navigation
Expand file tree
/
Copy pathInput.java
More file actions
100 lines (86 loc) · 3.32 KB
/
Copy pathInput.java
File metadata and controls
100 lines (86 loc) · 3.32 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
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
/*
* This file is used to keep all methods that are repeatedly used throughout the project.
* This is to prevent repetition of code and to keep things organized.
*/
import java.util.concurrent.TimeUnit;
public class Input {
public Scanner sc = new Scanner(System.in);
// This method is used to validate a choice input based on the number of choices there are
// method returns the choice # that was inputted after it has been validated:
public int ChoiceInput(int min, int max) {
boolean valid = false;
int choice = 0;
while (!valid) {
try {
System.out.println("\nEnter Choice:");
choice = sc.nextInt();
if (choice >= min && choice <= max) {
valid = true;
sc.nextLine();
} else {
System.out.println("That is not an option.");
}
} catch (InputMismatchException e) {
System.err.println("Invalid Input.");
sc.nextLine();
}
}
return choice;
}
// Method used to ask the player if they would wish to play again after every game:
public boolean PlayAgain() throws InterruptedException {
boolean playAgain = false;
System.out.println("\nChoose an option:");
System.out.println("1. Play Again");
System.out.println("2. Return to Main Menu");
System.out.println("3. Exit");
int choice = ChoiceInput(1, 3);
switch (choice) {
case 1 -> playAgain = true;
case 2 -> Main.Instance().MainMenu();
case 3 -> {
System.out.println("Goodbye!\n");
System.exit(0);
}
}
return playAgain;
}
public String DifficultyMenu() {
String difficulty;
boolean loop;
do {
loop = false;
System.out.println("\nChoose a difficulty:");
System.out.println("1. Easy");
System.out.println("2. Mid");
System.out.println("3. Hard");
difficulty = sc.nextLine();
if (!difficulty.equals("1") && !difficulty.equals("2") && !difficulty.equals("3")) {
System.out.println("\nInvalid Choice.");
loop = true;
}
} while (loop);
return difficulty;
}
// Generates random number:
public int GetRandomNum(int origin, int bound) {
Random ranNum = new Random();
return ranNum.nextInt(bound - origin + 1) + origin; // Bound and Origin are included in random num
}
public char GetRandomChar(char start, char end) {
return (char) GetRandomNum((int) 'A', (int) 'J');
}
// MESSAGE ** MUST ** BE A 3 CHARACTER STRING:
public void Delay(String message, int timeoutInMiliseconds) throws InterruptedException {
TimeUnit.MILLISECONDS.sleep(timeoutInMiliseconds / 2);
System.out.print(message.charAt(0));
TimeUnit.MILLISECONDS.sleep(timeoutInMiliseconds);
System.out.print(message.charAt(1));
TimeUnit.MILLISECONDS.sleep(timeoutInMiliseconds);
System.out.println(message.charAt(2));
TimeUnit.MILLISECONDS.sleep(timeoutInMiliseconds);
}
}