GameState
class represents the state of the game and serves as
the model for the application. It is implemented as a singleton to ensure
that there is only one instance of the game state throughout the
application's lifecycle. This class manages the core elements of the game,
such as the current word set and the number of guesses remaining.
The GameState
class is responsible for handling game logic related to
tracking and modifying the number of guesses available to the player. It
provides methods to increment, decrement, and reset the guess count.
This class also allows listeners to be added to monitor changes in the guess count, facilitating dynamic updates in views that depend on the game state.
The class is designed to be thread-safe through its use of the singleton
pattern and immutable fields where appropriate. However, it should be noted
that once the GameState
has been created, it cannot be recreated or
modified externally, ensuring the integrity and consistency of the game state
during runtime.
Usage example:
GameState gameState = GameState.createGameState(wordSet, 5); int remainingGuesses = gameState.getGuessCount(); String correctWord = gameState.getCorrectWord();
Note: Attempting to retrieve the GameState
instance before it is
created will result in an IllegalStateException
.
- Author:
- Kheagen Haskins
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addGuessListener
(javafx.beans.value.ChangeListener<? super Number> listener) Adds a listener to the guess count property.static GameState
createGameState
(WordSet wordSet, int guesses) Creates the singleton instance ofGameState
with the providedWordSet
and starting number of guesses.void
Decreases the current guess count by one.Returns the correct word that the player needs to guess.static GameState
Retrieves the singleton instance ofGameState
.int
Retrieves the current number of guesses remaining for the player.Retrieves theWordSet
currently used in this game session.void
Increases the current guess count by one.void
Resets the guess count to the initial starting value.void
setGuessCount
(int guessCount) Sets the current number of guesses remaining for the player.
-
Constructor Details
-
GameState
Constructs a newGameState
object with the specified word set and starting number of guesses. The game state is initialised with the providedWordSet
, and the number of guesses is set according to thestartingGuesses
parameter. Additionally, the color palette is set to the defaultFXPalettes#GREEN
.- Parameters:
wordSet
- theWordSet
used in this game session, containing the possible words and the correct word to guessstartingGuesses
- the initial number of guesses available to the player; this value sets the baseline for the guess counter
-
-
Method Details
-
getWordSet
Retrieves theWordSet
currently used in this game session. TheWordSet
contains the collection of possible words and the correct word that the player needs to guess.- Returns:
- the current
WordSet
for this game
-
getCorrectWord
Returns the correct word that the player needs to guess. This is a convenience method that delegates the call toWordSet.getCorrectWord()
.- Returns:
- the correct word to be guessed in this game session
-
getGuessCount
public int getGuessCount()Retrieves the current number of guesses remaining for the player. This value reflects the state of the game, showing how many more attempts the player has to correctly guess the word.- Returns:
- the current guess count
-
setGuessCount
public void setGuessCount(int guessCount) Sets the current number of guesses remaining for the player. The guess count is constrained between 0 and the starting guess count to ensure that it does not exceed the initial number of guesses or drop below zero.- Parameters:
guessCount
- the new guess count, which will be clamped between 0 and thestartingGuessCount
-
decrementGuesses
public void decrementGuesses()Decreases the current guess count by one. The guess count will not be reduced below zero. This method is typically called when the player makes an incorrect guess. -
incrementGuesses
public void incrementGuesses()Increases the current guess count by one. The guess count will not be increased above the original starting value. This method is typically called when the player triggers a special symbol set. -
resetGuesses
public void resetGuesses()Resets the guess count to the initial starting value. This method can be used to restart the game or to reset the player's attempts after a win or loss. -
addGuessListener
Adds a listener to the guess count property. The listener will be notified whenever the guess count changes, allowing for dynamic updates in views or other components that depend on this value.- Parameters:
listener
- theChangeListener
to be added to the guess count property; it will be triggered whenever the guess count changes
-
getGameState
Retrieves the singleton instance ofGameState
. This method will throw anIllegalStateException
if theGameState
has not yet been created using thecreateGameState(WordSet, int)
method.- Returns:
- the singleton instance of
GameState
- Throws:
IllegalStateException
- if theGameState
has not been created yet
-
createGameState
Creates the singleton instance ofGameState
with the providedWordSet
and starting number of guesses. This method can only be called once during the application's lifecycle; subsequent calls will result in anIllegalStateException
.Once the
GameState
is created, it can be retrieved via thegetGameState()
method.- Parameters:
wordSet
- theWordSet
used in this game sessionguesses
- the initial number of guesses available to the player- Returns:
- the newly created singleton instance of
GameState
- Throws:
IllegalStateException
- if theGameState
has already been created
-