/*
 * 2004 Utah High School Programming Contest, University of Utah
 * Take-Home Problem
 *
 * Dictionary.java
 *
 * This file contains the implementation of the Dictionary class.
 */

/**
 * A Dictionary represents a collection of known English words.  Dictionaries
 * are obviously useful for finding solutions to cryptograms: in this program,
 * Dictionaries are used by Puzzle objects in order to evaluate the quality of
 * possible solution ciphers.  The primary method of a Dictionary is `lookup',
 * which searches a dictionary for a given word.
 */
class Dictionary {
    /**
     * The words in a Dictionary, represented as an array of Strings.
     */
    private final String words[];
    
    /**
     * Construct a Dictionary from the given array of words.
     *
     * @param theWords    the array of English words
     */
    Dictionary(String theWords[]) {
        this.words = theWords;
    }
    
    /**
     * Look for a given word in the dictionary.  Return true if the word is in
     * the dictionary, and false if not.  Letter case is ignored in performing
     * the search.  For example, if "this" is in the dictionary, then this
     * method will return true for all of the strings "this", "This", "tHIS",
     * and "THIS".
     *
     * @param word    the String containing the word to be looked up
     * @return        true if the word is in the dictionary, false if not
     */
    public boolean lookup(String word) {
        // TODO: Fix linear searching.
        //
        // The code below implements a linear search of the `words' array,
        // which is not very fast or efficient.  You may want to improve this
        // code (and the Dictionary's data structures as well) to make word
        // lookups run much faster.
        //
        for (int i = 0; i < this.words.length; ++i) {
            if (this.words[i].equalsIgnoreCase(word)) {
                return true;
            }
        }
        return false;
    }
}

// End of file.

