-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.java
More file actions
28 lines (26 loc) · 1.26 KB
/
Input.java
File metadata and controls
28 lines (26 loc) · 1.26 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
// The Input class handles user input during the game.
// It extends the Handler abstract class utilise a shared Scanner.
// The class validates guesses and user input.
public class Input extends Handler {
public char getValidGuess(GameState state) {
while (true) {
// Prompts the user to enter a valid guess.
System.out.print("Enter a letter: ");
String input = scanner.nextLine().trim().toUpperCase();
// The input must be a single character.
if (input.length() != 1) {
System.out.println("Your input was invalid. Ensure that you enter a single letter.");
continue;
}
char guess = input.charAt(0);
// Checks to see if the character is actually a letter (and not a number, symbol etc.)
if (!Character.isLetter(guess)) {
System.out.println("Your input was invalid. Ensure that you enter a letter (A-Z).");
} else if (state.getGuessedCharacters().contains(guess)) { // Checks to see if the letter was already guessed.
System.out.println("You have already guessed that letter!");
} else { // i.e. the guess is valid.
return guess;
}
}
}
}