-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordSearch.java
347 lines (275 loc) · 9.85 KB
/
WordSearch.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/**
sofia escoto
makes a word search grid that can be printed out and/or put into a txt file
gives whatever words given
*/
import java.util.*; //array list, random, etc
public class WordSearch {
private char[][] grid;
private ArrayList<String> wordList;
private int maxWordSize = 0;
private String title = "Word Search"; //can be changed later
//minsize and maxsize of grid
private final int MINSIZE = 10;
private final int MAXSIZE = 25;
private final char DELIM = '.'; //marks an empty space in the grid
private final int MAXTRIES = 25; //number of times try to place a word before you give up
//filling in the grid
private String charsToFill = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//private String charsToFill = "0123456789";
private char[] ctf = charsToFill.toCharArray();
//constructor methods
WordSearch(int size) {
grid = newGrid(size);
wordList = new ArrayList<String>();
}
WordSearch() {
grid = newGrid(MINSIZE);
wordList = new ArrayList<String>();
}
WordSearch(int size, String[] words) {
grid = newGrid(size);
wordList = new ArrayList<String>();
setWordList(words);
}
WordSearch(String[] words) {
grid = newGrid(MINSIZE);
wordList = new ArrayList<String>();
setWordList(words);
}
//enter an entire list of words
public void setWordList(String[] words) {
//set initial word list
for (String w : words) {
wordList.add(w.toUpperCase());
//if word is longer than all the ones before, change maxWordSize
if (w.length() > maxWordSize) {
maxWordSize = w.length();
}
}
resize();
}
//add words one by one
public void addToWordList(String newWord) {
wordList.add(newWord.toUpperCase());
//check if need to change maxWordSize, if so resize
if (newWord.length() > maxWordSize) {
maxWordSize = newWord.length();
resize();
}
}
public void setTitle(String newTitle) {
title = newTitle;
}
/**
choose a random spot for word, choose direction, check if word fits there. if it does, place it and
remove from the wordList, if not try again (up to x times)
if x times have passed, write a little warning to the user that the word wasnt put into the crossword
and tell them to expand the grid or put fewer words in next time
place word gives a boolean - true if placed, false if not placed
*/
public void setSearchGrid() {
int numTries;
boolean placed;
Random rand = new Random();
ArrayList<String> unUsedWords = new ArrayList<String>();
//place words
for (String word : wordList) {
//try to place words
numTries = 0;
placed = false;
while (numTries < MAXTRIES & !placed) {
numTries++;
placed = placeWord(word);
}
//if it passes that and still isn't placed, tell the user and remove the word from the list
if (!placed) {
System.out.println("The word " + word + " could not be placed in the search grid.");
System.out.println("Please increase the size of the grid or use fewer words in the list.");
//wordList.removeIf(w -> w.equalsIgnoreCase(word));
//add to list of words to remove and remove outside of for loop
unUsedWords.add(word);
}
}
//any words that weren't used, take out of wordlist
for (String w : unUsedWords) {
wordList.remove(w);
}
//fill rest of spots up with random characters
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
if (grid[r][c] == DELIM) {
int val = rand.nextInt(ctf.length);
grid[r][c] = ctf[val];
}
}
}
}
public boolean placeWord(String word) {
//choose a random place for the word, see if it fits, if it does place it, if not return false
boolean placed = false;
Random rand = new Random();
int row = rand.nextInt(grid.length);
int col = rand.nextInt(grid[0].length);
String direction = getDirection();
//get word without any spaces
word = word.replace(" ", "");
if (wordFits(word, row, col, direction)) {
place(word, row, col, direction);
placed = true;
}
return placed;
}
private void place(String word, int row, int col, String direction) {
//places the word in the space and direction on the grid
//recursive method
//base case = do nothing, word length is 0 then all letters have been placed
//non-base case = place the first letter and then place the rest of the word
if (word.length() > 0) {
grid[row][col] = word.charAt(0);
String newWord = word.substring(1);
int newRow = getNextRow(row, direction);
int newCol = getNextCol(col, direction);
place(newWord, newRow, newCol, direction);
}
}
public boolean wordFits(String word, int row, int col, String direction) {
//returns if the word fits in the space
//recursive method
boolean fits;
//base case -> length = 0, obv fits
if (word.length() == 0) {
fits = true;
}
else {
//get word without the first letter and check at the next spot
String newWord = word.substring(1);
int newRow = getNextRow(row, direction);
int newCol = getNextCol(col, direction);
fits = charFits(word.charAt(0), row, col) & wordFits(newWord, newRow, newCol, direction);
}
return fits;
}
private boolean charFits(char letter, int row, int col) {
boolean fits;
//check that the row and column are valid, if not return false
if (row >= grid.length | row < 0) {
fits = false;
}
else if (col >= grid[row].length | col < 0) {
fits = false;
}
// then check if the space is either DELIM or the char given
else {
fits = (grid[row][col] == DELIM | grid[row][col] == letter);
}
return fits;
}
//returns string of char grid (formatted)
@Override
public String toString() {
int width = grid.length * 3 + 2; //edit later when working with real words
//print title
String mssg = "\t\t\t\t" + title + "\n";
//print top line
mssg += "|";
for (int i = 0; i < width; i++) {
mssg += "-";
}
mssg += "|" + "\n";
//print all lines
for (int row = 0; row < grid.length; row++) {
mssg += "|" + " ";
for (int col = 0; col < grid[row].length; col++) {
mssg += grid[row][col] + " ";
}
mssg += "|" + "\n";
}
mssg += "|";
//print bottom line
for (int i = 0; i < width; i++) {
mssg += "-";
}
mssg += "|" + "\n";
//print word list
Collections.sort(wordList);
Iterator<String> iter = wordList.iterator();
int i = 0;
while (iter.hasNext()) {
i++;
mssg += iter.next();
if (i == 3) {
i = 0;
mssg += "\n";
}
else {
mssg += "\t\t\t";
}
}
return mssg;
}
//
//helper methods!
//
//makes the word search bigger if it's less than h x h where h = maxWordSize + 1/2 word list length
private void resize() {
if (grid.length < (maxWordSize + wordList.size()/2) & grid.length <= MAXSIZE) {
grid = newGrid(maxWordSize + (wordList.size())/2);
}
}
//creates the size x size word grid and fills with given delimeter to mark as empty
private char[][] newGrid(int size) {
if (size > MAXSIZE) size = MAXSIZE;
char[][] g = new char[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
g[i][j] = DELIM;
}
}
return g;
}
//returns the next row to go to given a direction
//no need for error messages, will check for this in wordFits() method
private int getNextRow(int currRow, String direction) {
int newRow;
if (direction.equals("l") | direction.equals("lu") | direction.equals("ld")) {
//if left
newRow = currRow - 1;
}
else if (direction.equals("u") | direction.equals("d")) {
//if up or down
newRow = currRow;
}
else {
//to right
newRow = currRow + 1;
}
return newRow;
}
//returns next col to go to given a direction
//no need for error messages, will check in wordFits()
private int getNextCol(int currCol, String direction) {
int newCol;
if (direction.equals("u") | direction.equals("lu") | direction.equals("ru")) {
//if up
newCol = currCol - 1;
}
else if (direction.equals("l") | direction.equals("r")) {
//if left or right
newCol = currCol;
}
else {
//down
newCol = currCol + 1;
}
return newCol;
}
//returns a random direction for the word to go in the crossword
//left, right, up, down, left up, left down, right up, right down (8 options)
private String getDirection() {
Random rand = new Random();
int num = rand.nextInt(8);
String[] directions = {"l", "r", "u", "d", "lu", "ld", "ru", "rd"};
return directions[num];
}
}