This repository was archived by the owner on Jun 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCXBoardPanel.java
More file actions
729 lines (607 loc) · 21.4 KB
/
Copy pathCXBoardPanel.java
File metadata and controls
729 lines (607 loc) · 21.4 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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
/*
* Copyright (C) 2022 Lamberto Colazzo
*
* This file is part of the ConnectX software developed for the
* Intern ship of the course "Information technology", University of Bologna
* A.Y. 2021-2022.
*
* ConnectX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details; see <https://www.gnu.org/licenses/>.
*/
package connectx;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import connectx.CXGame.CXGameType;
import connectx.CXGame.CXPlayerType;
/**
* Inner class for custom graphics drawing.
*/
public class CXBoardPanel extends JPanel implements MouseListener, MouseMotionListener, Serializable {
protected final int NUMBER_OF_ROWS;
protected final int NUMBER_OF_COLS;
protected final int WIN_ANIMATION_FRAMES;
private static final int TIMEOUT = 10;
public static final int PLAYER1 = 0; // PLAYER1
public static final int PLAYER2 = 1; // PLAYER2
/** Scoring system */
private static int WINSCORE = 3;
private static int DRAWSCORE = 1;
protected int[] ScorePlayer = new int[2];
public int Board_Top_Border; // BOARD_TOP_BORDER
protected CXPlayerType[] Player;
protected CXPlayer[] ComPlayer;
protected CXGameType gameType;
// Stroke defaultStroke;
CXGameState gameState;
CXBoard board;
protected JLabel statusBar;
/* for drawing the grid */
int cellGap;
int boardWidth;
int boardHeight;
int extraBorder;
/* for animating coin drop */
int textAnimationInt; // used to control movement of text and text box across screen (want to stop in
// middle)
boolean animatingCoinDrop;
int animationColumn;
int animationRow;
int animationFrame;
int animationStartPosition;
double animationFrameAcceleration;
/*
* control mouse pointer which is the ball about to be dropped - pointer
* position is limited.
*/
boolean drawPointerOnMouse = false; // when mouse is in critical zone a piece is shown around it.
int mousePointerHorizontalPosition = 0;
int mousePointerVerticalPosition = 0; // this and above are all set on mouseMove
/* are used to monitor the game situation and animate accordingly */
int winAnimationFrame;
int ovalWidth;
int ovalWidthModifier = 1;
boolean fullColum = false;
boolean winFirstDetection = true;
Image dbImage; // for double buffering
/* gradient used for drawing pointers. */
GradientPaint gpYellow; // for the yellow player's moves
GradientPaint gpRed; // for the red player's moves
/* seriaisable */
private static final long serialVersionUID = 1L;
public CXBoardPanel(CXBoard board, CXGameType type, Border bord, int cell_size, CXPlayer[] ComPlayer,
CXPlayerType[] Player, JLabel statusBar) {
gameState = board.gameState;
gameType = type;
this.board = board;
this.ComPlayer = ComPlayer;
this.Player = Player;
this.statusBar = statusBar;
NUMBER_OF_ROWS = this.board.M;
NUMBER_OF_COLS = this.board.N;
cellGap = cell_size;
WIN_ANIMATION_FRAMES = cellGap * 4;
setLayout(new GridLayout(1, 1, 5, 5)); // a single cell
// setBorder(bord);
// setBackground(Color.RED);
addMouseListener(this);
addMouseMotionListener(this);
initScore();
initiateAnimationState(); // set the initial parameters for the coin animation
}
public void initScore() {
if (ScorePlayer == null) {
ScorePlayer[0] = board.currentPlayer();
ScorePlayer[1] = board.currentPlayer() % 1;
} else {
int tmp = ScorePlayer[0];
ScorePlayer[0] = ScorePlayer[1];
ScorePlayer[1] = tmp;
}
winFirstDetection = true;
}
public void initiateAnimationState() {
ovalWidth = cellGap - 18;
setAnimationState(false, 0, 0, 0);
winAnimationFrame = 0;
}
public void setAnimationState(boolean b, int col, int rowBegin, int a) {
/* variables are reset every time a coin is dropped */
animationFrame = 0;
animationFrameAcceleration = 1;
animationColumn = col;
animatingCoinDrop = b;
animationStartPosition = rowBegin; // this is the position of the mouse pointer - drops from the pointer
animationRow = a; // last move in Game is the one we are animating.
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
effectDoubleBuffering(g2d);
// Print status-bar message
switch (board.gameState()) {
case OPEN:
statusBar.setForeground(Color.BLACK);
String colorPlayer = board.currentPlayer() == 0 ? "Yellow" : "Red";
String msg = Player[board.currentPlayer()] == CXPlayerType.COMPUTER ? "Click to run"
: "Click on white bar to select column";
String name = Player[board.currentPlayer()] == CXPlayerType.COMPUTER
? ComPlayer[board.currentPlayer()].playerName()
: "Human";
statusBar.setText(colorPlayer + "'s Turn (" + name + ") - " + msg);
break;
case DRAW:
statusBar.setForeground(Color.RED);
statusBar.setText("Draw! Click reset to play again.");
break;
case WINP1:
String name1 = Player[0] == CXPlayerType.COMPUTER ? ComPlayer[0].playerName() : "Human";
statusBar.setForeground(Color.RED);
statusBar.setText("Yellow (" + name1 + ") Won! Click reset to play again.");
break;
case WINP2:
String name2 = Player[1] == CXPlayerType.COMPUTER ? ComPlayer[1].playerName() : "Human";
statusBar.setForeground(Color.RED);
statusBar.setText("Red (" + name2 + ") Won! Click reset to play again.");
break;
}
repaint();
}
public void effectDoubleBuffering(Graphics2D g2) {
if (dbImage == null) {
dbImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
}
Graphics2D offG = (Graphics2D) (dbImage.getGraphics()); // clear the screen
Board_Top_Border = (getHeight() - CXGame.EXTRA_ORIZONTAL_BORDER);
boardWidth = getWidth();
boardHeight = getHeight();
extraBorder = boardHeight - Board_Top_Border - (cellGap * NUMBER_OF_ROWS);
makeNextFrame(offG); // Put the dbImage image on the screen.
g2.drawImage(dbImage, 0, 0, null);
}
public void makeNextFrame(Graphics2D g2) {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// defaultStroke = g2.getStroke();
g2.setPaint(Color.WHITE);
g2.fillRect(0, 0, getWidth(), getHeight());
drawGameBoard(g2);
}
/* draw grid board */
private void drawShapeCells(Graphics2D g2) {
for (int i = 0; i < NUMBER_OF_ROWS; i++) {
for (int j = 0; j < NUMBER_OF_COLS; j++) {
Area af2 = new Area(cell(j * cellGap, i * cellGap + Board_Top_Border + extraBorder, cellGap));
af2.subtract(new Area(new Ellipse2D.Float(j * cellGap + 5,
i * cellGap + Board_Top_Border + extraBorder + 5, cellGap - 10, cellGap - 10)));
g2.setColor(Color.BLUE.darker().darker());
g2.fill(af2);
}
}
}
/* draw grid board, draw coins(marked cells) and animate win */
private void drawGameBoard(Graphics2D g2) {
if (animatingCoinDrop)
animateCoinDrop(g2);
if (drawPointerOnMouse)
drawMousePointer(g2);
drawShapeCells(g2);
CXCell[] list = board.getMarkedCells();
drawCounters(g2, list);
if ((gameState == CXGameState.WINP1 || gameState == CXGameState.WINP2) && (!animatingCoinDrop)) {
CXCell m = board.getLastMove();
animateWin(g2, winCells(m.i, m.j));
if (winFirstDetection) {
winFirstDetection = false;
switch (gameState) {
case WINP1:
ScorePlayer[0] += WINSCORE;
break;
case WINP2:
ScorePlayer[1] += WINSCORE;
break;
default:
break;
}
}
} else if (gameState == CXGameState.DRAW && (!animatingCoinDrop)) {
animateGameOverText(g2, textAnimationInt);
if (winFirstDetection) {
winFirstDetection = false;
ScorePlayer[0] += DRAWSCORE;
ScorePlayer[1] += DRAWSCORE;
}
if (textAnimationInt < (boardWidth / 2) - 100)
textAnimationInt = textAnimationInt + NUMBER_OF_COLS;
}
}
private void animateWin(Graphics2D g2, LinkedList<CXCell> moves) {
// remove the winning counters from the board (so they don't get
// drawn there as well as here)
Iterator<CXCell> iteratore = moves.iterator();
while (iteratore.hasNext()) {
CXCell m = iteratore.next();
int x = m.j * cellGap + 5;
int y = m.i * cellGap + (Board_Top_Border + extraBorder + 5);
g2.setPaint(Color.WHITE);
g2.create().fillOval(x, y, cellGap - 10, cellGap - 10);
}
if (board.currentPlayer == PLAYER1)
g2.setPaint(getRedGradientPaint());
else
g2.setPaint(getYellowGradientPaint());
/*
* winning pieces to spin - code below sorts this: ovalWidthModifier begins at 1
* but ovalWidth begins at cellGap, so first if is bypassed and
* ovalWidthModifier is set to 0 ovalwidth increases until oval is full sized
* cellGap.
*/
if ((ovalWidth == cellGap) && (winAnimationFrame < WIN_ANIMATION_FRAMES))
ovalWidthModifier = 1;
// when ovalwidth hits cellGap the width of the oval will decrease until
// it again hits cellGap.
if (ovalWidth == 0)
ovalWidthModifier = 0;
if (ovalWidthModifier == 1)
ovalWidth = winAnimationFrame % cellGap;
if (ovalWidthModifier == 0)
ovalWidth = cellGap - winAnimationFrame % cellGap;
if (ovalWidth == cellGap)
ovalWidth = 0;
else if (ovalWidth == 0)
ovalWidth = cellGap;
for (CXCell c : moves)
g2.fillOval(cellGap * c.j + 5 + (cellGap / 2 - ovalWidth / 2), Board_Top_Border + extraBorder + cellGap * c.i + 5, ovalWidth - 10, cellGap - 10);
// when game is won a token is added to winning pieces
if (winAnimationFrame >= WIN_ANIMATION_FRAMES) {
for (CXCell c : moves) {
if (board.currentPlayer == PLAYER1)
g2.setPaint(getRedGradientPaint());
else
g2.setPaint(getYellowGradientPaint());
g2.fillOval(cellGap * c.j + 5, Board_Top_Border + extraBorder + cellGap * c.i + 5, cellGap - 10, cellGap - 10);
if (cellGap == 60) {
g2.setPaint(Color.GREEN.darker());
g2.drawString("Win!", cellGap * c.j + (cellGap / 5) + 5, Board_Top_Border + extraBorder + cellGap * c.i + (cellGap / 2) + 5);
} else if (cellGap == 45) {
g2.setPaint(Color.GREEN.darker());
g2.drawString("Win!", cellGap * c.j + (cellGap / 9) + 5, Board_Top_Border + extraBorder + cellGap * c.i + (cellGap / 2) + 5);
} else {
g2.setPaint(Color.GREEN.darker());
g2.drawString("Win!", cellGap * c.j + (cellGap / 3) + 5, Board_Top_Border + extraBorder + cellGap * c.i + (cellGap / 2) + 5);
}
}
}
/*
* solution to the issue of flicker in the ovals - modular div letting me down
* on boundaries. Below ensures that there is a smooth transition between
* boundaries.
*/
if (ovalWidth == 0)
ovalWidth = cellGap;
else if ((ovalWidth == cellGap) && (winAnimationFrame < WIN_ANIMATION_FRAMES))
ovalWidth = 0;
animateGameOverText(g2, textAnimationInt);
if (winAnimationFrame < WIN_ANIMATION_FRAMES)
winAnimationFrame = winAnimationFrame + NUMBER_OF_ROWS;
if (textAnimationInt < (boardWidth / 2) - 100)
textAnimationInt = textAnimationInt + NUMBER_OF_COLS;
}
/* Game Over copy below moves from right to left until it is centralized */
private void animateGameOverText(Graphics2D g2, int textAnimationInt) {
int len;
g2.setPaint(Color.DARK_GRAY);
g2.fillRect(0 + textAnimationInt, ((boardHeight / 2)), 200, 100);
g2.setPaint(Color.white);
g2.setFont(new Font("Arial", Font.BOLD, 28));
g2.drawString("Game over", 30 + textAnimationInt, (boardHeight / 2) + 30);
if (gameState == CXGameState.WINP1)
g2.setPaint(getYellowGradientPaint());
else
g2.setPaint(getRedGradientPaint());
g2.setFont(new Font("Arial", Font.BOLD, 22));
len = getMoveResultText(gameState).length();
g2.drawString(getMoveResultText(gameState), (200 / len) + textAnimationInt, (boardHeight / 2) + 80);
}
public String getMoveResultText(CXGameState moveResult) {
switch (moveResult) {
case WINP1:
return "Yellow player wins";
case WINP2:
return "Red player wins";
case DRAW:
return "DRAW!";
case OPEN:
return "Error";
}
return "Error";
}
/*
* Check winning state from cell i, j, return win list moves
*/
private LinkedList<CXCell> winCells(int i, int j) {
CXCellState s = board.cellState(i, j);
LinkedList<CXCell> moves = new LinkedList<CXCell>();
int n;
// Horizontal check
n = 1;
for (int h = 1; j - h >= 0 && board.cellState(i, j - h) == s; h++) {
n++;
moves.add(new CXCell(i, j - h, s));
} // backward check
for (int h = 1; j + h < board.N && board.cellState(i, j + h) == s; h++) {
n++;
moves.add(new CXCell(i, j + h, s));
} // forward check
if (n >= board.X) {
moves.add(new CXCell(i, j, s));
return moves;
}
// Vertical check
n = 1;
moves.clear();
for (int h = 1; i + h < board.M && board.cellState(i + h, j) == s; h++) {
n++;
moves.add(new CXCell(i + h, j, s));
} // forward check
if (n >= board.X) {
moves.add(new CXCell(i, j, s));
return moves;
}
// Diagonal check
n = 1;
moves.clear();
for (int h = 1; i - h >= 0 && j - h >= 0 && board.cellState(i - h, j - h) == s; h++) {
n++;
moves.add(new CXCell(i - h, j - h, s));
} // backward check
for (int h = 1; i + h < board.M && j + h < board.N && board.cellState(i + h, j + h) == s; h++) {
n++;
moves.add(new CXCell(i + h, j + h, s));
} // forward check
if (n >= board.X) {
moves.add(new CXCell(i, j, s));
return moves;
}
// Anti-diagonal check
n = 1;
moves.clear();
for (int h = 1; i - h >= 0 && j + h < board.N && board.cellState(i - h, j + h) == s; h++) {
n++;
moves.add(new CXCell(i - h, j + h, s));
} // backward check
for (int h = 1; i + h < board.M && j - h >= 0 && board.cellState(i + h, j - h) == s; h++) {
n++;
moves.add(new CXCell(i + h, j - h, s));
} // forward check
if (n >= board.X) {
moves.add(new CXCell(i, j, s));
return moves;
}
return moves;
}
/* draw marked cells */
private void drawCounters(Graphics2D g2, CXCell[] list) {
for (CXCell c : list) {
int x = c.j * cellGap + 5;
int y = c.i * cellGap + (Board_Top_Border + 5 + extraBorder);
CXCellState s = c.state;
if (s == CXCellState.P1)
g2.setPaint(getYellowGradientPaint());
else if (s == CXCellState.P2)
g2.setPaint(getRedGradientPaint());
if (!animatingCoinDrop) {
g2.fillOval(x, y, cellGap - 10, cellGap - 10);
} else {
if (!(c.equals(list[list.length - 1]))) {
g2.fillOval(x, y, cellGap - 10, cellGap - 10);
}
}
}
}
Shape cell(int x, int y, int s) {
GeneralPath gp = new GeneralPath();
gp.append(new Ellipse2D.Float(x, y, s, s), false);
gp.moveTo(x, y);
gp.lineTo(x + s, y);
gp.lineTo(x + s, y + s);
gp.lineTo(x, y + s);
gp.closePath();
return gp;
}
private GradientPaint getYellowGradientPaint() {
if (gpYellow == null)
gpYellow = new GradientPaint(75, 75, new Color(255, 255, 0), 100, 100, new Color(200, 200, 0), true);
return gpYellow;
}
private GradientPaint getRedGradientPaint() {
if (gpRed == null)
gpRed = new GradientPaint(75, 75, new Color(220, 0, 0), 100, 100, new Color(150, 0, 0), true);
return gpRed;
}
/*
* shows the coin dropping to its resting point. As coin drops user input is
* blocked.
*/
private void animateCoinDrop(Graphics2D g2) {
if (gameState == CXGameState.OPEN) {
/*
* try { Thread.sleep(25); } catch (Exception e) { }
*/
animatingCoinDrop = (animationStartPosition - (cellGap / 2) + animationFrame < WIN_ANIMATION_FRAMES
- cellGap * 2 + (-1 * (animationRow - NUMBER_OF_ROWS - 4)) * cellGap);
if (animatingCoinDrop) {
animationFrameAcceleration = 2.5 * animationFrameAcceleration;
if (NUMBER_OF_ROWS >= 20 || NUMBER_OF_COLS >= 20) {
animationFrame = animationFrame + NUMBER_OF_ROWS;
}
animationFrame = animationFrame + (int) animationFrameAcceleration + 1;
if (board.currentPlayer == PLAYER1)
g2.setPaint(getRedGradientPaint()); // colors here appear 'inverted' - this is because the move
else
g2.setPaint(getYellowGradientPaint()); // has actually been made, so animation is for last move
// now draw the coin:
g2.fillOval((cellGap) * animationColumn, (animationStartPosition + animationFrame - cellGap),
cellGap - 10, cellGap - 10);
}
} else {
animatingCoinDrop = false;
}
}
/* draw mouse pointer on the white top border of board */
public void drawMousePointer(Graphics2D g2) {
// reset
// g2.setStroke(defaultStroke);
if (!animatingCoinDrop && Player[board.currentPlayer()] == CXPlayerType.HUMAN) {
if (board.currentPlayer == PLAYER1)
g2.setPaint(getYellowGradientPaint());
if (board.currentPlayer == PLAYER2)
g2.setPaint(getRedGradientPaint());
if (!(isSpaceInColumn(mousePointerHorizontalPosition))) {
g2.setPaint(Color.LIGHT_GRAY);
fullColum = true;
} else {
fullColum = false;
}
g2.fillOval(mousePointerHorizontalPosition * cellGap + 5, mousePointerVerticalPosition - cellGap / 2,
cellGap - 10, cellGap - 10);
if (fullColum) {
g2.setPaint(Color.WHITE);
g2.setFont(new Font("Arial", Font.BOLD, 14));
if (cellGap == 60) {
g2.drawString("FULL!", mousePointerHorizontalPosition * cellGap + 5 + (cellGap / 10),
mousePointerVerticalPosition);
} else if (cellGap == 45) {
g2.drawString("FULL!", mousePointerHorizontalPosition * cellGap + 5, mousePointerVerticalPosition);
} else {
g2.drawString("FULL!", mousePointerHorizontalPosition * cellGap + 5 + (cellGap / 4),
mousePointerVerticalPosition);
}
}
}
}
/*
* checks the top cell of each columns to see if it is vacant
*/
public boolean isSpaceInColumn(int column) {
if (column > (NUMBER_OF_COLS - 1))
return false;
else
return !board.fullColumn(column);
}
/*
* if the mouse is in the upper part of the screen a piece will be shown
*/
public void mouseMoved(MouseEvent mm) {
if (Player[board.currentPlayer()] == CXPlayerType.HUMAN) {
drawPointerOnMouse = (mm.getY() < (Board_Top_Border + extraBorder + cellGap / 1.5));
mousePointerHorizontalPosition = ((mm.getX() / cellGap)); // drawn in center of column - not centered over
// the mouse
mousePointerVerticalPosition = mm.getY(); // must be fixed place per column (no overlapping allowed).
} else {
drawPointerOnMouse = false;
}
}
/*
* need to find out which column has been clicked on then post it as a move to
* Game.
*/
public void mouseClicked(MouseEvent m1) {
if (gameState == CXGameState.OPEN) {
if (Player[board.currentPlayer()] == CXPlayerType.HUMAN) { // Human player
if (!animatingCoinDrop && !fullColum) {
// only want input when no coins are currently dropping.
int colClicked = 0;
// large area at top for clicking to make move
if (m1.getY() < Board_Top_Border + extraBorder + cellGap / 2) {
colClicked = (m1.getX() / cellGap);
if (isSpaceInColumn(colClicked))
gameState = board.markColumn(colClicked);
CXCell c = board.getLastMove();
setAnimationState(true, colClicked, m1.getY(), c.i);
}
}
} else { // Software player
int curr = board.currentPlayer();
final ExecutorService executor = Executors.newSingleThreadExecutor();
final Future<Integer> task = executor.submit(new StoppablePlayer(ComPlayer[curr], board.copy()));
executor.shutdown(); // Makes the ExecutorService stop accepting new tasks
Integer c = null;
try {
// TIMEOUT secs + 10% more time
c = task.get((int) (TIMEOUT + 0.1 * TIMEOUT), TimeUnit.SECONDS);
} catch (TimeoutException ex) {
executor.shutdownNow();
System.err.println(ComPlayer[curr].playerName() + " interrupted due to timeout");
System.exit(1);
} catch (Exception ex) {
System.err.println("Error: " + ComPlayer[curr].playerName() + " interrupted due to exception");
System.err.println(" " + ex);
System.exit(1);
}
if (!executor.isTerminated())
executor.shutdownNow();
if (isSpaceInColumn(c)) {
gameState = board.markColumn(c);
CXCell m = board.getLastMove();
setAnimationState(true, c, Board_Top_Border + extraBorder, m.i);
} else {
System.err.println(ComPlayer[curr].playerName() + " selected an illegal move!");
System.exit(1);
}
}
}
}
private class StoppablePlayer implements Callable<Integer> {
private final CXPlayer P;
private final CXBoard B;
public StoppablePlayer(CXPlayer P, CXBoard B) {
this.P = P;
this.B = B;
}
public Integer call() throws InterruptedException {
return P.selectColumn(B);
}
}
public void mouseDragged(MouseEvent md) {
}
public void mouseEntered(MouseEvent m2) {
}
public void mouseExited(MouseEvent m3) {
}
public void mousePressed(MouseEvent m4) {
}
public void mouseReleased(MouseEvent m5) {
}
}