// copyright Peter D. Chatterton 2000

Explanation of Ghost Program

This program is written as an exercise. It is a simple game where the computer and user take turns adding a letter to the end of a word so that whoever enters a complete word loses.
It is written for Java 1.1 and demonstrates TextField event handling and collection classes with enumeration.

Program Logic

DO Get a letter from the user, append to previous ones in the TextField.
DO
Get next dictionary word.
IF user's word is an exact match,
THEN exit and user loses.
IF word is a partial match,
THEN add next letter and compare with current dict. word.
IF match, we lose.
ELSE break to get next letter -- but don't increment dict. word.
IF word is less than dictionary word,
THEN it's a new word & user wins.
WHILE words in dictionary
IF got to here,
THEN add user's word to dictionary.
WHILE user doesn't break out.

Event Listeners

The main line (Ghost.java) has a TextListener which contains the logic of the program. It listens to each letter that is entered from a TextField. In addition, it is entered when the field is cleared internally, even though removeTextListener is called. An ActionListener is entered when the user has finished entering a (new) word or just pressed Enter on a message dialog.

Data Base

The data base is a collection of Word objects which implement the Comparable interface which requires compareTo(Comparable). These are stored in a NameSortedVector which extends SortedVector which is a Vector. (This is because I copied the structure from my Agency program which had two XXXSortedVector classes extending SortedVector.) The Words are explicitly created in the program (as specified in the req) but this can be modified by reading them from a file. Each new Word is placed in Comparable order by calling Comparable:compareTo() on the database.

Accessing the Data Base

Accessing the data base is done through a SortedEnumeration interface which implements Enumeration. This maintains a variable: index which is the last element of the collection and provides getIndex() and decrementIndex(). Also it over-rides nextElement() and hasMoreElements().
Zipped source code files.