-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
547 lines (492 loc) · 20.6 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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
import java.util.ArrayList;
public class Board {
private ArrayList<ArrayList<Tile>> board = new ArrayList<ArrayList<Tile>>();
private int lastX, lastY;
private ArrayList<Integer> extraMeeples = new ArrayList<Integer>();
private ArrayList<Integer> activeSides;
final int TILE_LINES = 3;
final int TILE_SPACES = 5;
final int TOP = 0;
final int LEFT = 1;
final int BOTTOM = 2;
final int RIGHT = 3;
final int EMPTY = -2;
final int GRASS = -1;
final int ROAD = 0;
final int CITY = 1;
final int NO_PLAYER = 0;
public Board() {
for (int i = 0; i < 3; i++) {
ArrayList<Tile> row = new ArrayList<Tile>();
row.add(null);
row.add(new Tile());
row.add(null);
board.add(row);
}
board.get(1).set(1, new Tile(CITY, ROAD, GRASS, ROAD, true, false, -1));
board.get(1).set(0, new Tile());
board.get(1).set(2, new Tile());
lastX = 1;
lastY = 1;
}
public Board(String save) {
board = new ArrayList<ArrayList<Tile>>();
lastX = 1;
lastY = 0;// points to empty tile: Should make all functions involving it not work, but
// not crash
String rowSave;
while (!save.equals("")) {
rowSave = save.substring(0, save.indexOf('\\'));
board.add(new ArrayList<Tile>());
while (!rowSave.equals("")) {
if (rowSave.indexOf(';') == 0)
board.get(board.size() - 1).add(null);
else
board.get(board.size() - 1).add(
new Tile(rowSave.substring(0, rowSave.indexOf(';'))));
rowSave = rowSave.substring(rowSave.indexOf(';') + 1);
}
save = save.substring(save.indexOf('\\') + 1);
}
}
public ArrayList<Tile> tiles() {
ArrayList<Tile> allTiles = new ArrayList<Tile>();
for (ArrayList<Tile> row : board)
for (Tile t : row)
if (t != null && t.getSide(0) != -2)
allTiles.add(t);
return allTiles;
}
private boolean exists(int x, int y) {
return x >= 0
&& x < board.size()
&& y >= 0
&& y < board.get(1).size()
&& board.get(x).get(y) != null
&& board.get(x).get(y).getSide(TOP) != -2;
} // change other iterations of 'exists' to .getSide(TOP) != -2
private boolean isFinished(ArrayList<Tile> element) {
for (Tile t : element) {
if (t.getSide(TOP) == -2) {
return false;
}
}
return true;
}
public ArrayList<Tile> elementAt(int x, int y, int side) {
if (!exists(x, y))
return new ArrayList<Tile>();
if (board.get(x).get(y).getSideType(side) == GRASS) {
if (board.get(x).get(y).getMonastery() > NO_PLAYER) {
ArrayList<Tile> element = new ArrayList<Tile>();
for (int r = x - 1; r < x + 2; r++) {
for (int c = y - 1; c < y + 2; c++) {
if (board.get(r).get(c) != null) {
element.add(board.get(r).get(c));
}
}
}
return element;
}
return new ArrayList<Tile>();
}
activeSides = new ArrayList<Integer>();
return elementAt(x, y, side, new ArrayList<Tile>());
}
private ArrayList<Tile> elementAt(int x, int y, int side, ArrayList<Tile> remainder) {
if (!board.get(x).get(y).getCityConnect() || !board.get(x).get(y).getRoadConnect()) {
int count = 0;// counts tiles tracked by activeSides
for (Tile t : remainder) {
if (t == board.get(x).get(y) && activeSides.get(count) == side) {
return remainder;// ONLY if remainder already contains this tile AND side do we stop
}
if (!t.getCityConnect() || !t.getRoadConnect())
count++;
}
activeSides.add(side);
int x1, y1;
switch (side) {
case TOP:
x1 = x - 1;
y1 = y;
break;
case LEFT:
x1 = x;
y1 = y - 1;
break;
case BOTTOM:
x1 = x + 1;
y1 = y;
break;
default:
x1 = x;
y1 = y + 1;
}
remainder.add(board.get(x).get(y));
if (board.get(x).get(y).getSideType(side) == board.get(x).get(y).getSideType(side)) {// see below
if (board.get(x1).get(y1).getSideType((side + 2) % 4) == board.get(x).get(y).getSideType(side)) {
elementAt(x1, y1, (side + 2) % 4, remainder);// find the element of the next tile
} else if (!exists(x1, y1)) {
remainder.add(board.get(x1).get(y1));
}
}
}
if (remainder.contains(board.get(x).get(y)))
return remainder;
remainder.add(board.get(x).get(y));
for (int i = 0; i < 4; i++) {
int x1, y1;
switch (i) {
case TOP:
x1 = x - 1;
y1 = y;
break;
case LEFT:
x1 = x;
y1 = y - 1;
break;
case BOTTOM:
x1 = x + 1;
y1 = y;
break;
default:
x1 = x;
y1 = y + 1;
}
if (board.get(x).get(y).getSideType(side) == board.get(x).get(y).getSideType(i)) {// if this side is the type we seek
if (board.get(x1).get(y1).getSideType((i + 2) % 4) == board.get(x).get(y).getSideType(side)) {// if the next tile's type matches
elementAt(x1, y1, (i + 2) % 4, remainder);// find the element of the next tile
} else if (!exists(x1, y1)) {
remainder.add(board.get(x1).get(y1));
} // mark unfinished element
}
}
return remainder;
}
public int placeTile(int position, Tile tile) {
int x = 0;
int y = 0;
int pos = 0;
boolean[] check = { false, false };// check roads or cities -- fixes meeples on different sides
// finds the xy of 'position'
for (x = 0; x < board.size() && pos != position; x++) {
for (y = 0; y < board.get(0).size() && pos != position; y++) {
if (board.get(x).get(y) != null && board.get(x).get(y).getSide(TOP) == -2) {
pos++;
} // checks for empty tile that is printed in toString as a placeable tile
}
}
if (x == board.size() && y == board.get(0).size()) return -1;
x -= 1;
y -= 1;
lastX = x;
lastY = y;
if (tile.getCityConnect() && tile.getRoadConnect()) {
for (int i = 0; i < 4; i++) {
if (tile.getSideColor(i) > 0)
check[tile.getSideType(i)] = true;
}
} // find side types with meeples
for (int i = 0; i < 4; i++) {
int x1, y1;
switch (i) {
case TOP:
x1 = x - 1;
y1 = y;
break;
case LEFT:
x1 = x;
y1 = y - 1;
break;
case BOTTOM:
x1 = x + 1;
y1 = y;
break;
default:
x1 = x;
y1 = y + 1;
}
if (exists(x1, y1)) {
if (tile.getSideType(i) != board.get(x1).get(y1).getSideType((i + 2) % 4)) {
return i + 1;
} // side type mismatch
if ((tile.getSide(i) > -1 && check[tile.getSideType(i)]) || tile.getSide(i) > 1) {// Changed - hopefully
// is no longer
// problematic?
for (Tile t : elementAt(x1, y1, (i + 2) % 4)) {
if (!t.getCityConnect() || !t.getRoadConnect()) {
int j;
if (activeSides.size() == 0) {
System.out.print("!");// prevents error
j = 0;
} else
j = activeSides.remove(0);// for caps - prevents checking unconnected sides
// (AAAAAAAAAAAA)
if (tile.getSideType(i) == t.getSideType(j) && t.getSideColor(j) != 0) {
return i + 5;
}
} else {
for (int j = 0; j < 4; j++) {
if (tile.getSideType(i) == t.getSideType(j) && t.getSideColor(j) != 0) {
return i + 5;
}
}
}
}
}
} // end exists
} // end for each side
// place the tile
board.get(x).set(y, tile);
// expand the board
if (x == 0) {
board.add(0, new ArrayList<Tile>(board.get(0).size()));
for (int i = 0; i < board.get(1).size(); i++) {
board.get(0).add(null);
}
x++;
}
if (x == board.size() - 1) {
board.add(new ArrayList<Tile>(board.get(0).size()));
for (int i = 0; i < board.get(1).size(); i++) {
board.get(board.size() - 1).add(null);
}
}
if (y == 0) {
for (ArrayList<Tile> row : board) {
row.add(0, null);
}
y++;
}
if (y == board.get(0).size() - 1) {
for (int i = 0; i < board.size(); i++) {
board.get(i).add(null);
}
}
// add empty tiles
if (board.get(x - 1).get(y) == null) {
board.get(x - 1).set(y, new Tile());
}
if (board.get(x + 1).get(y) == null) {
board.get(x + 1).set(y, new Tile());
}
if (board.get(x).get(y - 1) == null) {
board.get(x).set(y - 1, new Tile());
}
if (board.get(x).get(y + 1) == null) {
board.get(x).set(y + 1, new Tile());
}
lastX = x;
lastY = y;
return 0;
}
public ArrayList<Integer> score() {
ArrayList<Integer> scores = new ArrayList<Integer>();
extraMeeples = new ArrayList<Integer>();
for (int i = 0; i < 4; i++) {
ArrayList<Tile> toScore = elementAt(lastX, lastY, i);
ArrayList<ArrayList<Integer>> results;
if (isFinished(toScore) && board.get(lastX).get(lastY).getSide(i) >= 0) {
results = scoreElement(toScore, board.get(lastX).get(lastY).getSideType(i));
for (int p = 0; p < results.get(0).size(); p++) {
if (scores.size() == p) {
scores.add(results.get(0).get(p));
} // account for variable player number
else {
scores.set(p, scores.get(p) + results.get(0).get(p));
} // add scores to total
if (board.get(lastX).get(lastY).getSideType(i) == CITY && results.get(0).get(p) != 2) {
scores.set(p, scores.get(p) + results.get(0).get(p));
} // double scores for cities more than two tiles
} // end combine scores
for (int p = 0; p < results.get(1).size(); p++) {
if (extraMeeples.size() == p) {
extraMeeples.add(results.get(1).get(p));
} // account for variable player number
else {
extraMeeples.set(p, extraMeeples.get(p) + results.get(1).get(p));
} // add scores to total
} // end combine meeples
}
} // end score cities + roads
for (int r = lastX - 1; r < lastX + 2; r++) {
for (int c = lastY - 1; c < lastY + 2; c++) {
if (exists(r, c) && board.get(r).get(c).getMonastery() > NO_PLAYER) {
ArrayList<Tile> toScore;
ArrayList<ArrayList<Integer>> results;
if (board.get(r).get(c).getSideType(0) == GRASS)
toScore = elementAt(r, c, 0);
else
toScore = elementAt(r, c, 1); // just make sure we hit the monastery
if (isFinished(toScore)) {
results = scoreElement(toScore, 3);
for (int p = 0; p < results.get(0).size(); p++) {
if (scores.size() == p) {
scores.add(results.get(0).get(p));
} // account for variable player number
else {
scores.set(p, scores.get(p) + results.get(0).get(p));
} // add scores to total
} // end combine scores
for (int p = 0; p < results.get(1).size(); p++) {
if (extraMeeples.size() == p) {
extraMeeples.add(results.get(1).get(p));
} // account for variable player number
else {
extraMeeples.set(p, extraMeeples.get(p) + results.get(1).get(p));
} // add scores to total
} // end combine meeples
}
}
}
} // end score monasteries WTF WAS I THINKING HERE WHERE IS SCOREELEMENT
return scores;
}
public ArrayList<Integer> scoreAll() {
ArrayList<Integer> scores = new ArrayList<Integer>();
for (int r = 0; r < board.size(); r++) {
for (int c = 0; c < board.get(0).size(); c++) {
if (exists(r, c)) {
Tile t = board.get(r).get(c);
for (int i = 0; i < 4; i++) {
if (t.getSideColor(i) != NO_PLAYER) {
ArrayList<Tile> score = elementAt(r, c, i);
ArrayList<Integer> totals = scoreElement(score, t.getSideType(i)).get(0);
for (int p = 0; p < totals.size(); p++) {
if (scores.size() == p) {
scores.add(totals.get(p));
} // account for variable player number
else {
scores.set(p, scores.get(p) + totals.get(p));
} // add scores to total
} // end combine scores
}
} // end loop thru sides
if (t.getMonastery() > NO_PLAYER) {
ArrayList<Tile> score;
if (t.getSideType(0) == GRASS)
score = elementAt(r, c, 0);
else
score = elementAt(r, c, 1); // just make sure we hit the monastery
ArrayList<Integer> totals = scoreElement(score, 3).get(0);
for (int p = 0; p < totals.size(); p++) {
if (scores.size() == p) {
scores.add(totals.get(p));
} // account for variable player number
else {
scores.set(p, scores.get(p) + totals.get(p));
} // add scores to total
} // end combine scores
}
} // end check for tile
} // end loop thru columns
} // end loop thru rows
return scores;
}
private ArrayList<ArrayList<Integer>> scoreElement(ArrayList<Tile> element, int type) {
ArrayList<Integer> score = new ArrayList<Integer>();
ArrayList<Integer> meeples = new ArrayList<Integer>();
System.out.println("Scored! " + type);
while (element.remove(null));//removes all nullpieces?
// for (int i = 0; i < element.size(); i++) {
// if (element.get(i).getSide(TOP) == -2) {
// element.remove(i);
// i--;
// }
// } // we dont care abt end pieces at this point
int total = element.size();
if (type == 3 && total == 9) {
if (element.get(4).getMonastery() > NO_PLAYER) {
while (element.get(4).getMonastery() > meeples.size()) {
meeples.add(0);
} // account for flexible player number
meeples.set(element.get(4).getMonastery() - 1, meeples.get(element.get(4).getMonastery() - 1) + 1);
element.get(4).setMonastery(NO_PLAYER);
}
}
else for (Tile t : element) {
if (!t.getCityConnect() || !t.getRoadConnect()) {
int i = activeSides.remove(0);
if (t.getSideType(i) == type && t.getSideColor(i) != NO_PLAYER) {
while (t.getSideColor(i) > meeples.size()) {
meeples.add(0);
} // account for flexible player number
meeples.set(t.getSideColor(i) - 1, meeples.get(t.getSideColor(i) - 1) + 1);
t.setSideColor(i, NO_PLAYER);
}
} else {
for (int i = 0; i < 4; i++) {
if (t.getSideType(i) == type && t.getSideColor(i) != NO_PLAYER) {
while (t.getSideColor(i) > meeples.size()) {
meeples.add(0);
} // account for flexible player number
meeples.set(t.getSideColor(i) - 1, meeples.get(t.getSideColor(i) - 1) + 1);
t.setSideColor(i, NO_PLAYER);
}
} // end remove and track meeples
}
if (type == CITY && t.getFortified()) {
total++;
}
}
int max = 0;
for (Integer i : meeples) {
if (i > max)
max = i;
} // find owners of element
for (Integer i : meeples) {
if (i == max)
score.add(total);
else
score.add(0);
} // count scores for each player
ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>();
ret.add(score);
ret.add(meeples);
return ret;
}
/**
* Precondition: score() was just called
*/
public ArrayList<Integer> meeplesBack() {
return extraMeeples;
}
public String toString() {
String boardStr = "";
int placement = 0;
for (ArrayList<Tile> row : board) {
for (int i = 0; i < TILE_LINES; i++) {
for (Tile tile : row) {
if (tile == null) {
boardStr += " ";// x5
} else if (tile.getSide(0) == -2 && i == TILE_LINES / 2) {
placement++;
if (placement < 10)
boardStr += " " + placement + " ";
else if (placement < 100)
boardStr += " " + placement + " ";
else
boardStr += " " + placement + " ";
} else {
boardStr += tile.toString(i);
}
}
boardStr += "\n";
} // end every line
} // end every row of tiles
return boardStr;
}
public String save() {
String save = "";
for (ArrayList<Tile> row : board) {
for (Tile t : row) {
if (t != null) {
save += t.save();
}
save += ";";
}
save += "\\";
}
return save;
}
}