-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseGameState.java
More file actions
53 lines (42 loc) · 1.73 KB
/
BaseGameState.java
File metadata and controls
53 lines (42 loc) · 1.73 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
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
// Base abstract class with template for managing game state,
// including functionality such as the word to guess, remaining
// letters, guessed letters, and current progress.
public abstract class BaseGameState {
protected final String wordToGuess;
protected final char[] displayedWord;
protected int remainingAttempts;
protected final Set<Character> guessedLetters;
// Initialises the game state
public BaseGameState(String wordToGuess, Difficulty difficulty) {
this.wordToGuess = wordToGuess.toUpperCase();
this.displayedWord = new char[wordToGuess.length()];
Arrays.fill(this.displayedWord, '_');
this.remainingAttempts = difficulty.getMaximumAttempts();
this.guessedLetters = new HashSet<>();
}
// CONTRACT: Processes and validates the user's input - must be implemented
// by any subclasses to update and alter game state accordingly.
public abstract void validateGuess(char guess);
// Checks to see if the user has successfully identified the word.
public boolean hasSuccessfullyTerminated() {
return new String(displayedWord).equals(wordToGuess);
}
// Checks to see if no more attempts are left.
public boolean isGameOver() {
return remainingAttempts <= 0;
}
// Returns the current state of the identified fragment of the word.
public String getDisplayedWord() {
return new String(displayedWord);
}
public int getRemainingAttempts() {
return remainingAttempts;
}
// Returns the set of letters that have been guessed so far.
public Set<Character> getGuessedCharacters() {
return guessedLetters;
}
}