-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpellingBee.java
More file actions
251 lines (233 loc) · 8.16 KB
/
Copy pathSpellingBee.java
File metadata and controls
251 lines (233 loc) · 8.16 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import edu.willamette.cs1.spellingbee.SpellingBeeGraphics;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class SpellingBee {
private static final String ENGLISH_DICTIONARY = "EnglishWords.txt";
private SpellingBeeGraphics sbg;
private String puzzleLetters = ""; //The 7 puzzle letters
private String[] dictionary = new String[0]; //All of the dictionary words
private int dictionarySize = 0; //Number of words in the dictionary
private String[] found = new String[0]; //Words which have been found
private int wordCount = 0; //Number of words which have been found
private int totalScore = 0; //Score from the words that have been found
private int totalWords = 0; //Total valid words that have been found
//Puts text boxes and buttons on screen
public void run() {
sbg = new SpellingBeeGraphics();
sbg.addField("Puzzle", (s) -> puzzleAction(s));
sbg.addField("Word", (s) -> wordAction(s));
sbg.addButton("Solve", (s) -> solveAction());
}
//Makes sure input is valid, gets the dictionary, and resets counters
private void puzzleAction(String s) {
if (contains7Letters(s) && areAllCharsLetters(s) && noDuplicateLetters(s)) {
try {
dictionary = readFile(ENGLISH_DICTIONARY);
dictionarySize = dictionary.length;
found = new String[dictionarySize];
puzzleLetters = s.toUpperCase();
sbg.setBeehiveLetters(puzzleLetters);
wordCount = 0;
totalScore = 0;
totalWords = 0;
sbg.clearWordList();
sbg.showMessage("");
} catch (IOException e) {
//If dictionary file is missing it just resets everything
sbg.showMessage("Dictionary file not found", Color.red);
dictionary = new String[0];
dictionarySize = 0;
found = new String[0];
puzzleLetters = "";
sbg.clearWordList();
sbg.setBeehiveLetters("");
}
} else {
sbg.showMessage("Invalid puzzle", Color.red);
}
}
//Makes sure word is valid, scores it, colors pangrams
private void wordAction(String s) {
sbg.showMessage("");
String upper = s.toUpperCase();
if (puzzleLetters.equals("")) {
sbg.showMessage("Enter a puzzle por favor", Color.red);
} else if (!isValidWord(s)) {
if (s.length() < 4) {
sbg.showMessage("Too short", Color.red);
} else if (!upper.contains(puzzleLetters.substring(0, 1))) {
sbg.showMessage("Needs center letter", Color.red);
} else {
sbg.showMessage("Needs letters only in beehive", Color.red);
}
} else if (!inDictionary(s)) {
sbg.showMessage("Not in Dictionary", Color.red);
} else if (found(upper)) {
sbg.showMessage("Already found word", Color.red);
} else {
int score = getScore(s);
Color color = Color.black;
if (pangram(s)) {
color = Color.blue;
}
sbg.addWord(s + " (" + score + ")", color);
found[wordCount] = upper;
wordCount++;
totalWords++;
totalScore += score;
sbg.showMessage(totalWords + " words; " + totalScore + " points", Color.BLACK);
sbg.setField("Word", "");
}
}
//checks if word is in the dictionary
private boolean inDictionary(String word) {
String upper = word.toUpperCase();
for (int i = 0; i < dictionarySize; i++) {
if (dictionary[i].equals(upper)) {
return true;
}
}
return false;
}
//checks if word has already been found
private boolean found(String upper) {
for (int i = 0; i < wordCount; i++) {
if (found[i].equals(upper)) {
return true;
}
}
return false;
}
//finds all the valid words in the dictionary using the button
private void solveAction() {
searchDictionary(dictionary);
}
//validation of length
private boolean contains7Letters(String word) {
if (word.length() == 7) {
return true;
} else {
return false;
}
}
//validation of letters only
private boolean areAllCharsLetters(String word) {
int letters = 0;
for (int i = 0; i < word.length(); i++) {
if ((word.charAt(i) >= 'A' && word.charAt(i) <= 'Z') || (word.charAt(i) >=
'a' && word.charAt(i) <= 'z')) {
letters++;
}
}
if (letters == word.length()) {
return true;
} else {
return false;
}
}
//validation of no duplicates
private boolean noDuplicateLetters(String word) {
int duplicates = 0;
for (int i = 0; i < word.length(); i++) {
for (int j = i + 1; j < word.length(); j++) {
if (word.charAt(i) == word.charAt(j)) {
duplicates++;
}
}
}
if (duplicates == 0) {
return true;
} else {
return false;
}
}
//makes sure guessed word has length above four, center letter, and letters in puzzle
private boolean isValidWord(String word) {
if (puzzleLetters.equals("")) {
return false;
}
if (word.length() < 4) {
return false;
}
String upperWord = word.toUpperCase();
String center = puzzleLetters.substring(0, 1);
if (!upperWord.contains(center)) {
return false;
}
for (int i = 0; i < upperWord.length(); i++) {
String letter = upperWord.substring(i, i + 1);
if (!puzzleLetters.contains(letter)) {
return false;
}
}
return true;
}
//finds valid words and scores them and displays them
private void searchDictionary(String[] dictionary) {
sbg.clearWordList();
totalScore = 0;
totalWords = 0;
for (int i = 0; i < dictionarySize; i++) {
if (isValidWord(dictionary[i])) {
int score = getScore(dictionary[i]);
Color color = Color.black;
if (pangram(dictionary[i])) {
color = Color.blue;
}
sbg.addWord(dictionary[i] + " (" + score + ")", color);
totalWords++;
totalScore += score;
}
}
sbg.showMessage(totalWords + " words; " + totalScore + " points", Color.black);
}
//scores the words based on length/pangram
private int getScore(String word) {
int score = 0;
if (word.length() == 4) {
score = 1;
} else {
score = word.length();
}
if (pangram(word)) {
score += 7;
}
return score;
}
//checks if word uses all 7 puzzle letters
private boolean pangram(String word) {
String upperWord = word.toUpperCase();
for (int i = 0; i < puzzleLetters.length(); i++) {
String letter = puzzleLetters.substring(i, i + 1);
if (!upperWord.contains(letter)) {
return false;
}
}
return true;
}
//reads dictionary to count lines and then get words
public static String[] readFile(String address) throws IOException {
File myFile = new File(address);
Scanner scan = new Scanner(myFile);
int count = 0;
while (scan.hasNextLine()) {
scan.nextLine();
count++;
}
scan.close();
String[] words = new String[count];
scan = new Scanner(myFile);
int i = 0;
while (scan.hasNextLine()) {
words[i] = scan.nextLine().toUpperCase();
i++;
}
scan.close();
return words;
}
public static void main(String[] args) {
new SpellingBee().run();
}
}