-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
502 lines (463 loc) · 18.1 KB
/
Board.java
File metadata and controls
502 lines (463 loc) · 18.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
490
491
492
493
494
495
496
497
498
499
500
501
502
package examples;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
public class Board extends Frame {
public static void main(String[] args) {new Board();}
Board(){
super("Checkers");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
setSize(700, 700);
add("Center", new CvBoard());
setVisible(true);
}
}
class CvBoard extends Canvas {
float x0, y0, rWidth = 10.0F, rHeight = 10.0F, pixelSize;
int centerX, centerY, maxX, maxY, xP = 1000000, yP= 1000000;
int left, right, top, bottom;
int dY, dX, numBoxes, boxHeight, boxWidth, circleHeight, circleWidth;
int leftStart, topStart, leftCircleStart, topCircleStart;
int selected, oldX = -1, oldY = -1;
static int[][] pieces = new int[8][8];
static boolean[][] legalMoves = new boolean[8][8];
static boolean gameOver = false;
static boolean selectedQ = false;
int playerTurn = 1;
int mouseClicks;
Font unselectedFont = new Font("Helvetica", Font.PLAIN, 20);
Font selectedFont = new Font("Helvetica", Font.BOLD, 22);
boolean gameStart;
CvBoard(){
gameStart = false;
initBoard();
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent click) {
xP = click.getX();
yP = click.getY();
repaint();
}
});
}
// initialize board with starting values
// 0 is empty, 1 is player 1, 2 is player 2, 3 is player 1 king, 4 is player 2 king
static void initBoard(){
gameOver = false;
for(int i = 0; i < pieces.length;i++) {
for(int j = 0; j < pieces[i].length; j++) {
if (i % 2 != j % 2) {
if (i < 3)
pieces[i][j] = 2;
else if (i > 4)
pieces[i][j] = 1;
else
pieces[i][j] = 0;
} else {
pieces[i][j] = -1;
}
}
}
}
// checks on board
static boolean checkGrid(int row, int column) {
if(row < 0 || row > 7 || column < 0 || column > 7) return false;
return true;
}
// reset legalMoves
static void resetMoves(){
for(int i = 0; i < legalMoves.length;i++) {
for(int j = 0; j < legalMoves[i].length; j++) {
legalMoves[i][j] = false;
}
}
}
// add legal moves for selected piece
static void addLegalMoves(int player, int row, int column){
if(player == 1 || player == 3) {
// can jump
if(checkGrid(row-1,column-1) & checkGrid(row-2,column-2))
if((pieces[row-1][column-1] == 2 || pieces[row-1][column-1] == 4) & pieces[row-2][column - 2] == 0) legalMoves[row-2][column-2] = true;
if(checkGrid(row-1,column+1) & checkGrid(row-2,column+2))
if ((pieces[row-1][column + 1] == 2 || pieces[row-1][column + 1] == 4) & pieces[row-2][column + 2] == 0) legalMoves[row-2][column+2] = true;
// can move diagonally
if(checkGrid(row-1,column-1))
if(pieces[row-1][column-1] == 0) legalMoves[row-1][column-1] = true;
if(checkGrid(row-1,column+1))
if(pieces[row-1][column+1] == 0) legalMoves[row-1][column+1] = true;
}
if(player == 2 || player == 4) {
// can jump
if(checkGrid(row+1,column+1) & checkGrid(row+2,column+2))
if((pieces[row+1][column+1] == 1 || pieces[row+1][column+1] == 3) & pieces[row+2][column+2] == 0) legalMoves[row+2][column+2] = true;
if(checkGrid(row+1,column-1) & checkGrid(row+2,column-2))
if ((pieces[row+1][column-1] == 1 || pieces[row+1][column-1] == 3) & pieces[row+2][column-2] == 0) legalMoves[row+2][column-2] = true;
// can move diagonally
if(checkGrid(row+1,column+1))
if(pieces[row+1][column+1] == 0) legalMoves[row+1][column+1] = true;
if(checkGrid(row+1,column-1))
if(pieces[row+1][column-1] == 0) legalMoves[row+1][column-1] = true;
}
if(pieces[row][column] == 3) {
// can jump
if(checkGrid(row+1,column+1) & checkGrid(row+2,column+2))
if((pieces[row+1][column+1] == 2 || pieces[row+1][column+1] == 4) & pieces[row+2][column+2] == 0) legalMoves[row+2][column+2] = true;
if(checkGrid(row+1,column-1) & checkGrid(row+2,column-2))
if ((pieces[row+1][column-1] == 2 || pieces[row+1][column-1] == 2) & pieces[row+2][column-2] == 0) legalMoves[row+2][column-2] = true;
// can move diagonally
if(checkGrid(row+1,column+1))
if(pieces[row+1][column+1] == 0) legalMoves[row+1][column+1] = true;
if(checkGrid(row+1,column-1))
if(pieces[row+1][column-1] == 0) legalMoves[row+1][column-1] = true;
}
// if player 2 is king
if(pieces[row][column] == 4) {
// can jump
if(checkGrid(row-1,column-1) & checkGrid(row-2,column-2))
if((pieces[row-1][column-1] == 1 || pieces[row-1][column-1] == 3) & pieces[row-2][column - 2] == 0) legalMoves[row-2][column-2] = true;
if(checkGrid(row-1,column+1) & checkGrid(row-2,column+2))
if ((pieces[row-1][column + 1] == 1 || pieces[row-1][column + 1] == 3) & pieces[row-2][column + 2] == 0) legalMoves[row-2][column+2] = true;
// can move diagonally
if(checkGrid(row-1,column-1))
if(pieces[row-1][column-1] == 0) legalMoves[row-1][column-1] = true;
if(checkGrid(row-1,column+1))
if(pieces[row-1][column+1] == 0) legalMoves[row-1][column+1] = true;
}
}
// move piece function
static void movePiece(int row,int column,int toRow,int toColumn, int player) {
pieces[row][column] = 0;
if(Math.abs(toRow - row) == 1 && Math.abs(toColumn - column) == 1) {
pieces[toRow][toColumn] = player;
}
else if(toRow - row == -2 && toColumn - column == -2) {
pieces[toRow + 1][toColumn + 1] = 0;
pieces[toRow][toColumn] = player;
}
else if(toRow - row == -2 && toColumn - column == 2) {
pieces[toRow + 1][toColumn - 1] = 0;
pieces[toRow][toColumn] = player;
}
else if(toRow - row == 2 && toColumn - column == 2) {
pieces[toRow - 1][toColumn - 1] = 0;
pieces[toRow][toColumn] = player;
}
else if(toRow - row == 2 && toColumn - column == -2) {
pieces[toRow - 1][toColumn + 1] = 0;
pieces[toRow][toColumn] = player;
}
else {
return;
}
// turn kings
if(player == 1 && toRow == 0) pieces[toRow][toColumn] = 3;
if(player == 2 && toRow == 7) pieces[toRow][toColumn] = 4;
}
// checks for game over after player turn finished
static void checkGameOver(){
int play1 = 0;
int play2 = 0;
for(int i = 0; i < pieces.length;i++) {
for(int j = 0; j < pieces[i].length; j++) {
if(pieces[i][j] == 1 || pieces[i][j] == 3) play1++;
if(pieces[i][j] == 2 || pieces[i][j] == 4) play2++;
}
}
if(play1 == 0) {
System.out.println("PLAYER 2 WON\n");
System.out.println("GAMEOVER\n");
gameOver = true;
}
if(play2 == 0) {
System.out.println("PLAYER 1 WON\n");
System.out.println("GAMEOVER\n");
gameOver = true;
}
}
boolean blackTile(int i, int j){
if (j % 2 == 0 && i % 2 != 0 || j % 2 != 0 && i % 2 == 0)
return true;
else
return false;
}
void drawGrid(Graphics g){
left = iX(-rWidth/2);
right = iX(rWidth/2);
top = iY(rHeight/2);
bottom = iY(-rHeight/2);
dY = bottom-top; dX = right-left;
boxWidth = dX/10; boxHeight = dY/10;
numBoxes = 8; //Board is 8x8
circleWidth = (boxWidth*2)/3; circleHeight = (boxHeight*2)/3;
leftStart = left + boxWidth; topStart = top + boxHeight; //Leave some whitespace around the board
leftCircleStart = leftStart + boxWidth/6; topCircleStart = topStart + boxHeight/6;
//Following polygons create the 3D illusion
int[] topBottomX = {left+boxWidth, left+boxWidth, left+boxWidth*9, left+boxWidth*9};
int[] topYBoard = {top+((boxHeight*32)/35), top+boxHeight, top+boxHeight, top+((boxHeight*32)/35)};
int[] leftXBoard = {left+((boxWidth*32)/35), left+boxWidth, left+boxWidth, left+((boxWidth*32)/35)};
int[] leftRightY = {top+((boxHeight*32)/35), top+((boxHeight*32)/35), bottom-((boxHeight*32)/35), bottom-((boxHeight*32)/35)};
int[] rightXBoard = {right-((boxWidth*32)/35), left+boxWidth*9, left+boxWidth*9, right-((boxWidth*32)/35)};
int[] bottomYBoard = {bottom-((boxHeight*32)/35), top+boxHeight*9, top+boxHeight*9, bottom-((boxHeight*32)/35)};
int[] rightShadowX = {right-((boxWidth*32)/35), right-((boxWidth*27)/35), right-((boxWidth*27)/35), right-((boxWidth*32)/35)};
int[] rightShadowY = {top+((boxHeight*32)/35), top+((boxHeight*43)/35), bottom-((boxHeight*27)/35), bottom-((boxHeight*32)/35)};
int[] bottomShadowX = {left+((boxWidth*32)/35), left+((boxWidth*43)/35), right-((boxWidth*27)/35), right-((boxWidth*32)/35)};
int[] bottomShadowY = {bottom-((boxHeight*32)/35), bottom-((boxHeight*27)/35), bottom-((boxHeight*27)/35), bottom-((boxHeight*32)/35)};
Polygon topBoard = new Polygon(topBottomX, topYBoard, 4);
Polygon leftBoard = new Polygon(leftXBoard, leftRightY, 4);
Polygon rightBoard = new Polygon(rightXBoard, leftRightY, 4);
Polygon bottomBoard = new Polygon(topBottomX, bottomYBoard, 4);
Polygon rightShadow = new Polygon(rightShadowX, rightShadowY, 4);
Polygon bottomShadow = new Polygon(bottomShadowX, bottomShadowY, 4);
g.setColor(new Color(102, 0, 0));
g.drawPolygon(topBoard); g.drawPolygon(leftBoard); g.drawPolygon(rightBoard); g.drawPolygon(bottomBoard);
g.fillPolygon(topBoard); g.fillPolygon(leftBoard); g.fillPolygon(rightBoard); g.fillPolygon(bottomBoard);
g.setColor(Color.BLACK);
g.drawPolygon(rightShadow); g.drawPolygon(bottomShadow);
g.setColor(new Color(51, 0, 0));
g.fillPolygon(rightShadow); g.fillPolygon(bottomShadow);
g.setColor(Color.BLACK);
for (int i = 0; i < numBoxes; i++) {
for (int j = 0; j < numBoxes; j++) {
g.drawRect(leftStart+(boxWidth*j), topStart+(boxHeight*i), boxWidth, boxHeight);
if (blackTile(i, j)){
g.setColor(Color.BLACK);
g.fillRect(leftStart+(boxWidth*j), topStart+1+(boxHeight*i), boxWidth, boxHeight-1);
}
}
}
if (gameStart == false) {
drawStartPieces(g);
}
}
void drawPieces(Graphics g){
Font kingFont = new Font("Dialog", Font.BOLD, 14);
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
if(pieces[i][j] == 1){
g.setColor(Color.BLACK);
g.drawOval(leftCircleStart+(boxWidth*j), topCircleStart+(boxHeight*i), circleWidth, circleHeight);
g.setColor(Color.RED);
g.fillOval(leftCircleStart+1+(boxWidth*j), topCircleStart+1+(boxHeight*i), circleWidth-1, circleHeight-1);
}
else if(pieces[i][j] == 2){
g.setColor(Color.BLACK);
g.drawOval(leftCircleStart+(boxWidth*j), topCircleStart+(boxHeight*i), circleWidth, circleHeight);
g.setColor(new Color(0, 128, 255));
g.fillOval(leftCircleStart+1+(boxWidth*j), topCircleStart+1+(boxHeight*i), circleWidth-1, circleHeight-1);
}
else if(pieces[i][j] == 3){
g.setColor(Color.BLACK);
g.drawOval(leftCircleStart+(boxWidth*j), topCircleStart+(boxHeight*i), circleWidth+1, circleHeight+1);
g.setColor(new Color(255, 51, 111));
g.fillOval(leftCircleStart+1+(boxWidth*j), topCircleStart+1+(boxHeight*i), circleWidth-1, circleHeight-1);
g.setColor(Color.BLACK);
g.setFont(kingFont);
g.drawString("KING", leftCircleStart+(boxWidth*j)+circleWidth/6, topCircleStart+(13*circleHeight)/20+(boxHeight*i));
}
else if(pieces[i][j] == 4){
g.setColor(Color.BLACK);
g.drawOval(leftCircleStart+(boxWidth*j), topCircleStart+(boxHeight*i), circleWidth, circleHeight);
g.setColor(new Color(111, 51, 255));
g.fillOval(leftCircleStart+1+(boxWidth*j), topCircleStart+1+(boxHeight*i), circleWidth-1, circleHeight-1);
g.setColor(Color.BLACK);
g.setFont(kingFont);
g.drawString("KING", leftCircleStart+1+(boxWidth*j)+circleWidth/6, topCircleStart+(13*circleHeight)/20+(boxHeight*i));
}
else if(legalMoves[i][j] == true){
g.setColor(Color.BLACK);
g.drawOval(leftCircleStart+(boxWidth*j), topCircleStart+(boxHeight*i), circleWidth, circleHeight);
int alpha = 127;
g.setColor(new Color(255, 255, 0, alpha));
g.fillOval(leftCircleStart+1+(boxWidth*j), topCircleStart+1+(boxHeight*i), circleWidth-1, circleHeight-1);
}
else if(pieces[i][j] == 0){
g.setColor(Color.BLACK);
g.drawOval(leftCircleStart+(boxWidth*j), topCircleStart+(boxHeight*i), circleWidth, circleHeight);
g.fillOval(leftCircleStart+(boxWidth*j), topCircleStart+(boxHeight*i), circleWidth, circleHeight);
}
else{
g.setColor(Color.WHITE);
g.fillRect(leftStart+(boxWidth*j), topStart+1+(boxHeight*i), boxWidth, boxHeight-1);
}
}
}
}
void drawStartPieces(Graphics g) {
g.setFont(unselectedFont);
for (int i = 0; i < 8; i++) {
g.drawString(Character.toString((char)i+65), (left+(boxWidth/2)), (top+(boxHeight*i)+((3*boxHeight)/2)));
}
for (int j = 0; j < 8; j++) {
g.drawString(new String(""+j), (left+(boxWidth*j)+((3*boxWidth)/2)), (top+(4*boxHeight)/5));
}
drawPieces(g);
}
void selectPiece(Graphics g, int i, int j) {
g.setColor(Color.BLACK);
g.setFont(selectedFont);
g.drawString(Character.toString((char)i+65), (left+(boxWidth/2)), (top+(boxHeight*i)+((3*boxHeight)/2)));
g.drawString(new String(""+j), (left+(boxWidth*j)+((3*boxWidth)/2)), (top+(4*boxHeight)/5));
g.setFont(unselectedFont);
for (int w = 0; w < 8; w++) {
if (w == i) {
g.setFont(selectedFont);
g.drawString(Character.toString((char)i+65), (left+(boxWidth/2)), (top+(boxHeight*i)+((3*boxHeight)/2)));
g.setFont(unselectedFont);
}
}
for (int y = 0; y < 8; y++) {
if (y == j) {
g.setFont(selectedFont);
g.drawString(new String(""+j), (left+(boxWidth*j)+((3*boxWidth)/2)), (top+(4*boxHeight)/5));
g.setFont(unselectedFont);
}
}
for(int a = 0; a < 8; a++){
for(int b = 0; b < 8; b++){
if(checkGrid(i,j) && pieces[i][j] == 1){
g.setColor(Color.RED.darker());
g.drawOval(leftCircleStart+(boxWidth*j), topCircleStart+(boxHeight*i), circleWidth, circleHeight);
g.fillOval(leftCircleStart+(boxWidth*j), topCircleStart+(boxHeight*i), circleWidth, circleHeight);
}
else if(checkGrid(i,j) && pieces[i][j] == 2){
g.setColor(Color.BLUE.darker());
g.drawOval(leftCircleStart+(boxWidth*j), topCircleStart+(boxHeight*i), circleWidth, circleHeight);
g.fillOval(leftCircleStart+(boxWidth*j), topCircleStart+(boxHeight*i), circleWidth, circleHeight);
}
else if(checkGrid(i,j) && pieces[i][j] == 3){
g.setColor(Color.RED.darker());
g.drawOval(leftCircleStart+(boxWidth*j), topCircleStart+(boxHeight*i), circleWidth, circleHeight);
g.fillOval(leftCircleStart+(boxWidth*j), topCircleStart+(boxHeight*i), circleWidth, circleHeight);
}
else if(checkGrid(i,j) && pieces[i][j] == 4){
g.setColor(Color.BLUE.darker());
g.drawOval(leftCircleStart+(boxWidth*j), topCircleStart+(boxHeight*i), circleWidth, circleHeight);
g.fillOval(leftCircleStart+(boxWidth*j), topCircleStart+(boxHeight*i), circleWidth, circleHeight);
}
else{
selected = 0;
}
}
}
}
void initgr() {
Dimension d = getSize();
maxX = d.width - 1; maxY = d.height - 1;
pixelSize = Math.max(rWidth / maxX, rHeight / maxY);
centerX = maxX / 2; centerY = maxY / 2;
}
int iX(float x) {return Math.round(centerX + x / pixelSize);}
int iY(float y) {return Math.round(centerY - y / pixelSize);}
float fx(int x) {return (x - centerX) * pixelSize;}
float fy(int y) {return (centerY - y) * pixelSize;}
public void paint(Graphics g) {
initgr();
drawGrid(g);
// Player 1 Turn
if (xP != 1000000 && yP != 1000000 && playerTurn == 1) {
System.out.println("Player 1 Turn");
int js = (xP - leftStart)/boxWidth;
int is = (yP - topStart)/boxHeight;
char js1 = 'A';
for(int i = 0; i < 8; i++){
if(is == i && i == 0)
js1 = 'A';
else if(is == i && i ==1)
js1 = 'B';
else if(is == i && i ==2)
js1 = 'C';
else if(is == i && i ==3)
js1 = 'D';
else if(is == i && i ==4)
js1 = 'E';
else if(is == i && i ==5)
js1 = 'F';
else if(is == i && i ==6)
js1 = 'G';
else if(is == i && i ==7)
js1 = 'H';
}
System.out.println("Coordinates " + js + " " + js1);
selectPiece(g, is, js);
if(selectedQ && checkGrid(is,js) && legalMoves[is][js] == true){
System.out.println("Activated");
if(blackTile(is, js)){
movePiece(oldX,oldY,is,js,pieces[oldX][oldY]);
resetMoves();
drawPieces(g);
selectedQ = false;
checkGameOver();
playerTurn = 2;
}
}
else if(checkGrid(is,js) && (pieces[is][js] == 1 || pieces[is][js] == 3)) {
System.out.println("Piece Selected");
selectPiece(g, is, js);
resetMoves();
addLegalMoves(pieces[is][js],is,js);
drawPieces(g);
oldX = is;
oldY = js;
selectedQ = true;
}
else if (selected == 0 && is >=0 && is <= 7 && js >=0 && js <= 7) {
selectPiece(g, is, js);
}
}
// Player 2 Turn
if (xP != 1000000 && yP != 1000000 && playerTurn == 2) {
System.out.println("Player 2 Turn");
int js = (xP - leftStart)/boxWidth;
int is = (yP - topStart)/boxHeight;
char js1 = 'A';
for(int i = 0; i < 8; i++){
if(is == i && i == 0)
js1 = 'A';
else if(is == i && i ==1)
js1 = 'B';
else if(is == i && i ==2)
js1 = 'C';
else if(is == i && i ==3)
js1 = 'D';
else if(is == i && i ==4)
js1 = 'E';
else if(is == i && i ==5)
js1 = 'F';
else if(is == i && i ==6)
js1 = 'G';
else if(is == i && i ==7)
js1 = 'H';
}
System.out.println("Coordinates " + js1 + " " + is);
selectPiece(g, is, js);
if(selectedQ && checkGrid(is,js) && legalMoves[is][js] == true){
System.out.println("Activated");
if(blackTile(is, js)){
movePiece(oldX,oldY,is,js,pieces[oldX][oldY]);
resetMoves();
drawPieces(g);
selectedQ = false;
checkGameOver();
playerTurn = 1;
}
}
else if(pieces[is][js] == 2 || pieces[is][js] == 4) {
System.out.println("Piece Selected");
selectPiece(g, is, js);
resetMoves();
addLegalMoves(pieces[is][js],is,js);
drawPieces(g);
oldX = is;
oldY = js;
selectedQ = true;
}
else if (selected == 0 && is >=0 && is <= 7 && js >=0 && js <= 7) {
selectPiece(g, is, js);
}
}
}
}