- All Implemented Interfaces:
WordSet
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 Summary
ConstructorsConstructorDescriptionStaticWordSet
(JumbleStrategy strategy, String... listOfWords) Constructs a newWordSet
with the specified words. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addDudCountListener
(javafx.beans.value.ChangeListener<? super Number> cl) Adds a listener to monitor changes in the count of "dud" words.Retrieves the correct word from the word set.int
Calculates and returns the total number of characters across all words in the word list.jumble
(int size) Generates a string with the specified size, inserting random symbols between the words.boolean
Removes a specified dud word from the list of words.Returns a word from the word set that is intentionally incorrect or invalid.shuffle()
Shuffles the words in the word list using the Fisher-Yates algorithm.
-
Constructor Details
-
StaticWordSet
Constructs a newWordSet
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. TheWordSet
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 isnull
or empty, as a non-empty list of words is required for the operations supported by this class.- Parameters:
strategy
- An implementation of theJumbleStrategy
interfacelistOfWords
- the words to be managed by thisWordSet
. These words are used in all operations provided by the class.- Throws:
IllegalArgumentException
- if the word list isnull
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 interfaceWordSet
- Returns:
- the total number of characters in all words within this
StaticWordSet
.
-
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 interfaceWordSet
- Returns:
- the correct word as a
String
from the word set.
-
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 interfaceWordSet
- Returns:
- a word as a
String
that is not the correct answer or is intentionally incorrect.
-
removeDud
Removes a specified dud word from the list of words.- Specified by:
removeDud
in interfaceWordSet
- 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
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 thedudCountProperty
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 interfaceWordSet
- Parameters:
cl
- theChangeListener
to add; it must be able to handleNumber
type values, as the listener will receive updates whenever the count changes.
-
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.
-
jumble
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.
-