/** Name: dictionary->Hangman Description: Contains dictionary file related methods Programmer: Spencer R. Shimko Date: 03-7-2003 Version: 0.01 **/ import java.io.*; import java.util.*; /** Class: dictionary Handles parsing and retrieving info from dictionary file **/ public class dictionary { private LinkedList word_l=new LinkedList(); // Dictionary wordlist private int count=0; //Number of words in dictionary file private Random rand = new Random(); // Random number generator /** setDict: Test dictionary file and creates linklist of words if it exists and passes validity checks Receives: String pointing to dictionary file Returns: True if dict. file passes verification False if dict file fails verification **/ public boolean setDict(String dict_s){ // setup dictionary file File dict_f = new File(dict_s); BufferedReader dict_fs; // if the arg isn't a legit file... if (! dict_f.isFile()){ System.out.println("Dictionary must exist and be a valid file!"); return false; } // endif not file // create input filestream for reading try{ dict_fs = new BufferedReader(new FileReader(dict_f)); } catch(IOException e){ System.out.println("Error opening dictionary file!"); return false; } // Get the word count and parse the string as an int try{ count=Integer.parseInt(dict_fs.readLine()); } catch(IOException e){ System.out.println("Error getting dictionary word count!"); return false; } //for loop assigns words to wordlist linkedlist try{ for (int x=0; x < count; x++){ // when we add it make it lowercase and trim whitespace // just in case word_l.add((dict_fs.readLine()).toLowerCase().trim()); } return true; // the wordlist has been created sucessfully } catch(IOException e){ System.out.println("Error reading in wordlist from dictionary!"); return false; } }// end setDict method /** getWord: gets random word from dictionary word list Receives: Returns: Random word as string from word list **/ public String getWord(){ return (String) word_l.get(rand.nextInt(count)); } // end getWord method } // end dictionary class