-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoardGame.java
More file actions
617 lines (564 loc) · 24 KB
/
BoardGame.java
File metadata and controls
617 lines (564 loc) · 24 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
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*
To-Do:
Full Block Detection
(Winner and Square Tallying)
Full Block Detection <--- Do this now
Endscreen (Winner and Square Tallying)
Add Banner On Top
*/
import java.awt.*;
import java.util.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.sound.sampled.*;
import java.net.URL;
public class BoardGame {
private Grid grid, tutorialGrid;
private int[][] board, tutorialBoard;
private ArrayList<Piece> bluePieces, yellowPieces, redPieces, greenPieces, tutorialPieces;
private ArrayList<ArrayList<Piece>> allPieces;
private Color[] pieceColors;
private String[] playerNames;
private int playerNum, prevMouseX, prevMouseY;
private String state;
private Piece testingPiece;
private Font headerFont, scoreFont, titleFont, bodyFont;
private Piece botActivePiece;
private int botTargetRow, botTargetCol, delay;
private Screen screen;
private int[] skippedPlayers;
public BoardGame(String[] playerNames, Screen screen) throws IOException {
grid = new Grid(450, 50, 25);
bluePieces = new ArrayList<>();
yellowPieces = new ArrayList<>();
redPieces = new ArrayList<>();
greenPieces = new ArrayList<>();
allPieces = new ArrayList<>();
allPieces.add(bluePieces);
allPieces.add(yellowPieces);
allPieces.add(redPieces);
allPieces.add(greenPieces);
pieceColors = new Color[] {Color.BLUE, Color.YELLOW, Color.RED, Color.GREEN};
this.playerNames = playerNames;
BufferedReader br = new BufferedReader(new FileReader("Layouts.txt"));
String line = br.readLine();
boolean isRowNum = true;
int rowNum = 0;
int[][] layout = new int[0][0];
while (line != null) {
if (isRowNum) {
String[] dimensions = line.split(",");
layout = new int[Integer.parseInt(dimensions[0])][Integer.parseInt(dimensions[1])];
isRowNum = false;
rowNum = 0;
} else if (line.equals("")) {
for (int j = 0; j < allPieces.size(); j++) {
allPieces.get(j).add(new Piece(0, 0, 25, layout, pieceColors[j]));
}
isRowNum = true;
} else {
String[] rowString = line.split(" ");
for (int j = 0; j < rowString.length; j++) {
layout[rowNum][j] = Integer.parseInt(rowString[j]);
}
rowNum++;
}
line = br.readLine();
}
if (layout.length != 0) {
for (int j = 0; j < allPieces.size(); j++) {
allPieces.get(j).add(new Piece(0, 0, 25, layout, pieceColors[j]));
}
}
for (int i = 0; i < 4; i++) {
playerNum = i;
organizePieces();
}
playerNum = 0;
board = new int[20][20];
for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board[r].length; c++) {
board[r][c] = -1;
}
}
playerNum = 0;
tutorialPieces = new ArrayList<>();
tutorialPieces.add(new Piece(25, 300, 25, new int[][] {{0, 1, 0}, {0, 1, 0}, {1, 1, 1}}, Color.GREEN));
tutorialPieces.add(new Piece(75, 225, 25, new int[][] {{0, 0, 1}, {0, 0, 1}, {1, 1, 1}}, Color.GREEN));
tutorialPieces.add(new Piece(150, 175, 25, new int[][] {{0, 0, 1}, {1, 1, 1}}, Color.GREEN));
tutorialPieces.add(new Piece(150, 225, 25, new int[][] {{0, 1, 1}, {1, 1, 0}, {1, 0, 0}}, Color.RED));
tutorialPieces.add(new Piece(175, 300, 25, new int[][] {{1, 0}, {1, 1}, {1, 0}}, Color.RED));
tutorialPieces.add(new Piece(225, 300, 25, new int[][] {{0, 1}, {0, 1}, {1, 1}}, Color.RED));
tutorialPieces.add(new Piece(300, 413, 25, new int[][] {{0, 1, 1}, {1, 1, 0}, {1, 0, 0}}, Color.GREEN));
tutorialPieces.add(new Piece(400, 413, 25, new int[][] {{1, 0}, {1, 1}, {1, 0}}, Color.GREEN));
tutorialPieces.add(new Piece(300, 513, 25, new int[][] {{0, 1, 0}, {0, 1, 0}, {1, 1, 1}}, Color.RED));
tutorialPieces.add(new Piece(400, 513, 25, new int[][] {{0, 0, 1}, {0, 0, 1}, {1, 1, 1}}, Color.RED));
testingPiece = new Piece(25, 500, 25, new int[][] {{0, 1, 1}, {1, 1, 0}, {1, 0, 0}}, Color.YELLOW);
tutorialGrid = new Grid(25, 125, 25);
tutorialBoard = new int[10][10];
for (int r = 0; r < tutorialBoard.length; r++) {
for (int c = 0; c < tutorialBoard[r].length; c++) {
tutorialBoard[r][c] = -1;
}
}
titleFont = new Font("Times New Roman", Font.BOLD, 80);
bodyFont = new Font("Times New Roman", Font.PLAIN, 25);
this.screen = screen;
delay = -1;
skippedPlayers = new int[4];
for (int i = 0; i < skippedPlayers.length; i++) {
skippedPlayers[i] = -1;
}
state = "start";
}
public void draw(Graphics g) {
FontMetrics metrics = g.getFontMetrics(titleFont);
if (state.equals("start")) {
g.setColor(Color.BLUE);
g.fillRect(0, 0, 500, 300);
g.setColor(Color.YELLOW);
g.fillRect(0, 300, 500, 300);
g.setColor(Color.RED);
g.fillRect(500, 300, 500, 300);
g.setColor(Color.GREEN);
g.fillRect(500, 0, 500, 300);
g.setColor(new Color(222, 222, 222));
g.fillRect(300, 100, 400, 400);
g.setFont(titleFont);
g.setColor(Color.BLACK);
g.drawString("Blokus", 500 - metrics.stringWidth("Blokus") / 2, 200);
} else if (state.equals("setup")) {
g.setColor(Color.BLACK);
g.setFont(titleFont);
g.drawString("Setup Names and Bots", 500 - metrics.stringWidth("Setup Names and Bots") / 2, 75);
g.setColor(Color.BLUE);
g.fillRect(350, 100, 50, 50);
g.setColor(Color.YELLOW);
g.fillRect(350, 175, 50, 50);
g.setColor(Color.RED);
g.fillRect(350, 250, 50, 50);
g.setColor(Color.GREEN);
g.fillRect(350, 325, 50, 50);
} else if (state.equals("play")) {
grid.draw(g, board, pieceColors);
if (playerNum < allPieces.size() && !allPieces.get(playerNum).isEmpty()) {
for (Piece piece : new ArrayList<>(allPieces.get(playerNum))) {
piece.draw(g);
}
}
//Draw arrows
//Blue
drawArrows(g, grid.getX() - 5 , grid.getY() - 5, Color.BLUE);
//green
drawArrows(g, grid.getX() + 20 * grid.getSquareSize() + 5, grid.getY() - 5, Color.GREEN);
//yellow
drawArrows(g, grid.getX() - 5 , grid.getY() + 20 * grid.getSquareSize() + 5, Color.YELLOW);
//Red
drawArrows(g, grid.getX() + 20 * grid.getSquareSize() + 5, grid.getY() + 20 * grid.getSquareSize() + 5, Color.RED);
if (delay != -1) {
delay++;
if (delay > 10) {
delay = -1;
state = "endscreen";
playSound("Win.wav");
}
}
} else if (state.equals("rules")) {
tutorialGrid.draw(g, tutorialBoard, pieceColors);
for (Piece piece : tutorialPieces) {
piece.draw(g);
}
testingPiece.draw(g);
g.setColor(Color.BLACK);
g.setFont(titleFont);
g.drawString("Blokus Rules", 250, 80);
g.setFont(bodyFont);
g.drawString("4 Players Have Pieces of Their Color", 325, 150);
g.drawString("Your First Piece Must be In Your Corner", 325, 200);
g.drawString("Your Other Pieces Must be Corner-To-Corner", 325, 250);
g.drawString("Your Pieces can Touch Other Players' but not Overlap", 325, 300);
g.drawString("Once No One can Place a Piece, Squares are Counted", 325, 350);
g.drawString("The Player with the Least Squares Left Not Placed Wins", 325, 400);
g.drawString("Use Mouse, E/Q, & WASD", 25, 400);
g.drawString("Try with This Piece", 25, 450);
g.setFont(titleFont);
g.drawString("9 (Winner)", 500, 475);
g.drawString("10", 500, 575);
} else if (state.equals("endscreen")) {
int[] ranks = rankPlayers();
g.setColor(Color.BLACK);
g.setFont(titleFont);
for (int i = 0; i < 4; i++) {
String text = i + 1 + ". " + playerNames[ranks[i]] + ": " + getSquaresLeft(ranks[i]);
g.drawString(text, 500 - metrics.stringWidth(text) / 2, 100 + i * 100);
}
}
}
public void update() {
if (botActivePiece == null || !state.equals("play")) return;
if (botActivePiece.stepTowardsTarget()) {
int[][] layout = botActivePiece.getLayout();
for (int pr = 0; pr < layout.length; pr++) {
for (int pc = 0; pc < layout[pr].length; pc++) {
if (layout[pr][pc] == 1) {
board[botTargetRow + pr][botTargetCol + pc] = playerNum;
}
}
}
allPieces.get(playerNum).remove(botActivePiece);
botActivePiece = null;
organizePieces();
passPlay();
}
}
public void selectPiece(int x, int y, boolean selected) {
for (Piece piece : allPieces.get(playerNum)) {
if (selected && piece.containsPoint(x, y)) {
piece.setSelected(true);
} else {
piece.setSelected(false);
piece.resetPosition();
}
}
if (selected && testingPiece.containsPoint(x, y)) {
testingPiece.setSelected(true);
} else {
testingPiece.setSelected(false);
testingPiece.resetPosition();
}
prevMouseX = x;
prevMouseY = y;
}
public void movePiece(int x, int y) {
for (Piece piece : allPieces.get(playerNum)) {
if (piece.getSelected()) {
piece.move(x - prevMouseX, y - prevMouseY);
prevMouseX = x;
prevMouseY = y;
}
}
if (testingPiece.getSelected()) {
testingPiece.move(x - prevMouseX, y - prevMouseY);
prevMouseX = x;
prevMouseY = y;
}
}
public void rotatePiece(boolean clockwise) {
for (Piece piece : allPieces.get(playerNum)) {
if (piece.getSelected()) {
piece.rotate(clockwise);
}
}
if (testingPiece.getSelected()) {
testingPiece.rotate(clockwise);
}
}
public void flipPiece(boolean horizontal) {
for (Piece piece : allPieces.get(playerNum)) {
if (piece.getSelected()) {
piece.flip(horizontal);
}
}
if (testingPiece.getSelected()) {
testingPiece.flip(horizontal);
}
}
public void alignPiece() {
Piece removed = null;
for (Piece piece : allPieces.get(playerNum)) {
if (piece.getSelected()) {
piece.autoAlign();
if (isValidMove(piece)) {
int xDif = (piece.getX() - grid.getX()) / 25;
int yDif = (piece.getY() - grid.getY()) / 25;
int[][] layout = piece.getLayout();
for (int r = 0; r < layout.length; r++) {
for (int c = 0; c < layout[r].length; c++) {
if (layout[r][c] == 1) {
board[yDif + r][xDif + c] = playerNum;
}
}
}
removed = piece;
} else {
piece.resetPosition();
}
}
}
if (removed != null) {
allPieces.get(playerNum).remove(removed);
organizePieces();
passPlay();
}
if (testingPiece.getSelected()) {
testingPiece.autoAlign();
}
}
public boolean isValidMove(Piece piece) {
if (piece.getX() < grid.getX() || piece.getX() + (piece.getLayout()[0].length * 25) > grid.getX() + 500 || piece.getY() < grid.getY() || piece.getY() + (piece.getLayout().length * 25) > grid.getY() + 500) {
//System.out.println("Out of Bounds");
return false;
}
boolean firstMove = true;
for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board[r].length; c++) {
if (board[r][c] == playerNum) {
firstMove = false;
break;
}
if (! firstMove) {
break;
}
}
}
int[][] startingCorners = new int[][] {{grid.getX(), grid.getY()}, {grid.getX(), grid.getY() + 500}, {grid.getX() + 500, grid.getY() + 500}, {grid.getX() + 500, grid.getY()}};
if (firstMove) {
if (! piece.containsPoint(startingCorners[playerNum][0], startingCorners[playerNum][1])) {
//System.out.println("Not Corner Start");
return false;
}
//System.out.println("Valid First Move");
return true;
}
boolean cornerConnect = false;
int[][] layout = piece.getLayout();
for (int r = 0; r < layout.length; r++) {
for (int c = 0; c < layout[r].length; c++) {
if (layout[r][c] == 0) {
continue;
}
int pieceRow = r + (piece.getY() - grid.getY()) / 25;
int pieceCol = c + (piece.getX() - grid.getX()) / 25;
if (board[pieceRow][pieceCol] != -1) {
//System.out.println("Overlap");
return false;
}
for (int rDif = -1; rDif < 2; rDif++) {
for (int cDif = -1; cDif < 2; cDif++) {
try {
if (rDif == 0 && cDif == 0) {
continue;
} else if (rDif == 0 || cDif == 0) { //Horizontal check
if (board[pieceRow + rDif][pieceCol + cDif] == playerNum) {
//System.out.println("Horizontal to Friendly");
return false;
}
} else { //Diagonal check
if (board[pieceRow + rDif][pieceCol + cDif] == playerNum) {
cornerConnect = true;
}
}
} catch(ArrayIndexOutOfBoundsException e) {
//System.out.println("Array Index out of Bounds");
}
}
}
}
}
if (! cornerConnect) {
//System.out.println("Not Corner-To-Corner");
return false;
}
//System.out.println("Valid Regular Move");
return true;
}
public void organizePieces() {
int x = 25;
int y = 25;
int longestY = 0;
for (Piece piece : allPieces.get(playerNum)) {
int[][] layout = piece.getLayout();
if (x + layout[0].length * 25 + 25 >= 425) {
x = 25;
y += longestY + 25;
longestY = 0;
}
piece.setPosition(x, y);
piece.setOgPosition(x, y);
x += layout[0].length * 25 + 25;
if (layout.length * 25 > longestY) {
longestY = layout.length * 25;
}
}
}
public void drawArrows(Graphics g, int arrowX, int arrowY, Color color) {
//angle based on color
int angle = 45;
if (color.equals(Color.GREEN)) angle = 135;
if (color.equals(Color.RED)) angle = -135;
if (color.equals(Color.YELLOW)) angle = -45;
// cast g2d
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(color);
// origin goes to cords provided
g2d.translate(arrowX, arrowY);
g2d.rotate(Math.toRadians(angle));
// draw arrow
g2d.fillRect(-35, -6, 25, 12);
int[] xPoints = {-10, -10, 0};
int[] yPoints = {-10, 10, 0};
g2d.fillPolygon(xPoints, yPoints, 3);
}
public boolean canMakeMove() {
for (Piece piece : allPieces.get(playerNum)) {
for (int flipNum = 0; flipNum < 2; flipNum++) {
for (int rotNum = 0; rotNum < 4; rotNum++) {
for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board[r].length; c++) {
piece.setPosition(grid.getX() + c * grid.getSquareSize(), grid.getY() + r * grid.getSquareSize());
if (isValidMove(piece)) {
piece.resetPosition();
return true;
}
}
}
piece.rotate(true);
}
piece.flip(true);
}
piece.resetPosition();
}
return false;
}
public int[] rankPlayers() {
int[] ranks = new int[] {0, 1, 2, 3};
for (int i = 0; i < playerNames.length - 1; i++) {
for (int j = 0; j < playerNames.length - 1 - i; j++) {
if (getSquaresLeft(ranks[j]) > getSquaresLeft(ranks[j+1])) {
int temp = ranks[j];
ranks[j] = ranks[j+1];
ranks[j+1] = temp;
}
}
}
return ranks;
}
/**
* Makes a valid move for the current player automatically.
* Strategy: tries pieces largest-first. For the biggest piece that has
* any valid placement, picks the placement whose center of mass is
* closest to the board center (most central positioning).
*/
public void botMove() {
if (botActivePiece != null) return; // already animating
ArrayList<Piece> pieces = allPieces.get(playerNum);
Collections.shuffle(pieces);
pieces.sort((a, b) -> b.getSquares() - a.getSquares());
// Board center in cell coordinates
double boardCenterR = (board.length - 1) / 2.0; // 9.5
double boardCenterC = (board[0].length - 1) / 2.0; // 9.5
for (Piece piece : pieces) {
// Track the best placement found for this piece
int bestRow = -1;
int bestCol = -1;
int[][] bestLayout = null; // snapshot of the winning orientation
double bestDist = Double.MAX_VALUE;
for (int flipNum = 0; flipNum < 2; flipNum++) {
for (int rotNum = 0; rotNum < 4; rotNum++) {
for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board[r].length; c++) {
piece.setPosition(
grid.getX() + c * grid.getSquareSize(),
grid.getY() + r * grid.getSquareSize()
);
if (isValidMove(piece)) {
// Compute center of mass of this placement in board coords
int[][] layout = piece.getLayout();
double sumR = 0, sumC = 0, filled = 0;
for (int pr = 0; pr < layout.length; pr++) {
for (int pc = 0; pc < layout[pr].length; pc++) {
if (layout[pr][pc] == 1) {
sumR += r + pr;
sumC += c + pc;
filled++;
}
}
}
double dist = Math.sqrt(
Math.pow((sumR / filled) - boardCenterR, 2) +
Math.pow((sumC / filled) - boardCenterC, 2)
);
if (dist < bestDist) {
bestDist = dist;
bestRow = r;
bestCol = c;
// Deep-copy the layout so the orientation is frozen here
bestLayout = new int[layout.length][];
for (int i = 0; i < layout.length; i++)
bestLayout[i] = layout[i].clone();
}
}
}
}
piece.rotate(true);
}
piece.flip(true);
}
piece.resetPosition(); // Restore x/y for tray; shape is already back after 4 rotations + 2 flips
if (bestLayout != null) {
piece.setShape(bestLayout);
piece.setTarget(
grid.getX() + bestCol * grid.getSquareSize(),
grid.getY() + bestRow * grid.getSquareSize()
);
botActivePiece = piece;
botTargetRow = bestRow;
botTargetCol = bestCol;
return;
}
// No valid placement for this piece — fall through to try the next smaller one
}
}
public void passPlay() {
// Advance to the next player, wrapping within 0..3.
playerNum = (playerNum + 1) % 4;
// Skip every consecutive player who has no legal move. Test at most
// 4 players; if none can move, the game is over.
int playersTested = 0;
while (!canMakeMove()) {
playSound("Skip.wav");
playersTested++;
if (playersTested >= 4) {
// No one can move: trigger the end-screen transition.
delay = 0;
return;
}
playerNum = (playerNum + 1) % 4;
}
if (playerNames[playerNum].indexOf("Bot") != -1) {
botMove();
}
}
public void playSound(String file) {
new Thread(() -> {
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(new File(file));
Clip clip = AudioSystem.getClip();
clip.open(ais);
clip.start();
Thread.sleep(clip.getMicrosecondLength() / 1000);
clip.close();
} catch (Exception e) {
System.out.println("playSound error: " + e);
}
}).start();
}
public int getSquaresLeft() {
return getSquaresLeft(playerNum);
}
public int getSquaresLeft(int player) {
int total = 0;
for (Piece piece : allPieces.get(player)) {
total += piece.getSquares();
}
return total;
}
public String getState() {return state;}
public void setState(String state) {
this.state = state;
if (state.equals("play") && playerNames[playerNum].indexOf("Bot") != -1) {
botMove();
}
}
public void setNames(String[] names) {playerNames = names;}
}