Class HangmanDictionary

java.lang.Object
  extended byHangmanDictionary
All Implemented Interfaces:
IHangmanDictionary

public class HangmanDictionary
extends java.lang.Object
implements IHangmanDictionary

A source of words for the game of Hangman. Words are taken from a textfile, or in case the file is not located or has no suitable words, a short internal list is generated. The method isValidWord(), together with constants defining minimum and maximum lengths, determine which words from the file are considered valid for the game.


Field Summary
static int MAX_WORD_LENGTH
          Longest allowable word.
static int MIN_WORD_LENGTH
          Shortest allowable word.
static java.lang.String WORD_DELIMITER
          Delimiter (regular expression) for separating words on the input file.
static java.lang.String WORD_MATCH_PATTERN
          Pattern (regular expression) used to extract candidate words on the input.
 
Constructor Summary
HangmanDictionary(java.lang.String fileID)
          Create a dictionary of Hangman words from a given file; if the file cannot be opened, a small built-in default set of words is used.
 
Method Summary
 java.lang.String chooseWord()
          Pick a word which hasn't been chosen recently.
static boolean isLegalWord(java.lang.String word)
          Determine if the given string is a valid word for the game.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MIN_WORD_LENGTH

public static final int MIN_WORD_LENGTH
Shortest allowable word.

See Also:
Constant Field Values

MAX_WORD_LENGTH

public static final int MAX_WORD_LENGTH
Longest allowable word.

See Also:
Constant Field Values

WORD_DELIMITER

public static final java.lang.String WORD_DELIMITER
Delimiter (regular expression) for separating words on the input file. Note that hyphens are not delimiters; words containing hyphens are completely ignored.

See Also:
Constant Field Values

WORD_MATCH_PATTERN

public static final java.lang.String WORD_MATCH_PATTERN
Pattern (regular expression) used to extract candidate words on the input. The pattern used matches strings of lower-case letters.

See Also:
Constant Field Values
Constructor Detail

HangmanDictionary

public HangmanDictionary(java.lang.String fileID)
Create a dictionary of Hangman words from a given file; if the file cannot be opened, a small built-in default set of words is used.
A DelimitedTextFile is used, which means that the file will be looked for in lots of places, and if not found the user will be prompted. The string given can be simple filename or a full or partial path or a full URL, local or remote. The file can have any extension and practically any contents. All strings of lowercase letters delimited by whitespace or by typical punctuation characters are candidate words. In brief, any reasonable text file can be used as a Hangman Dictionary.

Parameters:
fileID - identifier of a file containing suitable words for Hangman.
Method Detail

chooseWord

public java.lang.String chooseWord()
Pick a word which hasn't been chosen recently. The choice is made randomly. No choice will be repeated until all of the words have been used once.

Specified by:
chooseWord in interface IHangmanDictionary
Returns:
a word, or null if there are no words to choose from.
See Also:
IHangmanDictionary.chooseWord()

isLegalWord

public static boolean isLegalWord(java.lang.String word)
Determine if the given string is a valid word for the game. Valid strings have lengths within a certain limit and contain only letters. Words containing capital letters are taken to be proper nouns and are excluded.

Parameters:
word - any string
Returns:
true iff the word, without alteration, is valid for the game.