-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordSearch.java
More file actions
executable file
·301 lines (270 loc) · 6.88 KB
/
Copy pathWordSearch.java
File metadata and controls
executable file
·301 lines (270 loc) · 6.88 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
290
291
292
293
294
295
296
297
298
299
300
301
//Cindy Lee Period 9
import java.util.*;
import java.io.*;
public class WordSearch {
private ArrayList <String> track;
private char [][] search;
public WordSearch () {
search = new char [10][10];
track = new ArrayList <String> ();
}
public WordSearch (int rows, int cols) {
search = new char [rows] [cols];
}
public String toString () {
String s = "";
for (int i = 0; i < search.length; i++) {
for (int j= 0; j< search[i].length; j++) {
if (search [i][j] == '\0') {
s += " - ";
}
else{
s += ((Character)search [i][j]).toString() + " ";
}
}
s+= "\n";
}
s += "\n WORDS IN SEARCH: \n";
for (int i = 0; i < track.size(); i++ ) {
s += track.get (i) + "\n";
}
return s;
}
public boolean addWordH (int row, int col, String s) {
if ((row >= 0 && row < search.length) && (col >= 0 && col < search[row].length)) {
if (search[row].length - col >= s.length () ) {
int i = 0;
int clear = col;
String u = s.toUpperCase ();
while (i < s.length ()) {
if (search[row][col] != '\0') {
if (search[row][col] != u.charAt (i)) {
return false;
}
else {
col++;
i++;
}
}
else {
col++;
i++;
}
}
int j = 0;
while (j < s.length ()){
if (search[row][clear] == '\0') {
search[row][clear] = u.charAt (j);
clear += 1;
j++;
}
else {
clear +=1;
j++;
}
}
track.add (u);
return true;
}
else {
return false;
}
}
else {
return false;
}
}
public boolean addWordV (int row, int col, String s) {
if ((row >=0 && row < search.length) && (col >= 0 && col < search[row].length)) {
if (search.length - row >= s.length()) {
int i = 0;
int clear = row;
String u = s.toUpperCase();
while (i < s.length ()) {
if (search[row][col] != '\0') {
if (search[row][col] != u.charAt (i)) {
return false;
}
else {
row++;
i++;
}
}
else {
row++;
i++;
}
}
int j = 0;
while (j < s.length ()){
if (search[clear][col] == '\0') {
search[clear][col] = u.charAt (j);
clear += 1;
j++;
}
else {
clear +=1;
j++;
}
}
track.add (u);
return true;
}
else {
return false ;
}
}
else {
return false;
}
}
public boolean addWordD (int row, int col, String s) {
if ((row >= 0 && col >=0) && (row < search.length && col < search[row].length)) {
if (search[row].length - col >= s.length () && search.length - row >= s.length() ){
int i = 0;
String u = s.toUpperCase ();
int clear = row;
int meh = col;
while (i < s.length ()) {
if (search[row][col] != '\0') {
if (search[row][col] != u.charAt (i)) {
return false;
}
else {
row++;
col++;
i++;
}
}
else {
row++;
col++;
i++;
}
}
int j = 0;
while (j < s.length ()){
if (search[clear][meh] == '\0') {
search[clear][meh] = u.charAt (j);
clear += 1;
meh+=1;
j++;
}
else {
clear +=1;
meh++;
j++;
}
}
track.add (u);
return true;
}
else {
return false;
}
}
else {
return false;
}
}
public void fillGrid () {
Random randy = new Random ();
String alpha = "ABCDEFGHIJKLMNOPQRSTUVXWYZ";
for (int r = 0; r < search.length; r++) {
for (int c = 0; c < search[r].length; c++) {
if (search[r][c] == '\0') {
int n = randy.nextInt (alpha.length ());
search [r][c] = alpha.charAt (n);
}
}
}
}
public ArrayList<String> loadDictionary() {
String s = "zzz";
ArrayList<String> dictionary = new ArrayList<String>();
try {
FileReader f = new FileReader("wordlist.txt");
BufferedReader b = new BufferedReader(f);
while( s != null ) {
s = b.readLine();
if ( s != null )
dictionary.add(s);
}
}
catch (IOException e) {}
return dictionary;
}
public static void main (String [] args) {
WordSearch ws = new WordSearch();
ArrayList <String> dictionary = new ArrayList <String> ();
dictionary = ws.loadDictionary ();
int what = 0;
Random rand = new Random ();
Random cand = new Random ();
Random numb = new Random ();
while (what < dictionary.size()) {
int number = numb.nextInt (dictionary.size());
int randy = rand.nextInt (10);
int candy = rand.nextInt (10);
boolean hori = ws.addWordH (randy,candy, dictionary.get(number));
if (hori == true) {
dictionary.remove (number);
}
else {
boolean verti = ws.addWordV (randy,candy, dictionary.get(number));
if (verti == true) {
dictionary.remove (number);
}
else {
boolean diag = ws.addWordD (randy,candy, dictionary.get(number));
if (diag == true) {
dictionary.remove (diag);
}
else {
what++;
}
}
}
}
/*Check
//working horizontal words
ws.addWordH(0, 0, "hello");
ws.addWordH(2, 4, "batman");
ws.addWordH(5, 1, "apple");
//Horizontal index error checking
ws.addWordH(-2, 4, "joker");
ws.addWordH(10, 4, "unicorn");
ws.addWordH(3, -1, "cowboys");
ws.addWordH(5, 8, "dogs");
//horizontal collision checking
ws.addWordH(5, 3, "plow");
ws.addWordH(2, 0, "neato");
//working vertical words
ws.addWordV(1, 0, "nice");
ws.addWordV(4, 9, "yankee");
ws.addWordV(4, 4, "old");
//Verical index error checking
ws.addWordV(-2, 4, "joker");
ws.addWordV(7, 4, "unicorn");
ws.addWordV(3, -1, "cowboys");
ws.addWordV(5, 20, "dogs");
//vertical collision checking
ws.addWordV(0, 4, "ores");
ws.addWordV(4, 9, "goober");
//working diagonal words
ws.addWordD(7, 0, "cat");
ws.addWordD(0, 0, "home");
ws.addWordD(0, 3, "loam");
/*
//Diagonal index error checking
ws.addWordD(-2, 0, "cat");
ws.addWordD(3, -1, "whelm");
ws.addWordD(7, 7, "after");
//Diagonal collision checking
ws.addWordD(0, 4, "ores");
ws.addWordD(4, 4, "oats");*/
System.out.println(ws);
ws.fillGrid();
System.out.println(ws);
}
}