Class GameState

java.lang.Object
com.slinky.hackmaster.model.GameState

public final class GameState extends Object
The 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 Details

    • GameState

      public GameState(WordSet wordSet)
      Constructs a new GameState object with the specified word set and starting number of guesses. The game state is initialised with the provided WordSet, and the number of guesses is set according to the startingGuesses parameter. Additionally, the color palette is set to the default FXPalettes#GREEN.
      Parameters:
      wordSet - the WordSet used in this game session, containing the possible words and the correct word to guess
      startingGuesses - the initial number of guesses available to the player; this value sets the baseline for the guess counter
  • Method Details

    • getWordSet

      public WordSet getWordSet()
      Retrieves the WordSet currently used in this game session. The WordSet contains the collection of possible words and the correct word that the player needs to guess.
      Returns:
      the current WordSet for this game
    • getCorrectWord

      public String getCorrectWord()
      Returns the correct word that the player needs to guess. This is a convenience method that delegates the call to WordSet.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 the startingGuessCount
    • 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

      public void addGuessListener(javafx.beans.value.ChangeListener<? super Number> listener)
      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 - the ChangeListener to be added to the guess count property; it will be triggered whenever the guess count changes
    • getGameState

      public static GameState getGameState()
      Retrieves the singleton instance of GameState. This method will throw an IllegalStateException if the GameState has not yet been created using the createGameState(WordSet, int) method.
      Returns:
      the singleton instance of GameState
      Throws:
      IllegalStateException - if the GameState has not been created yet
    • createGameState

      public static GameState createGameState(WordSet wordSet, int guesses)
      Creates the singleton instance of GameState with the provided WordSet and starting number of guesses. This method can only be called once during the application's lifecycle; subsequent calls will result in an IllegalStateException.

      Once the GameState is created, it can be retrieved via the getGameState() method.

      Parameters:
      wordSet - the WordSet used in this game session
      guesses - the initial number of guesses available to the player
      Returns:
      the newly created singleton instance of GameState
      Throws:
      IllegalStateException - if the GameState has already been created