-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordGrid.java
214 lines (177 loc) · 5.77 KB
/
WordGrid.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
/*----------------------------------------------------------------
* Author: Maxim Kukhtenkov
* Written: 10/5/2014
* Last updated: 10/7/2014
*
* WordGrid Class for a Word Search Puzzle. Used to create a
* word grid object.
*
*----------------------------------------------------------------*/
public class WordGrid {
private int maxRowCount;
private int maxColCount;
private Letter letters[][];
WordGrid(int maxRowCount, int maxColCount) {
letters = new Letter[maxRowCount][maxColCount];
for (int y = 0; y < maxRowCount; y++) {
for (int x = 0; x < maxColCount; x++) {
int random = (int) (Math.random() * 26);
letters[y][x] = new Letter((char) ('A' + random), y, x);
}
}
this.maxRowCount = maxRowCount;
this.maxColCount = maxColCount;
}
private class Letter {
char value;
int yCoordinate;
int xCoordinate;
Letter[] neighbors;
// Initializes value of the letter and declares the neighbor array
Letter(char value, int yCoordinate, int xCoordinate) {
this.yCoordinate = yCoordinate;
this.xCoordinate = xCoordinate;
this.value = value;
neighbors = new Letter[8];
}
}
void displayGrid() {
System.out.println("-----------------\n Word Puzzle \n-----------------\n");
for (int y = 0; y < maxRowCount; y++) {
System.out.print(y + "\t");
for (int x = 0; x < maxColCount; x++) {
System.out.print(letters[y][x].value + " ");
}
System.out.println();
}
}
public void searchWord(String word) {
int startY = 0;
int startX = 0;
int endY = 0;
int endX = 0;
boolean wordFound = false;
for (int y = 0; y < maxRowCount && !wordFound; y++) {
for (int x = 0; x < maxColCount && !wordFound; x++) {
// If first letter is found
if (word.charAt(0) == letters[y][x].value) {
startY = y;
startX = x;
// Search all neighbors
for (int i = 0; i < 8 && !wordFound; i++) {
boolean nextLetterFound = true;
int count = 1;
Letter gridLetter = letters[y][x].neighbors[i];
while(count < word.length() && nextLetterFound && !wordFound && gridLetter != null) {
if (word.charAt(count) != gridLetter.value) {
nextLetterFound = false;
}
else if (count == word.length() - 1 && word.charAt(count) == gridLetter.value) {
wordFound = true;
endY = gridLetter.yCoordinate;
endX = gridLetter.xCoordinate;
}
else {
count++;
gridLetter = gridLetter.neighbors[i];
}
}
} // End-search all neighbors
} // End-if first letter is found
}
}
if (wordFound) {
System.out.println("(" + startX + ", " + startY + "), (" + endX + ", " + endY + ")");
}
else {
System.out.println("Not Found");
}
}
public void initializeNeighbors(boolean wrap) {
for (int y = 0; y < maxRowCount; y++) {
for (int x = 0; x < maxColCount; x++) {
populateNeighbors(y, x, wrap);
}
}
}
private void populateNeighbors(int y, int x, boolean wrap) {
int maxRow = maxRowCount - 1;
int maxCol = maxColCount - 1;
boolean northLimit = (y == 0);
boolean southLimit = (y == maxRow);
boolean westLimit = (x == 0);
boolean eastLimit = (x == maxCol);
// North-West
if (!northLimit && !westLimit)
letters[y][x].neighbors[0] = letters[y - 1][x - 1];
else if (!wrap)
letters[y][x].neighbors[0] = null;
else if (northLimit && westLimit)
letters[y][x].neighbors[0] = letters[maxRow][maxCol];
else if (northLimit)
letters[y][x].neighbors[0] = letters[maxRow][x - 1];
else if (westLimit)
letters[y][x].neighbors[0] = letters[y - 1][maxCol];
// North
if (!northLimit)
letters[y][x].neighbors[1] = letters[y - 1][x];
else if (!wrap)
letters[y][x].neighbors[1] = null;
else if (northLimit)
letters[y][x].neighbors[1] = letters[maxRow][x];
//North-East
if (!northLimit && !eastLimit)
letters[y][x].neighbors[2] = letters[y - 1][x + 1];
else if (!wrap)
letters[y][x].neighbors[2] = null;
else if (northLimit && eastLimit)
letters[y][x].neighbors[2] = letters[maxRow][0];
else if (northLimit)
letters[y][x].neighbors[2] = letters[maxRow][x + 1];
else if (eastLimit)
letters[y][x].neighbors[2] = letters[y - 1][0];
//East
if (!eastLimit)
letters[y][x].neighbors[3] = letters[y][x + 1];
else if (!wrap)
letters[y][x].neighbors[3] = null;
else if (eastLimit)
letters[y][x].neighbors[3] = letters[y][0];
//South-East
if (!southLimit && !eastLimit)
letters[y][x].neighbors[4] = letters[y + 1][x + 1];
else if (!wrap)
letters[y][x].neighbors[4] = null;
else if (southLimit && eastLimit)
letters[y][x].neighbors[4] = letters[0][0];
else if (southLimit)
letters[y][x].neighbors[4] = letters[0][x + 1];
else if (eastLimit)
letters[y][x].neighbors[4] = letters[y + 1][0];
//South
if (!southLimit)
letters[y][x].neighbors[5] = letters[y + 1][x];
else if (!wrap)
letters[y][x].neighbors[5] = null;
else if (southLimit)
letters[y][x].neighbors[5] = letters[0][x];
//South-West
if (!southLimit && !westLimit)
letters[y][x].neighbors[6] = letters[y + 1][x - 1];
else if (!wrap)
letters[y][x].neighbors[6] = null;
else if (westLimit && southLimit)
letters[y][x].neighbors[6] = letters[0][maxCol];
else if (westLimit)
letters[y][x].neighbors[6] = letters[y + 1][maxCol];
else if (southLimit)
letters[y][x].neighbors[6] = letters[0][x - 1];
//West
if (!westLimit)
letters[y][x].neighbors[7] = letters[y][x - 1];
else if (!wrap)
letters[y][x].neighbors[7] = null;
else if (westLimit)
letters[y][x].neighbors[7] = letters[y][maxCol];
}
}