Class StaticWordSet

java.lang.Object
com.slinky.hackmaster.model.text.StaticWordSet
All Implemented Interfaces:
WordSet

public final class StaticWordSet extends Object implements WordSet
The StaticWordSet class is responsible for managing a set of words, providing functionality such as random selection, shuffling, and generating strings with random symbols inserted between words.

This class is designed to support word-based games or applications requiring randomisation and manipulation of word lists. It allows for shuffling words, selecting a random "correct" word, and creating a "jumbled" string of words interspersed with random symbols.

Example usage:

 WordSet beginnerList = WordBank.getWordSet(Difficulty.BEGINNER);
 String correctWord = wordSet.getCorrectWord();
 wordSet.shuffle();
 String jumbledString = wordSet.jumble(50);
 

This class is immutable once instantiated with a list of words. It uses a Fisher-Yates shuffle algorithm for randomisation and ensures that the symbols added between words are randomly selected from a predefined set of characters.

Version:
2.0
Author:
Kheagen Haskins
  • Constructor Details

    • StaticWordSet

      public StaticWordSet(JumbleStrategy strategy, String... listOfWords)
      Constructs a new WordSet with the specified words.

      The constructor initialises the wordList with the provided array of words and selects a random index to designate one of the words as the "correct" word. The WordSet is immutable in the sense that once the list of words is set, it cannot be modified. However, the order of words can be shuffled, and symbols can be added between them when generating strings.

      Throws an IllegalArgumentException if the provided word list is null or empty, as a non-empty list of words is required for the operations supported by this class.

      Parameters:
      strategy - An implementation of the JumbleStrategy interface
      listOfWords - the words to be managed by this WordSet. These words are used in all operations provided by the class.
      Throws:
      IllegalArgumentException - if the word list is null or empty.
  • Method Details

    • getTotalCharacters

      public int getTotalCharacters()
      Calculates and returns the total number of characters across all words in the word list.

      This method iterates through each word in the wordList and sums up their lengths to determine the total number of characters. This can be useful for operations where the cumulative length of the words is required, such as when determining the size of a jumbled string.

      Specified by:
      getTotalCharacters in interface WordSet
      Returns:
      the total number of characters in all words within this StaticWordSet.
    • getCorrectWord

      public String getCorrectWord()
      Retrieves the correct word from the word set.

      The "correct" word is the one selected randomly during the construction of this StaticWordSet instance. This word can be used as an answer or reference in word-based games or applications that require a randomly chosen word from the set.

      Specified by:
      getCorrectWord in interface WordSet
      Returns:
      the correct word as a String from the word set.
    • removeRandomDud

      public String removeRandomDud()
      Returns a word from the word set that is intentionally incorrect or invalid.

      The "dud" word is typically used in scenarios where an incorrect option is needed, such as in word-based games, puzzles, or tests where the player or user is required to identify or differentiate between correct and incorrect words. This method complements the getCorrectWord() method by providing an alternative that should not be chosen as the correct answer.

      Specified by:
      removeRandomDud in interface WordSet
      Returns:
      a word as a String that is not the correct answer or is intentionally incorrect.
    • removeDud

      public boolean removeDud(String dud)
      Removes a specified dud word from the list of words.
      Specified by:
      removeDud in interface WordSet
      Parameters:
      dud - the word to be removed from the list of potential duds. Must not be null or empty.
      Returns:
      true if the dud was successfully removed; false if the dud was not found in the list.
      Throws:
      IllegalArgumentException - if the provided dud is null or empty, or if the dud has already been removed.
    • addDudCountListener

      public void addDudCountListener(javafx.beans.value.ChangeListener<? super Number> cl)
      Adds a listener to monitor changes in the count of "dud" words.

      This method allows you to attach a ChangeListener that will be notified whenever the value of the dudCountProperty changes. This is particularly useful in scenarios where you need to react to changes in the number of incorrect or invalid words within the application, such as updating the user interface or triggering certain actions when the count changes.

      Specified by:
      addDudCountListener in interface WordSet
      Parameters:
      cl - the ChangeListener to add; it must be able to handle Number type values, as the listener will receive updates whenever the count changes.
    • shuffle

      public WordSet shuffle()
      Shuffles the words in the word list using the Fisher-Yates algorithm.

      This method implements the modern version of the Fisher-Yates shuffle algorithm, also known as the Knuth shuffle. It ensures each permutation of the array elements is equally probable. The method iterates through the array from the first element to the last, swapping each element with a randomly selected one from the range of unshuffled elements. By gradually reducing the range of the random selection, the algorithm prevents elements from being shuffled multiple times, which would compromise the uniformity of the shuffle.

      Specified by:
      shuffle in interface WordSet
      Returns:
      the StaticWordSet object with its words rearranged in a random order. The method modifies the original StaticWordSet object and returns the same reference for chaining or further manipulation.
    • jumble

      public String jumble(int size)
      Generates a string with the specified size, inserting random symbols between the words.

      The generated string includes each word from the list with random symbols added between them to reach the specified size.

      Specified by:
      jumble in interface WordSet
      Parameters:
      size - the desired size of the game string.
      Returns:
      the generated game string.