-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dictionary.java
executable file
·163 lines (145 loc) · 5.95 KB
/
Dictionary.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import java.util.*;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.lang.NullPointerException;
import javax.swing.JOptionPane;
/**
* This class creates a Dictionary by taking a file selected by the user,
* reading the file, and storing the words in a data structure
*
* @author (Jesse Nelson)
* @version (September 19, 2012 : Windows Vista(x32) Java 1.7)
*/
public class Dictionary {
/**
* Maximum word length in the dictionary
*/
static final int MAX_WORD_LENGTH = 29;
/**
* Minimum word length in the dictionary
*/
static final int MIN_WORD_LENGTH = 2;
/**
* Default Dictionary file
*/
static final String DEFAULT_DICTIONARY = "DefaultDictionary.txt";
/**
* Lowest character a word may have in Dictionary
*/
static final char LOWEST_CHARACTER = 'A';
/**
* Highest character a word may have in Dictionary
*/
static final char HIGHEST_CHARACTER = 'z';
private Scanner dictionaryFile;
private ArrayList<LinkedList<String>> words;
private Random r;
private LinkedList<String> candidates;
/**
* Default constructor to load default dictionary
*
* Initializes private instance variables, calls readFile(), and closes the file when writing to it is finished
*/
public Dictionary() throws FileNotFoundException {
words = new ArrayList<LinkedList<String>>();
r = new Random();
dictionaryFile = new Scanner(new FileReader(DEFAULT_DICTIONARY));
initializeDictionary();
readFile();
dictionaryFile.close();
}
/**
* Initializes private instance variables, calls readFile(), and closes the file when writing to it is finished
*
* @args File to be used as Dictionary
*/
public Dictionary(String file) throws FileNotFoundException {
words = new ArrayList<LinkedList<String>>();
r = new Random();
dictionaryFile = new Scanner(new FileReader(file));
initializeDictionary();
readFile();
dictionaryFile.close();
}
/**
* Reads file and stores Dictionary in an array of LinkedLists
*
* @return Returns the current Dictionary to the Game
*/
private Dictionary readFile() {
while(dictionaryFile.hasNext())
insert(dictionaryFile.nextLine());
return this;
}
/**
* Verifies the dictionary contains a word of the specifed
* length and if none are present, a new length is requested.
*
* Generates a random word to be returned to the game
*
* @args Size of word to be returned
*
* @return Random word from the dictionary of the specified size
*/
public String randWord(int wordSize) {
candidates = words.get(wordSize);
if(candidates.size() < 1 ) {
boolean flag = false;
do {
String s =(JOptionPane.showInputDialog("I apologize, but I can not " +
"seem to find any words of length " + wordSize
+ "\nPlease enter a new word length"));
if(s == null || s.length() < 1 || !Character.isDigit(s.charAt(0)) ||
Integer.parseInt(s) < Dictionary.MIN_WORD_LENGTH ||
Integer.parseInt(s) > Dictionary.MAX_WORD_LENGTH) {
JOptionPane.showMessageDialog(null, "You have not enterd a valid length.\n "
+ "Please enter a valid value in between "
+ Dictionary.MIN_WORD_LENGTH + " and " +
Dictionary.MAX_WORD_LENGTH,"ERROR", JOptionPane.ERROR_MESSAGE);
} else {
flag = true;
return randWord(Integer.parseInt(s));
}
} while(!flag);
}
return candidates.get(r.nextInt(candidates.size()));
}
/**
* @return Entire Dictionary
*/
public ArrayList<LinkedList<String>> returnDictionary () { // Added to use in tests
return this.words;
}
public LinkedList<String> allWordsOfLength(int wordSize) {
candidates = words.get(wordSize);
if(candidates.size() < 1 ) {
boolean flag = false;
do {
String s =(JOptionPane.showInputDialog("I apologize, but I can not " +
"seem to find any words of length " + wordSize
+ "\nPlease enter a new word length"));
if(s == null || s.length() < 1 || !Character.isDigit(s.charAt(0)) ||
Integer.parseInt(s) < Dictionary.MIN_WORD_LENGTH ||
Integer.parseInt(s) > Dictionary.MAX_WORD_LENGTH) {
JOptionPane.showMessageDialog(null, "You have not enterd a valid length.\n "
+ "Please enter a valid value in between "
+ Dictionary.MIN_WORD_LENGTH + " and " +
Dictionary.MAX_WORD_LENGTH,"ERROR", JOptionPane.ERROR_MESSAGE);
} else {
flag = true;
return allWordsOfLength(Integer.parseInt(s));
}
} while(!flag);
}
return candidates;
}
// Inserts current word from file into data structure
private void insert(String word) {
words.get(word.length()).add(word);
}
private void initializeDictionary() {
for(int i = 0; i <= MAX_WORD_LENGTH; i++ ) {
words.add(new LinkedList<String>());
}
}
}