1 7 8 9 package hangman; 10 11 import java.io.FileReader ; 12 import java.io.IOException ; 13 import java.io.LineNumberReader ; 14 import java.io.Reader ; 15 import java.util.ArrayList ; 16 import java.util.Collections ; 17 import java.util.List ; 18 19 import Jmc.baseTools.*; 20 21 28 29 public class WordSource 30 { 31 private int _nextWord; 32 private List _words = new ArrayList (); 33 34 public WordSource() 35 { 36 readWords(); 37 } 38 39 private void readWords() 40 { 41 42 try 43 { 44 Reader r = new FileReader (base_environment.pcmf_getRootDir() + "WordList.txt"); 46 LineNumberReader lineReader = new LineNumberReader (r); 47 48 while (true) 49 { 50 String line = lineReader.readLine(); 51 52 if (line == null) 53 break; 54 55 if (line.startsWith("#")) 56 continue; 57 58 String word = line.trim().toLowerCase(); 59 60 if (word.length() == 0) 61 continue; 62 63 _words.add(word); 64 } 65 66 lineReader.close(); 67 } 68 catch (IOException ex) 69 { 70 throw new RuntimeException ("Unable to read list of words from file WordList.txt.", ex); 71 } 72 73 75 Collections.shuffle(_words); 76 77 } 78 79 85 86 public String nextWord() 87 { 88 if (_nextWord >= _words.size()) 89 { 90 _nextWord = 0; 91 Collections.shuffle(_words); 92 } 93 94 return (String ) _words.get(_nextWord++); 95 } 96 } | Popular Tags |