-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
432 lines (387 loc) · 16.5 KB
/
Copy pathBoard.java
File metadata and controls
432 lines (387 loc) · 16.5 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package test;
import java.util.ArrayList;
import java.util.Arrays;
public class Board {
private final Tile[][] tiles;
private final int[][] type;
//0- regular, 1- double letter, 2- triple letter, 3-double word, 4- triple word, 5- star.
private boolean isStarInitialized;
private static Board b = null;
private Board() {
this.isStarInitialized = false;
this.tiles = new Tile[15][15];
this.type = new int[15][15];
// Coordinates for different values
int[][] coordinates1 = {{0, 3}, {0, 11}, {2, 6}, {2, 8}, {3, 0}, {3, 7}, {3, 14}, {6, 2}, {6, 6}, {6, 8}, {6, 12}, {7, 3}, {7, 11}, {8, 2}, {8, 6}, {8, 8}, {8, 12}, {11, 0}, {11, 7}, {11, 14}, {12, 6}, {12, 8}, {14, 3}, {14, 11}};
int[][] coordinates2 = {{1, 5}, {1, 9}, {5, 1}, {5, 5}, {5, 9}, {5, 13}, {9, 1}, {9, 5}, {9, 9}, {9, 13}, {13, 5}, {13, 9}};
int[][] coordinates3 = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {10, 10}, {11, 11}, {12, 12}, {13, 13}, {1, 13}, {2, 12}, {3, 11}, {4, 10}, {13, 1}, {12, 2}, {11, 3}, {10, 4}};
int[][] coordinates4 = {{0, 0}, {0, 7}, {0, 14}, {7, 0}, {7, 14}, {14, 0}, {14, 7}, {14, 14}};
int[][] coordinates5 = {{7, 7}};
// Insert value 1 into coordinates1
insertValues(this.type, coordinates1, 1);
// Insert value 2 into coordinates2
insertValues(this.type, coordinates2, 2);
// Insert value 3 into coordinates3
insertValues(this.type, coordinates3, 3);
// Insert value 4 into coordinates4
insertValues(this.type, coordinates4, 4);
// Insert value 5 into coordinates5
insertValues(this.type, coordinates5, 5);
}
public static Board getBoard() {
if(b == null)
b = new Board();
return b;
}
// Method to create a shallow copy of the tiles array
public Tile[][] getTiles() {
Tile[][] copiedTiles = new Tile[15][15];
for (int i = 0; i < tiles.length; i++) {
System.arraycopy(tiles[i], 0, copiedTiles[i], 0, tiles[i].length);
}
return copiedTiles;
}
// Method to insert values into coordinates of the array
private void insertValues(int[][] array, int[][] coordinates, int value) {
for (int[] coordinate : coordinates) {
int x = coordinate[0];
int y = coordinate[1];
array[x][y] = value;
}
}
//Not good
public boolean boardLegal(Word w) {
if(w.getRow() > 14 || w.getRow() < 0 || w.getCol() > 14 || w.getCol() < 0) {
return false;
}
if(w.isVertical()) {
if(w.getTiles().length + w.getRow() > 15)
return false;
}
else {
if(w.getTiles().length + w.getCol() > 15)
return false;
}
if(tiles[7][7] == null) {
if (w.isVertical()) {
if (w.getCol() != 7)
return false;
if (w.getRow() > 7)
return false;
if (w.getTiles().length + w.getRow() < 7)
return false;
} else {
if (w.getRow() != 7)
return false;
if (w.getCol() > 7)
return false;
if (w.getTiles().length + w.getCol() < 7)
return false;
}
}
Tile[] wordTiles = w.getTiles();
if(w.isVertical()) {
for (int i = 0; i < wordTiles.length; i++) {
Tile currentTile = tiles[w.getRow() + i][w.getCol()];
if (currentTile != null) {
if (!currentTile.equals(wordTiles[i])) {
if(wordTiles[i]!=null){
if (wordTiles[i].getLetter() != '_') {
return false; // Tile does not match and is not empty, so return false
}
}
}
}
}
}
else{
for (int i = 0; i < wordTiles.length; i++) {
Tile currentTile = tiles[w.getRow()][w.getCol() + i];
if (currentTile != null) {
if (!currentTile.equals(wordTiles[i])) {
if(wordTiles[i]!=null){
if (wordTiles[i].getLetter() != '_') {
return false;
}
}
}
}
}
}
return true;
}
//The function will be implemented in the future
public boolean dictionaryLegal(Word w) {
return true;
}
private int[] findWordRange(int row, int startCol, int endCol) {
// Move startCol towards the beginning of the row until encountering a null tile or reaching the row limit
while (startCol >= 0 && tiles[row][startCol] != null) {
startCol--;
}
// Increment startCol to point to the beginning of the sequence of non-null tiles
startCol++;
// Move endCol towards the end of the row until encountering a null tile or reaching the row limit
while (endCol < 15 && tiles[row][endCol] != null) {
endCol++;
}
// Decrement endCol to point to the end of the sequence of non-null tiles
endCol--;
// Return the indices of the full row
return new int[]{startCol, endCol};
}
private int[] findVerticalWordRange(int col, int startRow, int endRow) {
// Move startRow upwards until encountering a null tile or reaching the column limit
while (startRow >= 0 && tiles[startRow][col] != null) {
startRow--;
}
// Increment startRow to point to the beginning of the sequence of non-null tiles
startRow++;
// Move endRow downwards until encountering a null tile or reaching the column limit
while (endRow < 15 && tiles[endRow][col] != null) {
endRow++;
}
// Decrement endRow to point to the end of the sequence of non-null tiles
endRow--;
// Return the indices of the full column
return new int[]{startRow, endRow};
}
private Word findWordWithMaxScore(int row, int start, int end, int fullStart, int fullEnd) {
int maxScore = 0;
Word maxWord = null;
// Iterate over each possible sequence of word within the given maximum range
for (int i = fullStart; i <= fullEnd; i++) {
for (int j = i; j <= fullEnd; j++) {
// Check if the current sequence contains the minimum sequence represented by start and end
if (i <= start && j >= end) {
// Create a Word object for the current sequence
Tile[] sequenceTiles = Arrays.copyOfRange(tiles[row], i, j + 1);
Word currentWord = new Word(sequenceTiles, row, i, false); // Assuming horizontal word
// Check if the word is legal according to the dictionary
if (dictionaryLegal(currentWord)) {
// Calculate the score of the word
int currentScore = getScore(currentWord);
// Update maxScore and maxWord if necessary
if (currentScore > maxScore) {
maxScore = currentScore;
maxWord = currentWord;
}
}
}
}
}
return maxWord;
}
private Word findVerticalWordWithMaxScore(int col, int start, int end, int fullStart, int fullEnd) {
int maxScore = 0;
Word maxWord = null;
// Iterate over each possible sequence of word within the given maximum range
for (int i = fullStart; i <= fullEnd; i++) {
for (int j = i; j <= fullEnd; j++) {
// Check if the current sequence contains the minimum sequence represented by start and end
if (i <= start && j >= end) {
// Create a Word object for the current sequence
Tile[] sequenceTiles = new Tile[j - i + 1];
for (int k = i; k <= j; k++) {
sequenceTiles[k - i] = tiles[k][col];
}
Word currentWord = new Word(sequenceTiles, i, col, true); // Vertical word
// Check if the word is legal according to the dictionary
if (dictionaryLegal(currentWord)) {
// Calculate the score of the word
int currentScore = getScore(currentWord);
// Update maxScore and maxWord if necessary
if (currentScore > maxScore) {
maxScore = currentScore;
maxWord = currentWord;
}
}
}
}
}
return maxWord;
}
public ArrayList<Word> getWords(Word word) {
ArrayList<Word> words = new ArrayList<>();
int row = word.getRow();
int col = word.getCol();
if (word.isVertical()) {
// Find word range for the vertical word
int[] range = findVerticalWordRange(col, row, row + word.getTiles().length - 1);
// Find word with max score for the vertical word
Word maxWord = findVerticalWordWithMaxScore(col, row, row + word.getTiles().length - 1, range[0], range[1]);
// Add maxWord to the ArrayList if it's not null
if (maxWord != null && word.getTiles().length > 1) {
words.add(maxWord);
}
else{
return null;
}
int j = 0;
// Iterate over the given word coordinates and find words with max score for their horizontal segments
for (int i = row; i < row + word.getTiles().length; i++) {
if(word.getTiles()[j] != null){
if(word.getTiles()[j].getLetter()>= 'A' && word.getTiles()[j].getLetter() <= 'Z') {
int[] range2 = findWordRange(i, col, col);
Word horizontalWord = findWordWithMaxScore(i, col, col, range2[0], range2[1]);
if (horizontalWord != null && horizontalWord.getTiles().length > 1) {
words.add(horizontalWord);
}
}
}
j++;
}
} else {
// Find word range for the non-vertical word
int[] range = findWordRange(row, col, col + word.getTiles().length - 1);
// Find word with max score for the non-vertical word
Word maxWord = findWordWithMaxScore(row, col, col + word.getTiles().length - 1, range[0], range[1]);
// Add maxWord to the ArrayList if it's not null
if (maxWord != null && word.getTiles().length > 1) {
words.add(maxWord);
}
else{
return null;
}
int j = 0;
// Iterate over the given word coordinates and find words with max score for their vertical segments
for (int i = col; i < col + word.getTiles().length; i++) {
if(word.getTiles()[j] != null){
if(word.getTiles()[j].getLetter()>= 'A' && word.getTiles()[j].getLetter() <= 'Z') {
int[] range2 = findVerticalWordRange(i, row, row);
Word verticalWord = findVerticalWordWithMaxScore(i, row, row, range2[0], range2[1]);
if (verticalWord != null && verticalWord.getTiles().length > 1) {
words.add(verticalWord);
}
}
}
j++;
}
}
return words;
}
public int getScore(Word word) {
int score = 0;
int multiplier = 1;
int tileScore;
if (word.isVertical()) {
for(int i = 0; i < word.getTiles().length; i++){
tileScore = tiles[i+ word.getRow()][word.getCol()].getScore();
switch (type[i+ word.getRow()][word.getCol()]) {
case 0:
break;
case 1:
tileScore*=2;
break;
case 2:
tileScore*=3;
break;
case 3:
multiplier*=2;
break;
case 5:
if(!this.isStarInitialized) {
multiplier*=2;
}
break;
case 4:
multiplier*=3;
break;
default:
return 0;
}
score += tileScore;
}
}
else{
for(int i = 0; i < word.getTiles().length; i++){
tileScore = tiles[word.getRow()][word.getCol() + i].getScore();
switch (type[word.getRow()][word.getCol() + i]) {
case 0:
break;
case 1:
tileScore*=2;
break;
case 2:
tileScore*=3;
break;
case 3:
multiplier*=2;
break;
case 5:
if(!this.isStarInitialized) {
multiplier*=2;
}
break;
case 4:
multiplier*=3;
break;
default:
return 0;
}
score += tileScore;
}
}
score*=multiplier;
return score;
}
public int tryPlaceWord(Word word) {
if (!boardLegal(word)) {
return 0;
}
if (word.isVertical()) {
int x = word.getRow();
int y = word.getCol();
for (int i = 0; i < word.getTiles().length; i++) {
if(word.getTiles()[i] != null){
if (word.getTiles()[i].getLetter() >= 'A' && word.getTiles()[i].getLetter() <= 'Z') {
tiles[x+i][y] = word.getTiles()[i];
}
}
}
} else {
int x = word.getRow();
int y = word.getCol();
for (int i = 0; i < word.getTiles().length; i++) {
if(word.getTiles()[i] != null){
if (word.getTiles()[i].getLetter() >= 'A' && word.getTiles()[i].getLetter() <= 'Z') {
tiles[x][y + i] = word.getTiles()[i];
}
}
}
}
int score = 0;
ArrayList<Word> words = getWords(word);
if(words == null){
if (word.isVertical()) {
int x = word.getRow();
int y = word.getCol();
for (int i = 0; i < word.getTiles().length; i++) {
if(word.getTiles()[i] != null) {
if (word.getTiles()[i].getLetter() >= 'A' && word.getTiles()[i].getLetter() <= 'Z') {
tiles[x+i][y] = null;
}
}
}
} else {
int x = word.getRow();
int y = word.getCol();
for (int i = 0; i < word.getTiles().length; i++) {
if(word.getTiles()[i] != null) {
if (word.getTiles()[i].getLetter() >= 'A' && word.getTiles()[i].getLetter() <= 'Z') {
tiles[x][y + i] = null;
}
}
}
}
}else {
for(Word w: words) {
score= score + getScore(w);
}
if(!this.isStarInitialized){
this.isStarInitialized = true;
}
}
return score;
}
}