-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
343 lines (264 loc) · 7.57 KB
/
Board.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
// MINISCRABBLE
package GameComponents;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import Squares.DoubleLetterSquare;
import Squares.DoubleWordSquare;
import Squares.Square;
import Squares.TripleLetterSquare;
import Squares.TripleWordSquare;
public class Board {
public static final int ROWS = 15;
public static final int COLS = 15;
Square[][] squares;
/*
// instantiates a 15x15 board, with each square being "_"
public Board() {
squares = new Square[ROWS][COLS];
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
squares[row][col] = new Square();
squares[row][col].paint();
}
System.out.println();
}
}
*/
// this sets up the special tiles on the board at specific locations
public Board () throws IOException {
File file1 = new File("SpecialSquaresBoard.txt");
BufferedReader br = new BufferedReader(new FileReader(file1));
squares = new Square[ROWS][COLS];
try {
File file = new File("SpecialSquaresBoard.txt");
Scanner data = new Scanner(file);
int row = 0;
int col = 0;
while (data.hasNextLine()) {
String line = data.nextLine();
String[] values = line.split(" ");
if (values.length != 15) {
String err = "parse failed";
data.close();
throw new IllegalStateException(err);
}
for (String str : values) {
switch(str) {
case "TW":
squares[row][col] = new TripleWordSquare();
squares[row][col].paint();
break;
case "DW":
squares[row][col] = new DoubleWordSquare();
squares[row][col].paint();
break;
case "TL":
squares[row][col] = new TripleLetterSquare();
squares[row][col].paint();
break;
case "DL":
squares[row][col] = new DoubleLetterSquare();
squares[row][col].paint();
break;
case "--":
squares[row][col] = new Square();
squares[row][col].paint();
}
col+=1;
}
System.out.println();
row += 1;
col = 0;
}
data.close();
} catch(Exception e) {
e.printStackTrace();
System.exit(2);
}
}
// paints ONE SQUARE of the board with a tile.
public void paintBoard (Tile tile, int r, int c) {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
squares[r][c].setAssignedTile(tile);
squares[row][col].paint();
}
System.out.println();
}
}
public void paintBoard() {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
squares[row][col].paint();
}
System.out.println();
}
}
// checks whether word entered is aligned on the board by coordinate entered on board
/*
* can have something like
* _ _ _ _
* _ _ _ _
* _ * * *
* _ _ _ _
*
* cannot have something like
*
*
* _ _ _ _
* _ _ _ _
* _ * * _
* _ _ _ *
*
*/
// checks whether all of the spaces that are occupied are aligned
// this method is the first check for checking whether word entered on board is allowed
public boolean isAligned() {
boolean map[][] = new boolean[squares.length][squares.length];
for (int row = 0; row < squares.length; row++) {
for (int col = 0; col < squares[row].length; col++) {
if (squares[row][col].isOccupied())
map[row][col] = true;
else
map[row][col] = false;
}
}
int count = 0;
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if (map[i][j]) {
isAlignedHelper(i, j, map);
count++;
}
}
}
if (count > 1)
return false;
return true;
}
public static void isAlignedHelper(int i, int j, boolean [][] map) {
//nothing happens
int[][] move = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
map[i][j] = false;
for(int k=0;k<4;k++){
int i2 = i+move[k][0];
int j2 = j+move[k][1];
if(0<=i2 && i2<map.length && 0<=j2 && j2<map.length && map [i2][j2])
isAlignedHelper(i2,j2, map);
}
}
// input: squares that user entered
// returns list of words which tiles entered on the board formed words with
// this is to check whether the letters entered are in the dictionary
// returns list of squares for vertical word PER SQUARE
public ArrayList<Square> makeWordVertical(int x_initial, int y_initial) {
ArrayList<Square> vertSquareList = new ArrayList<>();
int row = x_initial;
// increment row until we've reached unoccupied square
while (row - 1 >= 0) {
if (squares[row][y_initial].isOccupied()) {
row--;
}
else {
break;
}
}
if (row > 0)
++row;
while (row < squares.length && squares[row][y_initial].isOccupied()) {
vertSquareList.add(squares[row][y_initial]);
++row;
}
return vertSquareList;
}
// returns list of squares for horizontal word PER SQUARE
public ArrayList<Square> makeWordHorizontal(int x_initial, int y_initial) {
ArrayList<Square> horizSquareList = new ArrayList<>();
int col = y_initial;
// decrement column until we've reached unoccupied square
while (col - 1 >= 0) {
if (squares[x_initial][col].isOccupied()) {
col--;
}
else {
break;
}
}
if (col > 0)
++col;
// increment column to form word (add tiles to ArrayList)
while (col < squares.length && squares[x_initial][col].isOccupied()) {
horizSquareList.add(squares[x_initial][col]);
++col;
}
return horizSquareList;
}
/*
// for the word suggestions
public ArrayList<String> getAllWords() {
for (int i = 0; i < squares.length; i++) {
for (int j = 0; i < squares[i].length; j++) {
if (squares[i][j].isOccupied()) {
}
}
}
}
*/
// returns list words only if their length >= 2
public ArrayList<String> getWords(Set<ArrayList<Square>> horizontalOrVerticalList) {
ArrayList<String> listOfStrings = new ArrayList<String>();
for (ArrayList<Square> listOfSquares : horizontalOrVerticalList) {
String word = "";
for (Square s : listOfSquares) {
char c = s.getLetter();
word += c;
}
if (word.length() >= 2)
listOfStrings.add(word);
}
return listOfStrings;
}
// this is a function that rips off the tiles on the board if the move is illegitimate
// i.e. : if the board is not aligned, if the word(s) formed are not in the dictionary, etc
public void undoBoard(ArrayList<Square> enteredSquares) {
ArrayList <Coord> coordinateList = new ArrayList<>();
for (int i = 0; i < enteredSquares.size(); i++) {
Square s = enteredSquares.get(i);
Coord squareCoords = s.getCoords();
coordinateList.add(squareCoords);
}
int indexInCoordList = 0;
while (indexInCoordList < coordinateList.size()) {
int row = coordinateList.get(indexInCoordList).getRow();
int col = coordinateList.get(indexInCoordList).getCol();
squares[row][col].setToNull();
indexInCoordList++;
}
System.out.println();
/*
//repaint the board
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
squares[r][c].paint();
}
System.out.println();
}
*/
}
// this is to make sure that the first player populates middle square (7, 7)
// if isEmptySquares(), and it's the first
public boolean isEmptySquares() {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (squares[row][col].isOccupied())
return false;
}
}
return true;
}
}