-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDisplay.java
More file actions
executable file
·329 lines (277 loc) · 10.4 KB
/
Copy pathDisplay.java
File metadata and controls
executable file
·329 lines (277 loc) · 10.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
import java.awt.*;
import java.awt.event.*;
import java.util.concurrent.*;
/// Window class responsible for human IO and graphics display
/**
* A window class that displays the game and is responsible for performing human
* IO if necessary. You should not need to worry about the contents of this
* class, as none of the methods contained here will be necessary for developing
* an AI player.
*
* @author Leonid Shamis
*/
public final class Display extends javax.swing.JPanel implements IOModule, MouseListener, MouseMotionListener
{
/// Subclass to synchronize human player actions.
/**
* This class is used to implement the human moves. It relies on a bit of
* threading hacking and should not be modified as minor changes can break
* the human input functions.
*/
private static final class HumanBlocker
{
/// Semaphore used for thread blocking.
private final Semaphore readySignal = new Semaphore(0);
/// Move chosen by human player.
private volatile int result;
/// Flag indicating whether or not the human player has chosen a move.
private volatile boolean moveRetrieved = false;
/// Sets what the player's move was, then unblocks the thread so it it will complete.
/**
* Sets what the player's move was, then unblocks the thread so it it will complete.
* @param move The move to record.
*/
public void setMove(int move)
{
result = move;
readySignal.release();
}
/// Request a move selection from active human player.
/**
* Blocks until the human has chosen a move, then returns that move. Calling this
* function more than once per HumanBlocker will cause an IllegalStateException.
*
* @return The move chosen by the human.
*/
public int getMove()
{
// Set that we've retrieved the move.
synchronized(this)
{
if(moveRetrieved)
{
throw new IllegalStateException("Already retrieved stored move!");
}
moveRetrieved = true;
}
// Wait for the move to be ready
try
{
readySignal.acquire();
}
catch(InterruptedException iex)
{
throw new RuntimeException("Interrupted a thread... this shouldn't be possible");
}
return result;
}
}
/// Colors used in the display.
private static final Color [] coinColors = new Color[]{Color.WHITE, Color.RED, Color.YELLOW};
/// Human interaction blocker, to be set on human action request.
private volatile HumanBlocker currentBlocker = null;
/// Active game being displayed.
private GameStateModule game;
/// Column over which the mouse is located.
private int MouseX;
/// Default constructor.
public Display()
{
setBackground(Color.GRAY);
addMouseListener(this);
addMouseMotionListener(this);
setPreferredSize(new Dimension(500, 450));
}
/// Set up request for human player chosen move.
public int getHumanMove()
{
final HumanBlocker ourBlocker = new HumanBlocker();
currentBlocker = ourBlocker;
this.repaint();
return ourBlocker.getMove();
}
/// Set up pretty anti-aliased graphics.
private Graphics2D setup2DGraphics(final Graphics g)
{
final Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
return g2d;
}
/// Draw the active game board.
private void drawBoard(final Graphics2D g2d)
{
// Get panel dimentions
final int width = this.getWidth();
final int height = this.getHeight();
// Dimentions of the circles for a game.getWidth()xgame.getWidth() grid
final double circleWidth = ((double) width) / game.getWidth();
final double circleHeight = ((double) height) / (1 + game.getHeight());
// Reduce the dimentions to prevent overlaps
final int cW = (int) (circleWidth) - 2;
final int cH = (int) (circleHeight) - 2;
// for each column
for(int x = 0; x < game.getWidth(); x++)
{
//for each row
for(int y = 1; y <= game.getHeight(); y++)
{
// Location of the current circle
final int cX = (int) (x * circleWidth) + 1;
final int cY = (int) (y * circleHeight) + 1;
// Choose the appropriate color
g2d.setColor(coinColors[game.getAt(x, game.getHeight() - y)]);
// Draw circle
g2d.fillOval(cX, cY, cW, cH);
}
}
}
/// Outlines all pieces to make it look nicer.
private void drawOutlines(final Graphics2D g2d)
{
// Setup the brush
g2d.setStroke(new BasicStroke(2));
g2d.setColor(Color.BLACK);
// Get panel dimentions
final int width = this.getWidth();
final int height = this.getHeight();
// Dimentions of the circles for a game.getWidth()xgame.getWidth() grid
final double circleWidth = ((double) width) / game.getWidth();
final double circleHeight = ((double) height) / (1 + game.getHeight());
// Reduce the dimentions to prevent overlaps
final int cW = (int) (circleWidth) - 2;
final int cH = (int) (circleHeight) - 2;
// for each column
for(int x = 0; x < game.getWidth(); x++)
{
// for each row
for(int y = 1; y <= game.getHeight(); y++)
{
// Location of the current circle
final int cX = (int) (x * circleWidth) + 1;
final int cY = (int) (y * circleHeight) + 1;
// Outline circle
g2d.drawOval(cX, cY, cW, cH);
}
}
// Draw a line between the move selector and the board
g2d.setStroke(new BasicStroke(3));
g2d.drawLine(-1, cH - 2, width + 1, cH - 2);
}
/// Draw the human player move selector.
private void drawMove(final Graphics2D g2d)
{
// Get dimentions
final int width = this.getWidth();
final int height = this.getHeight();
final double circleWidth = ((double) width) / game.getWidth();
final double circleHeight = ((double) height) / (1 + game.getHeight());
final int cX = (int) (MouseX * circleWidth) + game.getHeight();
final int cY = 1;
final int cW = (int) (circleWidth) - 12;
final int cH = (int) (circleHeight) - 12;
// Get color
g2d.setColor(coinColors[game.getActivePlayer()]);
// Draw and outline move
g2d.fillOval(cX, cY, cW, cH);
g2d.setStroke(new BasicStroke(2));
g2d.setColor(Color.BLACK);
g2d.drawOval(cX, cY, cW, cH);
}
/// If a player has won draw a line connecting the four checkers.
private void drawVictory(final Graphics2D g2d, final Point start, final Point end)
{
// Get panel dimentions
final int width = this.getWidth();
final int height = this.getHeight();
// Dimentions of the circles for a game.getWidth()xgame.getWidth() grid
final double circleWidth = ((double) width) / game.getWidth();
final double circleHeight = ((double) height) / (1 + game.getHeight());
// Get the first victory tile
final int startX = (int) (start.x * circleWidth) + 1 + (int) (circleWidth / 2);
final int startY = (int) ((game.getHeight() - start.y) * circleHeight) + 1 + (int) (circleHeight / 2);
// Get the last victory tile
final int endX = (int) (end.x * circleWidth) + 1 + (int) (circleWidth / 2);
final int endY = (int) ((game.getHeight() - end.y) * circleHeight) + 1 + (int) (circleHeight / 2);
// Draw a line through the victory tiles
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(10, BasicStroke.CAP_ROUND, 0));
g2d.drawLine(startX, startY, endX, endY);
g2d.setColor(Color.WHITE);
g2d.setStroke(new BasicStroke(5, BasicStroke.CAP_ROUND, 0));
g2d.drawLine(startX, startY, endX, endY);
}
/// Instructions for repainting the panel.
@Override
public void paintComponent(final Graphics g)
{
super.paintComponent(g);
if(game == null)
return;
final Graphics2D g2d = setup2DGraphics(g);
drawBoard(g2d);
drawOutlines(g2d);
// Draw move selector if there is a request for it
if(currentBlocker != null)
{
drawMove(g2d);
}
if(game.isGameOver() && game.getWinner() != 0)
{
drawVictory(g2d, game.getStartPt(), game.getEndPt());
}
}
/// Calls for repaint
public void drawBoard(final GameStateModule game)
{
this.game = game;
repaint();
}
/// Empty.
public void mousePressed(MouseEvent e)
{
}
/// Gets selected move from the active human player if it was requested.
public void mouseReleased(MouseEvent e)
{
// Update mouse position
MouseX = game.getWidth() * e.getX() / this.getWidth();
// If this isn't a legal move, abort.
if(!game.canMakeMove(MouseX)) return;
// Ignore if there is no request
if(currentBlocker == null)
{
return;
}
// Releases the blocker. DO NOT REORDER THESE LINES!
final HumanBlocker current = currentBlocker;
currentBlocker = null;
// Report Selection
current.setMove(MouseX);
// Update graphics
this.repaint();
}
/// Empty.
public void mouseEntered(MouseEvent e)
{
}
/// Empty.
public void mouseExited(MouseEvent e)
{
}
/// Empty.
public void mouseClicked(MouseEvent e)
{
}
/// Updates mouse position if the mouse has moved.
public void mouseMoved(MouseEvent e)
{
MouseX = game.getWidth() * e.getX() / this.getWidth();
this.repaint();
}
/// Updates mouse position if the mouse has moved.
public void mouseDragged(MouseEvent e)
{
MouseX = game.getWidth() * e.getX() / this.getWidth();
this.repaint();
}
}