-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordSearchSolver.java
More file actions
257 lines (226 loc) · 10.4 KB
/
Copy pathWordSearchSolver.java
File metadata and controls
257 lines (226 loc) · 10.4 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
/**
* [WordSearch.java]
* Capitalizes words found in a given wordsearch
* @author Derek Chen
* @version 1.0 May 23, 2020
*/
import java.util.Scanner;
import java.io.File;
class WordSearchSolver{
public static boolean checkOutOfBond(int sideLength, int i, int j) {
if (i >= 0 && i < sideLength && j >= 0 && j < sideLength) {
return true;
} else {
return false;
}
}
public static boolean search(char[][] puzzle, String word, int[][] charIndex, String[] possibleOutput, int g, int count) {
boolean equal = true;
for(int i = 0; i<charIndex.length; i++){
for(int j = 0; j<2; j++){
int num1 = charIndex[i][0];
int num2 = charIndex[i][1];
if(puzzle[num1][num2] != word.charAt(i)){
equal = false;
}
if (i == charIndex.length-1 && j == 1 && equal == true){
return true;
}
}
}
char nextLetter = word.charAt(count + 1);
int i = charIndex[count][0];
int j = charIndex[count][1];
if (checkOutOfBond(puzzle.length, i - 1, j - 1) == true && puzzle[i - 1][j - 1] == nextLetter && possibleOutput[g].equals("upleft")) {
charIndex[count + 1][0] = i - 1;
charIndex[count + 1][1] = j - 1;
return search(puzzle, word, charIndex, possibleOutput, g, count + 1);
} else if (checkOutOfBond(puzzle.length, i - 1, j - 0) == true && puzzle[i - 1][j - 0] == nextLetter && possibleOutput[g].equals("up")) {
charIndex[count + 1][0] = i - 1;
charIndex[count + 1][1] = j - 0;
return search(puzzle, word, charIndex, possibleOutput, g, count + 1);
} else if (checkOutOfBond(puzzle.length, i - 1, j + 1) == true && puzzle[i - 1][j + 1] == nextLetter && possibleOutput[g].equals("upright")) {
charIndex[count + 1][0] = i - 1;
charIndex[count + 1][1] = j + 1;
return search(puzzle, word, charIndex, possibleOutput, g, count + 1);
} else if (checkOutOfBond(puzzle.length, i - 0, j - 1) == true && puzzle[i - 0][j - 1] == nextLetter && possibleOutput[g].equals("left")) {
charIndex[count + 1][0] = i - 0;
charIndex[count + 1][1] = j - 1;
return search(puzzle, word, charIndex, possibleOutput, g, count + 1);
} else if (checkOutOfBond(puzzle.length, i - 0, j + 1) == true && puzzle[i - 0][j + 1] == nextLetter && possibleOutput[g].equals("right")) {
charIndex[count + 1][0] = i - 0;
charIndex[count + 1][1] = j + 1;
return search(puzzle, word, charIndex, possibleOutput, g, count + 1);
} else if (checkOutOfBond(puzzle.length, i + 1, j - 1) == true && puzzle[i + 1][j - 1] == nextLetter && possibleOutput[g].equals("downleft")) {
charIndex[count + 1][0] = i + 1;
charIndex[count + 1][1] = j - 1;
return search(puzzle, word, charIndex, possibleOutput, g, count + 1);
} else if (checkOutOfBond(puzzle.length, i + 1, j - 0) == true && puzzle[i + 1][j - 0] == nextLetter && possibleOutput[g].equals("down")) {
charIndex[count + 1][0] = i + 1;
charIndex[count + 1][1] = j - 0;
return search(puzzle, word, charIndex, possibleOutput, g, count + 1);
} else if (checkOutOfBond(puzzle.length, i + 1, j + 1) == true && puzzle[i + 1][j + 1] == nextLetter && possibleOutput[g].equals("downright")) {
charIndex[count + 1][0] = i + 1;
charIndex[count + 1][1] = j + 1;
return search(puzzle, word, charIndex, possibleOutput, g, count + 1);
} else {
return false;
}
}
public static int countPossible(char[][] puzzle, char nextLetter, int i, int j) {
int count = 0;
if (checkOutOfBond(puzzle.length, i - 1, j - 1) == true && puzzle[i - 1][j - 1] == nextLetter) {
count++;
}
if (checkOutOfBond(puzzle.length, i - 1, j - 0) == true && puzzle[i - 1][j - 0] == nextLetter) {
count++;
}
if (checkOutOfBond(puzzle.length, i - 1, j + 1) == true && puzzle[i - 1][j + 1] == nextLetter) {
count++;
}
if (checkOutOfBond(puzzle.length, i - 0, j - 1) == true && puzzle[i - 0][j - 1] == nextLetter) {
count++;
}
if (checkOutOfBond(puzzle.length, i - 0, j + 1) == true && puzzle[i - 0][j + 1] == nextLetter) {
count++;
}
if (checkOutOfBond(puzzle.length, i + 1, j - 1) == true && puzzle[i + 1][j - 1] == nextLetter) {
count++;
}
if (checkOutOfBond(puzzle.length, i + 1, j - 0) == true && puzzle[i + 1][j - 0] == nextLetter) {
count++;
}
if (checkOutOfBond(puzzle.length, i + 1, j + 1) == true && puzzle[i + 1][j + 1] == nextLetter) {
count++;
}
return count;
}
public static void findDirection(char[][] puzzle, char nextLetter, String[] possibleOutput, int i, int j) {
int c = 0;
if (checkOutOfBond(puzzle.length, i - 1, j - 1) == true && puzzle[i - 1][j - 1] == nextLetter) {
possibleOutput[c] = "upleft";
c++;
}
if (checkOutOfBond(puzzle.length, i - 1, j - 0) == true && puzzle[i - 1][j - 0] == nextLetter) {
possibleOutput[c] = "up";
c++;
}
if (checkOutOfBond(puzzle.length, i - 1, j + 1) == true && puzzle[i - 1][j + 1] == nextLetter) {
possibleOutput[c] = "upright";
c++;
}
if (checkOutOfBond(puzzle.length, i - 0, j - 1) == true && puzzle[i - 0][j - 1] == nextLetter) {
possibleOutput[c] = "left";
c++;
}
if (checkOutOfBond(puzzle.length, i - 0, j + 1) == true && puzzle[i - 0][j + 1] == nextLetter) {
possibleOutput[c] = "right";
c++;
}
if (checkOutOfBond(puzzle.length, i + 1, j - 1) == true && puzzle[i + 1][j - 1] == nextLetter) {
possibleOutput[c] = "downleft";
c++;
}
if (checkOutOfBond(puzzle.length, i + 1, j - 0) == true && puzzle[i + 1][j - 0] == nextLetter) {
possibleOutput[c] = "down";
c++;
}
if (checkOutOfBond(puzzle.length, i + 1, j + 1) == true && puzzle[i + 1][j + 1] == nextLetter) {
possibleOutput[c] = "downright";
c++;
}
}
//Begin Main
public static void main(String[] args) throws Exception {
File testCasesFile = new File("TestCases.txt");
Scanner input = new Scanner(testCasesFile);
//Checks to see if there is still text in the text file
while (input.hasNextLine()) {
//Stores the number of words in the word bank into a variable
int numWords = input.nextInt();
input.nextLine();
//Creates an array for the puzzle's word bank
String[] wordBank = new String[numWords];
for (int i = 0; i < numWords; i++) {
wordBank[i] = input.nextLine();
}
//Find the length of the first line in the wordsearch
String firstLine = input.nextLine();
firstLine = firstLine.replaceAll(" ", ""); //Removes spaces from the first line
int sideLength = firstLine.length();
//Creates two new variables based on the length of the puzzle, an original, which holds the word search in all lowercase, and a duplicate, which displays the words with capitalized letters
char[][] puzzle = new char[sideLength][sideLength]; //This array is the "original" word search and stores the puzzle in all lowercase
char[][] puzzleAnswer = new char[sideLength][sideLength]; //This array is a duplicate of the original, and is later modified to display the puzzle with capitalized words. This helps with words that overlap because the words checked are only checked in lowercase
//Copies the first line of the puzzle
for (int i = 0; i < sideLength; i++) {
puzzle[0][i] = firstLine.charAt(i);
}
//Copies the other lines of the puzzle into the array
int puzzleRow = 1; //Starts on the second row of the array because the first row was already copied
int puzzleColumn = 0;
int limiter = 0; //Limiter to prevent the other lines from becoming too large
while (limiter != ((sideLength - 1) * sideLength)) {
if (puzzleColumn == sideLength) {
puzzleColumn = 0;
puzzleRow++;
}
puzzle[puzzleRow][puzzleColumn] = input.next().charAt(0); //Copies each letter of the array (takes the next character, so skips spacees)
puzzleColumn++;
limiter++;
}
//Copies the original array into the duplicate array
for (int c = 0; c < sideLength; c++) {
for (int v = 0; v < sideLength; v++) {
puzzleAnswer[c][v] = puzzle[c][v];
}
}
//The meat of the code: this for loop calls for 3 methods to find the words in the word search
for (int i = 0; i < numWords; i++) {
String wordToSearch = wordBank[i]; //Variable that takes each word the code is trying to find
int[][] charIndex = new int[wordToSearch.length()][2]; //2d array with x, 2 length???
boolean wordFound = false;
//These two variables are basically a for loop in a for loop
int a = 0; //Column variable
int b = 0; //Row variable
while (wordFound == false) {
if (a == sideLength) { //Checks to see if the column goes out of bounds (basically the limiter in a for loop)
a = 0;
}
if (wordToSearch.charAt(0) == puzzle[a][b]) {
char nextLetter = wordToSearch.charAt(1);
int numberOfOutput = countPossible(puzzle, nextLetter, a, b);
String[] possibleOutput = new String[numberOfOutput];
findDirection(puzzle, nextLetter, possibleOutput, a, b);
charIndex[0][0] = a;
charIndex[0][1] = b;
for (int g = 0; g < numberOfOutput; g++) {
if (wordFound == false) {
wordFound = search(puzzle, wordToSearch, charIndex, possibleOutput, g, 0);
}
}
}
if (b == sideLength - 1) {
a++;
b = 0;
} else {
b++;
}
}
// Capitalizes words found in the word search
for (int x = 0; x < charIndex.length; x++) {
int index1 = charIndex[x][0];
int index2 = charIndex[x][1];
puzzleAnswer[index1][index2] = Character.toUpperCase(puzzleAnswer[index1][index2]);
}
}
//Prints the copied word search, with capitalized words
for (int k = 0; k < puzzle.length; k++) {
for (int u = 0; u < puzzle.length; u++) {
System.out.print(puzzleAnswer[k][u] + " ");
}
System.out.println();
}
input.nextLine();
} //End of checking for text while loop
} //END OF MAIN
}