-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLetterBoxed.java
More file actions
289 lines (253 loc) · 9.2 KB
/
LetterBoxed.java
File metadata and controls
289 lines (253 loc) · 9.2 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* LetterBoxed - represents the state of a Letter Boxed puzzle,
* and solves it using recursive backtracking.
*
*/
import java.util.*;
public class LetterBoxed {
public static final int MOST_WORDS = 10;
public static final String WORDS_FILE = "word_list.txt";
public static final Dictionary dictionary = new Dictionary(WORDS_FILE);
// The sides of the puzzle, given as four strings of length 3.
private String[] sides;
// The individual letters in the puzzle.
// Each element of this array is a single-character string.
private String[] letters;
// The words in the solution.
private String[] words;
/*
* Constructor for a puzzle with the specified sides, where each
* side is a string containing the 3 letters from one side of the square.
*/
public LetterBoxed(String[] sides) {
if (sides == null || sides.length != 4) {
throw new IllegalArgumentException(
"parameter must be an array of 4 strings");
}
this.sides = sides;
this.letters = new String[12];
int letterNum = 0;
for (int i = 0; i < sides.length; i++) {
if (sides[i] == null || sides[i].length() != 3) {
throw new IllegalArgumentException(
"invalid side string: " + sides[i]);
}
for (int j = 0; j < 3; j++) {
this.letters[letterNum] = this.sides[i].substring(j, j+1);
letterNum++;
}
}
this.words = new String[MOST_WORDS];
for (int i = 0; i < this.words.length; i++) {
this.words[i] = "";
}
}
/*
* Returns a string that shows the letters of the puzzle in "square" form.
* For example, if the sides are {"cat", "dog", "fry", "pie"}, it will
* return a string that when printed looks like this:
* c a t
* d f
* o r
* g y
* p i e
*/
public String toString() {
String s = "";
// top of the square (i.e., sides[0])
for (int i = 0; i < 3; i++) {
s += " " + this.sides[0].charAt(i);
}
s += "\n";
// left and right sides (sides[1] and sides[2])
for (int i = 0; i < 3; i++) {
s += this.sides[1].charAt(i);
s += " " + this.sides[2].charAt(i);
s += "\n";
}
// bottom of the square (i.e., sides[3])
for (int i = 0; i < 3; i++) {
s += " " + this.sides[3].charAt(i);
}
s += "\n";
return s;
}
/*
* lastLetter - returns a single-character string containing
* the last letter in the specified word
*
* assumes that word is a String with at least one character
*/
private static String lastLetter(String word) {
return word.substring(word.length() - 1);
}
/*
* removeLast - returns the string that is formed by removing
* the last character of the specified word
*
* assumes that word is a String with at least one character
*/
private static String removeLast(String word) {
return word.substring(0, word.length() - 1);
}
/*
* addLetter - adds the specified letter as the next letter
* in the word at position wordNum in the solution
* and also updates the solnString accordingly
*/
private void addLetter(String letter, int wordNum) {
this.words[wordNum] += letter;
}
/*
* removeLetter - removes the specified letter from the end of
* the word at position wordNum in the solution
* and also updates the solnString accordingly
*/
private void removeLetter(int wordNum) {
this.words[wordNum] = this.removeLast(this.words[wordNum]);
}
/*
* alreadyUsed - returns true if the specified word is already
* one of the words in the solution, and false otherwise.
*/
private boolean alreadyUsed(String word) {
for (String w : this.words) {
if (w.equals(word)) {
return true;
}
}
return false;
}
/*
* onSameSide - returns true if the single-character strings
* letter1 and letter2 come from the same side of the puzzle,
* and false otherwise
*/
private boolean onSameSide(String letter1, String letter2) {
for (String side : this.sides) {
if (side.contains(letter1) && side.contains(letter2)) {
return true;
}
}
return false;
}
/*
* allLettersUsed - returns true if all of the letters in the puzzle
* are currently being used in the solution to the puzzle,
* and false otherwise
*/
private boolean allLettersUsed() {
for (String letter : this.letters) {
boolean anyWordHasLetter = false;
for (String w : this.words) {
if (w.contains(letter)) {
anyWordHasLetter = true;
break;
}
}
if (!anyWordHasLetter) {
return false;
}
}
return true;
}
/*
* printSolution - prints the words in the soln array from
* position 0 up to and including position wordNum
*/
private void printSolution(int wordNum) {
for (int i = 0; i <= wordNum; i++) {
System.out.println(this.words[i]);
}
}
/*
* isValid - returns true if the specified letter (a one-character string)
* is a valid choice for the letter in position charNum of the word at
* position wordNum in the soln, and false otherwise.
*
* Since this is a private helper method, we assume that only
* appropriate values will be passed in.
* In particular, we assume that letter is one of the letters of the puzzle.
*/
private boolean isValid(String letter, int wordNum, int charNum) {
if (wordNum == 0 && charNum == 0) {
return true;
} else if (charNum == 0) {
if (lastLetter(this.words[wordNum - 1]).equals(letter)) {
return true;
}
} else {
String last = lastLetter(this.words[wordNum]);
String word = this.words[wordNum];
if (!onSameSide(last, letter) && dictionary.hasString(word + letter) && !alreadyUsed(word + letter)) {
return true;
}
}
return false;
}
/*
* solveRB - the key recursive backtracking method.
* Handles the process of adding one letter to the word at position
* wordNum as part of a solution with at most maxWords words.
* Returns true if a solution has been found, and false otherwise.
*
* Since this is a private helper method, we assume that only
* appropriate values will be passed in.
*/
private boolean solveRB(int wordNum, int charNum, int maxWords) {
if (this.allLettersUsed() && dictionary.hasFullWord(this.words[wordNum])) {
this.printSolution(wordNum);
return true;
} else if (wordNum >= maxWords) {
return false;
}
for (int i = 0; i < this.letters.length; i++) {
String letter = this.letters[i];
if (this.isValid(letter, wordNum, charNum)) {
this.addLetter(letter, wordNum);
if (this.solveRB(wordNum, charNum + 1, maxWords)) {
return true;
} else if (dictionary.hasFullWord(this.words[wordNum]) && this.words[wordNum].length() >= 3) {
if (this.solveRB(wordNum + 1, 0, maxWords)) {
return true;
}
}
this.removeLetter(wordNum);
}
}
return false;
}
/*
* solve - the method that the client calls to solve the puzzle.
* Serves as a wrapper method for solveRB(), which it repeatedly calls
* with a gradually increasing limit for the number of words in the solution.
*/
public void solve() {
int maxWords = 1;
while (maxWords <= MOST_WORDS) {
System.out.println("Looking for a solution of length "
+ maxWords + "...");
if (this.solveRB(0, 0, maxWords)) {
return;
}
maxWords++;
}
System.out.println("No solution found using up to "
+ MOST_WORDS + " words.");
}
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
/* get the sides of the square from the user */
String[] sides = new String[4];
String[] prompts = {"top side: ", "left side: ",
"right side: ", "bottom side: "};
for (int i = 0; i < 4; i++) {
System.out.print(prompts[i]);
sides[i] = console.nextLine();
}
LetterBoxed puzzle = new LetterBoxed(sides);
System.out.println("Here is the puzzle:");
System.out.println(puzzle);
puzzle.solve();
}
}