-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
471 lines (378 loc) · 16.4 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
package chessgui;
import chessgui.pieces.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;
import java.util.Timer;
import java.util.TimerTask;
@SuppressWarnings("serial")
public class Board extends JComponent {
public int limit = 60;
public static Timer timer;
public static Timer timer2;
public int turnCounter = 0;
private static Image NULL_IMAGE = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
private final int Square_Width = 77;
public ArrayList<Piece> Red_Pieces;
public ArrayList<Piece> Blue_Pieces;
public ArrayList<DrawingShape> Static_Shapes;
public ArrayList<DrawingShape> Piece_Graphics;
public Piece Active_Piece;
private final int rows = 9;
private final int cols = 10;
private Integer[][] BoardGrid;
private String board_file_path = "images" + File.separator + "chessBoard.png";
private String active_square_file_path = "images" + File.separator + "active_square.png";
public void checkWinner() {
//create some variables for later use
boolean redGeneral = false;
boolean blueGeneral = false;
int winner;
//check if red general still on the chess board
for (int i = 0; i < Red_Pieces.size(); i++) {
String path = Red_Pieces.get(i).getFilePath();
if (path.equals("General.png")) {
redGeneral = true;
}
}
//check if blue general still on the chess board
for (int i = 0; i < Blue_Pieces.size(); i++) {
String path = Blue_Pieces.get(i).getFilePath();
if (path.equals("General.png")) {
blueGeneral = true;
}
}
//if red general is not on the chess board, blue wins
if (!redGeneral) {
JOptionPane.showMessageDialog(null, "Blue wins.\nConfirm to exit the game.");
System.exit(0);
} else if (!blueGeneral) {
JOptionPane.showMessageDialog(null, "Red wins.\nConfirm to exit the game.");
System.exit(0);
}
//because the general is always the first item in this ArrayList, check the place of first variable
int redKingY = Red_Pieces.get(0).getY();
int blueKingY = Blue_Pieces.get(0).getY();
int redKingX = Red_Pieces.get(0).getX();
int blueKingX = Blue_Pieces.get(0).getX();
//count are there any piece in between
int countPiece = 0;
if(redKingX == blueKingX){
for (int i = redKingY + 1; i < blueKingY; i++){
Piece p = getPiece(redKingX, i);
if (p != null) {
countPiece++;
}
}
//if the two general meets, return the winner situation and stop the program
if (countPiece == 0) {
if(turnCounter % 2 == 0) {
JOptionPane.showMessageDialog(null, "Blue General meets Red General.\nRed Loses.");
System.exit(0);
} else if (turnCounter % 2 == 1) {
JOptionPane.showMessageDialog(null, "Blue General meets Red General.\nRed Loses.");
System.exit(0);
}
}
}
}
public void initGrid() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
BoardGrid[i][j] = 0;
}
}
//Image white_piece = loadImage("images/white_pieces/" + piece_name + ".png");
//Image black_piece = loadImage("images/black_pieces/" + piece_name + ".png");
Red_Pieces.add(new General(4, 0, true, "General.png", this));
Red_Pieces.add(new Guard(3, 0, true, "Guard.png", this));
Red_Pieces.add(new Guard(5, 0, true, "Guard.png", this));
Red_Pieces.add(new Elephant(2, 0, true, "Elephant.png", this));
Red_Pieces.add(new Elephant(6, 0, true, "Elephant.png", this));
Red_Pieces.add(new Horse(1, 0, true, "Horse.png", this));
Red_Pieces.add(new Horse(7, 0, true, "Horse.png", this));
Red_Pieces.add(new Rook(0, 0, true, "Rook.png", this));
Red_Pieces.add(new Rook(8, 0, true, "Rook.png", this));
Red_Pieces.add(new Cannon(1, 2, true, "Cannon.png", this));
Red_Pieces.add(new Cannon(7, 2, true, "Cannon.png", this));
Red_Pieces.add(new Pawn(0, 3, true, "Pawn.png", this));
Red_Pieces.add(new Pawn(2, 3, true, "Pawn.png", this));
Red_Pieces.add(new Pawn(4, 3, true, "Pawn.png", this));
Red_Pieces.add(new Pawn(6, 3, true, "Pawn.png", this));
Red_Pieces.add(new Pawn(8, 3, true, "Pawn.png", this));
Blue_Pieces.add(new General(4, 9, false, "General.png", this));
Blue_Pieces.add(new Guard(3, 9, false, "Guard.png", this));
Blue_Pieces.add(new Guard(5, 9, false, "Guard.png", this));
Blue_Pieces.add(new Elephant(2, 9, false, "Elephant.png", this));
Blue_Pieces.add(new Elephant(6, 9, false, "Elephant.png", this));
Blue_Pieces.add(new Horse(1, 9, false, "Horse.png", this));
Blue_Pieces.add(new Horse(7, 9, false, "Horse.png", this));
Blue_Pieces.add(new Rook(0, 9, false, "Rook.png", this));
Blue_Pieces.add(new Rook(8, 9, false, "Rook.png", this));
Blue_Pieces.add(new Cannon(1, 7, false, "Cannon.png", this));
Blue_Pieces.add(new Cannon(7, 7, false, "Cannon.png", this));
Blue_Pieces.add(new Pawn(0, 6, false, "Pawn.png", this));
Blue_Pieces.add(new Pawn(2, 6, false, "Pawn.png", this));
Blue_Pieces.add(new Pawn(4, 6, false, "Pawn.png", this));
Blue_Pieces.add(new Pawn(6, 6, false, "Pawn.png", this));
Blue_Pieces.add(new Pawn(8, 6, false, "Pawn.png", this));
}
public Board() {
BoardGrid = new Integer[rows][cols];
Static_Shapes = new ArrayList();
Piece_Graphics = new ArrayList();
Red_Pieces = new ArrayList();
Blue_Pieces = new ArrayList();
initGrid();
this.setBackground(new Color(37, 13, 84));
this.setPreferredSize(new Dimension(850, 790));
this.setMinimumSize(new Dimension(100, 100));
this.setMaximumSize(new Dimension(1000, 1000));
this.addMouseListener(mouseAdapter);
this.addComponentListener(componentAdapter);
this.addKeyListener(keyAdapter);
this.setVisible(true);
this.requestFocus();
drawBoard();
}
private void drawBoard() {
Piece_Graphics.clear();
Static_Shapes.clear();
//initGrid();
Image board = loadImage(board_file_path);
Static_Shapes.add(new DrawingImage(board, new Rectangle2D.Double(0, 0, board.getWidth(null), board.getHeight(null))));
if (Active_Piece != null) {
Image active_square = loadImage("images" + File.separator + "active_square.png");
Static_Shapes.add(new DrawingImage(active_square, new Rectangle2D.Double((17 + Square_Width * Active_Piece.getX()), (10 + Square_Width * Active_Piece.getY()), active_square.getWidth(null), active_square.getHeight(null))));
}
for (int i = 0; i < Red_Pieces.size(); i++) {
int COL = Red_Pieces.get(i).getX();
int ROW = Red_Pieces.get(i).getY();
Image piece = loadImage("images" + File.separator + "blue_pieces" + File.separator + Red_Pieces.get(i).getFilePath());
Piece_Graphics.add(new DrawingImage(piece, new Rectangle2D.Double((17 + Square_Width * COL), (10 + Square_Width * ROW), piece.getWidth(null), piece.getHeight(null))));
}
for (int i = 0; i < Blue_Pieces.size(); i++) {
int COL = Blue_Pieces.get(i).getX();
int ROW = Blue_Pieces.get(i).getY();
Image piece = loadImage("images" + File.separator + "red_pieces" + File.separator + Blue_Pieces.get(i).getFilePath());
Piece_Graphics.add(new DrawingImage(piece, new Rectangle2D.Double((17 + Square_Width * COL), (10 + Square_Width * ROW), piece.getWidth(null), piece.getHeight(null))));
}
this.repaint();
}
public Piece getPiece(int x, int y) {
for (Piece p : Red_Pieces) {
if (p.getX() == x && p.getY() == y) {
return p;
}
}
for (Piece p : Blue_Pieces) {
if (p.getX() == x && p.getY() == y) {
return p;
}
}
return null;
}
private MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
int d_X = e.getX();
int d_Y = e.getY();
int Clicked_Row = d_Y / Square_Width;
int Clicked_Column = d_X / Square_Width;
boolean is_whites_turn = true;
if (turnCounter % 2 == 1) {
is_whites_turn = false;
}
Piece clicked_piece = getPiece(Clicked_Column, Clicked_Row);
if (Active_Piece == null && clicked_piece != null
&& ((is_whites_turn && clicked_piece.isBlue()) || (!is_whites_turn && clicked_piece.isRed()))) {
Active_Piece = clicked_piece;
} else if (Active_Piece != null && Active_Piece.getX() == Clicked_Column && Active_Piece.getY() == Clicked_Row) {
Active_Piece = null;
} else if (Active_Piece != null && Active_Piece.canMove(Clicked_Column, Clicked_Row)
&& ((is_whites_turn && Active_Piece.isBlue()) || (!is_whites_turn && Active_Piece.isRed()))) {
// if piece is there, remove it so we can be there
if (clicked_piece != null) {
if (clicked_piece.isBlue()) {
Red_Pieces.remove(clicked_piece);
} else {
Blue_Pieces.remove(clicked_piece);
}
}
// do move
Active_Piece.setX(Clicked_Column);
Active_Piece.setY(Clicked_Row);
// if piece is a pawn set has_moved to true
if (Active_Piece.getClass().equals(Pawn.class)) {
Pawn castedPawn = (Pawn) (Active_Piece);
castedPawn.setHasMoved(true);
}
checkWinner();
countDown();
turnCounter++;
Active_Piece = null;
}
drawBoard();
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
}
};
private void adjustShapePositions(double dx, double dy) {
Static_Shapes.get(0).adjustPosition(dx, dy);
this.repaint();
}
private Image loadImage(String imageFile) {
try {
return ImageIO.read(new File(imageFile));
} catch (IOException e) {
return NULL_IMAGE;
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
drawBackground(g2);
drawShapes(g2);
}
private void drawBackground(Graphics2D g2) {
g2.setColor(getBackground());
g2.fillRect(0, 0, getWidth(), getHeight());
}
private void drawShapes(Graphics2D g2) {
for (DrawingShape shape : Static_Shapes) {
shape.draw(g2);
}
for (DrawingShape shape : Piece_Graphics) {
shape.draw(g2);
}
}
private ComponentAdapter componentAdapter = new ComponentAdapter() {
@Override
public void componentHidden(ComponentEvent e) {
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentResized(ComponentEvent e) {
}
@Override
public void componentShown(ComponentEvent e) {
}
};
private KeyAdapter keyAdapter = new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
};
/**
* This timer will countdown for 60 seconds after either player
* move their pieces
* @param black the side of the action performer
* @return an integer for determaining the loser
*/
public int countDown() {
limit = 60;
if (turnCounter == 0) {
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
limit--;
BoardFrame.timeSet(limit);//upload the remaining time to the GUI
if (limit == 0) {
timer.cancel();//stop the timer
System.out.println("1");
JOptionPane.showMessageDialog(null,"Time out!\nRound over.");
System.exit(0);
}
}
}, 0, 1000);//start 0 seconds later, repeat once per second
} else if (turnCounter % 2 == 1) {
timer.cancel();
timer2 = new Timer();//create a timer for black team
timer2.scheduleAtFixedRate(new TimerTask() {
public void run() {
limit--;
BoardFrame.timeSet(limit);//upload the remaining time to the GUI
if (limit == 0) {
System.out.println("1");
timer2.cancel();//stop the timer
JOptionPane.showMessageDialog(null,"Red time out!\nBlue wins.");
System.exit(0);
}
}
}, 0, 1000);//start 0 seconds later, repeat once per second
} else if(turnCounter % 2 == 0) {
timer2.cancel();
timer = new Timer();//create a timer for red team
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
limit--;
BoardFrame.timeSet(limit);//upload the remaining time to the GUI
if (limit == 0) {
System.out.println("1");
timer.cancel();//stop the timer
JOptionPane.showMessageDialog(null,"Blue time out!\nRed wins.");
System.exit(0);
}
}
}, 0, 1000);//start 0 seconds later, repeat once per second
}
if (limit == 0 && turnCounter % 2 == 0) {
return 1; //black lose
} else if (limit == 0 && turnCounter % 2 == 1) {
return -1; //red lose
} else {
return 0; //no result
}
}
}
interface DrawingShape {
boolean contains(Graphics2D g2, double x, double y);
void adjustPosition(double dx, double dy);
void draw(Graphics2D g2);
}
class DrawingImage implements DrawingShape {
public Image image;
public Rectangle2D rect;
public DrawingImage(Image image, Rectangle2D rect) {
this.image = image;
this.rect = rect;
}
@Override
public boolean contains(Graphics2D g2, double x, double y) {
return rect.contains(x, y);
}
@Override
public void adjustPosition(double dx, double dy) {
rect.setRect(rect.getX() + dx, rect.getY() + dy, rect.getWidth(), rect.getHeight());
}
@Override
public void draw(Graphics2D g2) {
Rectangle2D bounds = rect.getBounds2D();
g2.drawImage(image, (int) bounds.getMinX(), (int) bounds.getMinY(), (int) bounds.getMaxX(), (int) bounds.getMaxY(),
0, 0, image.getWidth(null), image.getHeight(null), null);
}
}