Repository navigation
Expand file tree
/
Copy pathGame.java
More file actions
489 lines (386 loc) · 13.1 KB
/
Copy pathGame.java
File metadata and controls
489 lines (386 loc) · 13.1 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
class Move {
Point from, to;
Piece captured = null;
Move(int x1, int y1, int x2, int y2 ) {
from.setLocation(x1, y1);
to.setLocation(x2, y2);
}
Move(Point f, Point t, Piece c ) {
from = f;
to t;
captured = c;
}
}
class Game extends JPanel {
private static final long serialVersionUID = 1L;
// MEMBER VARIABLES
BufferedImage ui;
AI ai;
// MULTIARRAY
Square[][] board = new Square[8][8];
ArrayList<Move> history = new ArrayList<Move>();
Point clickStart = null;
Point clickEnd null;
int turn = 0;
Point cursor;
// CONSTRUCTOR
public Game() {
setLayout(null);
loadSquares();
loadPieces();
gameListener();
}
///////////////////////////////////////////////////////////////////////////////
// MEMBERS
///////////////////////////////////////////////////////////////////////////////
public boolean validMove() {
int x1 = (int) clickStart.getX();
int y1 = (int) clickStart.getY();
int x2 = (int) clickEnd.getX();
int y2 = (int) clickEnd.getY();
int dx = (turn = = 0) (x 2 - x1) : ( x2 - x1);
int dy = (turn = = 0) (y 2 - y1) : ( y2 - y1);
int py = (turn = = 0) ? -( y 2 - y1) : (y2 - y1);
Piece piece = board[y1][x1].piece;
List<Float> slopes = new ArrayList<>();
List<Float> distances = new ArrayList<>();
// CALCULATE THE SLOPE/DISTANCE FOR THE DESIRED MOVE
float slope = (dx = = 0 || dy == 0) ? 0 : (fl oat) dy / (f loat) dx;
float dist (float) Math.sqrt(dx * dx + d y * dy);
boolean pawntest = true;
// IF TARGET IS ON SAME TEAM LEAVE EARLY
if (board[y2][x2].piece != null && board[y2][x2].piece.side == board[y1][x1].piece.side)
return false;
// ASSIGN EACH OF THE PIECES A SLOPE AND DISTANCE THEY CAN MOVE
switch (piece.type) {
case KING:
slopes.add((float) 0);
slopes.add((float) 1);
distances.add((float) 1);
distances.add((float) Math.sqrt(2));
break;
case QUEEN:
slopes.add((float) 0);
slopes.add((float) 1);
distances.add((float) 0);
break;
case BISCHOP:
slopes.add((float) 1);
distances.add((float) 0);
break;
case KNIGHT:
slopes.add((float) 2);
slopes.add((float) .5);
distances.add((float) Math.sqrt(5));
break;
case ROOK:
slopes.add((float) 0);
distances.add((float) 0);
break;
case PAWN:
slopes.add((float) 0);
slopes.add(float) 1);
distances.add((float) 1);
distances.add(float) Math.sqrt(2));
if (piece.moved == 0)
distances.add((float) 2);
// pr ev en
horizontal pawn movement
if (dy == 0)
paw n te
t = false;
// prevent backward movement
if (py < 0)
pawntest = false;
// if trying to go forward prevent capture
if (Math.abs(slope) == 0 && (board[y2][x2].piece != null))
pawntes
// if trying to go diagonal make sure it is a capture
if (Math.abs(slope) == 1 && (board[y2][x2].piece == null || turn == board[y2][x2].piece.side))
pawntest = false;
break;
ca s EMPTY:
break;
}
// CHECK FOR COLLISION
if (checkCollision(clickStart, clickEnd))
return false;
// EVALUATE FINAL RESPONSE
if (listContains(slopes, Math.abs(slope)) && (listContains(distances, 0) || listContains(distances, dist))
&& pawntest) {
return true;
}
return false;
}
boolean listContains(List<Float> list, float ke y) {
fo r (float ele m : list)
if (elem == ke y)
return true;
return false;
public b ool e an ch eckCollis i
on(Po
i nt a, P oin t b) {
if (bo ard [ (int) a.getY() ]
[(int
) a.getX ()] . piece .type != C
hessP
iece.KNIGHT) {
int xp = (int) b.getX();
int yp = (int) b.getY();
while (true) {
// increment in direction of ro
t
if (xp > (int) a.getX()) {
xp--;
} else if (xp < (int) a.getX()) {
xp++;
}
if (yp > (int) a.getY()) {
yp--;
} else if (yp < (int) a.getY( )) {
yp++;
}
// if at root exit while
if (yp == (int) a.getY() && xp == (int) a.getX())
break;
// if piece found exit validation
if (board[yp][xp].piece != null)
return true;
}
}
return false;
}
public void movePiece() {
board[(int) clickEnd.getY()][(int) clickEnd.getX()].piece = board[(int) clickStart.getY()][(int) clickStart
.getX()].piece;
board[(int) clickStart.getY()][(int) clickStart.getX()].piece = null;
// Update Current State
// ai.setState(board);
// currentState.print( if (Main.client != null && Main.client.isActive()) {
Main.client.sendData(ai.serialize());
}
if (Main.server != null && Main.server.isActive()) {
Main.server.sendData(ai.serialize());
}
public void loadSquares() {
for ( int y = 0 ; y < 8; for (int x = 0; x < 8; x++) {
board[y][x] = new Square(x, y);
}
}
}
public v od lo adPiec es () {
ChessPiece type = ChessPiece.PAWN;
int sid e = 1;
int w =0;
for (int y = 0; y < 8; y++) {
if (y == 4)
side--;
for (int x = 0; x < 8; x++) {
if ((y == 0 || y == 7) && (x == 0 || x == 7)) {
tpe = Chess Pi ece.ROOK ;
w = 1;
}
if ((y == 0 || y == 7) && (x == 1 || x == 6)) {
ty pe = Che ss Piece.KNIGHT;
w = 1;
}
if ((y == 0 || y == 7) && (x == 2 || x == 5)) {
type = ChessPiece.BISCHOP;
w = 1;
}
if ((y == 0 || y == 7) && (x == 3)) {
type = ChessPiece.QUEEN;
w = 1;
}
if ((y == 0 || y == 7) && (x == 4)) {
type = ChessPiece.KING;
w = 1;
}
if (y == 1 || y == 6) {
type = ChessPiece.PAWN;
//
w = 1;
}
if (w == 1) {
board[y][x].piece = new Piece(type, side);
}
w = 0;
}
}
}
public void checkCapture() {
if (bo ard[(int) clickEnd.g
tY()][(int) clickEnd.getX()].piece != null) {
// System.out.println("captured: " +
// board[(int)clickEnd.getY()][(int)clickEnd.getX()].piece.type);
history.add(new Move(clickStart, clickEnd, board[(int) clickEnd.getY()][(int) clickEnd.getX()].piece));
} else {
history.add(new Move(clickStart, clickEnd, null));
}
}
public void gameListener() {
addMouseMotionListener(new MouseAdapter() {
// BOARD/MOUSE INTERACTION
public void mouseMoved(MouseEvent e) {
cursor = e.getLocationOnScreen();
if (clickStart != null)
repaint();
}
});
addMouseListener(new MouseAdapter() {
// BOARD/MOUSE INTERACTION
public void mousePressed(MouseEvent e) {
boolean valid = false;
// previously dealt with a final click, flush the trigger
if (clickEnd != null) {
clickStart = null;
clickEnd = null;
}
// SEE IF A SQUARE WAS CLICKED
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
// if an initial square is selected set to start or start over if already
// selected
if (board[y][x].shape.contains(e.getPoint()) && clickStart == board[y][x].coord) {
// do nothing because validator below will catch it
} else if (board[y][x].shape.contains(e.getPoint()) && clickStart == null && board[y][x].piece != null
&& board[y][x].piece.side == turn) {
System.out.println("from " + board[y][x].coord);
clickStart = new Point(x, y);
valid = true;
// if a start has already been selected set the destination
} else if (board[y][x].shape.contains(e.getPoint()) && clickStart != null) {
System.out.println("to " + board[y][x].coord);
clickEnd = new Point(x, y);
if (validMove()) {
board[(int) clickStart.getY()][(int) clickStart.getX()].piece.moved = 1;
turn = (turn == 0) ? 1 : 0;
checkCapture();
movePiece();
}
valid = false;
}
}
}
// if something other than square was clicked
if (!valid) {
clickStart = null;
clickEnd = null;
} else {
valid = false;
}
// DISPLAY CHANGES
repaint();
}
});
}
// DRAW GRAPHICS
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
try {
ui = ImageIO.read(new File("./img/bg/wood.png"));
} catch (IOException e) {
e.printStackTrace();
}
// DRAW BACKGROUND
g.drawImage(ui, 0, 0, null);
try {
ui = ImageIO.read(new File("./img/bg/trim.png"));
} catch (IOException e) {
e.printStackTrace();
}
g.drawImage(ui, board[0][0].offx - 24, board[0][0].offy - 24, null);
// DRAW TURN INDICATOR
g2.setColor((turn == 0) ? Color.decode("#ffffff") : Color.decode("#000000"));
g2.fillRect(board[0][0].offx - 25, board[0][0].offy - 25, 25, 25);
// DRAW BOARD
int toggle = 0;
int rowh = 8;
char colh;
for (int y = 0; y < 8; y++) {
colh = 'A';
// H EDING Y
g2.setColor(Color.decode("#111111"));
g2.fillRect(board[0][0].offx - 25, board[0][0].offy + y * board[0][0].size, 25, board[0][0].size);
g2.setColor(Color.decode("#ffffff"));
g2.drawString(String.valueOf(rowh), board[0][0].offx - 16, board[0][0].offy + 48 + (y * board[0][0].size));
for (int x = 0; x < 8; x++) {
if (y == 0) {
// HEADING X
g2.setColor(Color.decode("#111111"));
g2.fillRect(board[0][0].offx + x * board[0][0].size, board[0][0].offy - 25, board[0][0].size, 25);
g2.setColor(Color.decode("#ffffff"));
g2.drawString(String.valueOf(colh), board[0][0].offx + 34 + (x * board[0][0].size), board[0][0].offy - 8);
}
g2.setColor(toggle == 1 ? Color.BLACK : Color.white);
if (board[y][x].coord.equals(clickStart))
g2.setColor(Color.decode("#003366"));
g2.fill(board[y][x].shape);
toggle = toggle == 0 ? 1 : 0;
colh++;
}
toggle = toggle == 0 ? 1 : 0;
rowh-- ;
}
// DRAW PIECES
String fn;
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
if (board[y][x].piece != null) {
fn = "./img/" + (board[y][x].piece.side == 0 ? "w" : "b") + "_" + board[y][x].piece.type.name().toLowerCase()
+ ".png";
try {
ui = ImageIO.read( n ew File(fn));
} catch (IOException e) {
e.printStackTrace();
}
if (board[y][x].co ord.equals(clickStart)) {
g.drawImage(ui, (int) cursor.getX() - board[0][0].size / 2,
(int) cursor.ge tY() - (board[0][0].size / 2) - 25, null);
} else {
g.drawImage(ui, board[y][x].shape.getBounds().x, board[y][x].shape.getBounds().y, null);
}
}
}
}
int jxw = board[0][0].offx - board[0][0].size * 2;
int jy w = board[0][0].off y;
int jxb = b oard[0][0].offx + (board[0][0].size * 9 - 19);
int jyb = board[0][0].offy;
int cntw = 0, c nt b = 0;
for (Move move : history) {
if (move.captured != null) {
fn = "./img/" + ( move .captured.side == 0 ? "w" : "b") + "_" + move.captured.type.name().toLowerCase() + ".png";
try {
ui = ImageIO.read(new File(fn));
} ca tch ( IO Ex ception e) {
e.printStackTrace();
}
if (move.captured.side == 0) {
if (cntw == 8) {
jxw = board[0][0].offx - (board[0][0].size * 2) - 50;
jyw = board[0][0].offy;
}
} else {
g.drawImage(ui, jxb, jyb, null);
jyb += board[0][0].size;
cntb++;
if (cntb == 8) {
jxb = board[0][0].offx + (board[0][0].size * 9 - 19) + 50;
jyb = board[0][0].offy;
}
}
}
}
}
}