-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPortalsCanvas
419 lines (340 loc) · 13.9 KB
/
PortalsCanvas
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
package Updated;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
class PortalsCanvas extends Canvas implements ActionListener, MouseListener, KeyListener {
// This canvas displays a 160-by-160 checkerboard pattern with
// a 2-pixel black border. It is assumed that the size of the
// canvas is set to exactly 164-by-164 pixels. This class does
// the work of letting the users play checkers, and it displays
// the checkerboard.
Button resignButton; // Current player can resign by clicking this button.
Button newGameButton; // This button starts a new game. It is enabled only
// when the current game has ended.
Label message; // A label for displaying messages to the user.
PortalsData board; // The data for the checkers board is kept here.
// This board is also responsible for generating
// lists of legal moves.
boolean gameInProgress; // Is a game currently in progress?
/* The next three variables are valid only when the game is in progress. */
int currentPlayer; // Whose turn is it now? The possible values
// are PortalsData.WHITE and PortalsData.BLACK.
int selectedRow, selectedCol; // If the current player has selected a piece to
// move, these give the row and column
// containing that piece. If no piece is
// yet selected, then selectedRow is -1.
PortalsMoves[] legalMoves; // An array containing the legal moves for the
// current player.
public PortalsCanvas()
{
// Constructor. Create the buttons and lable. Listen for mouse
// clicks and for clicks on the buttons. Create the board and
// start the first game.
setBackground(Color.black);
addMouseListener(this);
//addKeyListener(this);
setFont(new Font("Serif", Font.BOLD, 14));
resignButton = new Button("Resign");
resignButton.addActionListener(this);
newGameButton = new Button("New Game");
newGameButton.addActionListener(this);
message = new Label("",Label.CENTER);
board = new PortalsData();
doNewGame();
}
public void actionPerformed(ActionEvent evt)
{
// Respond to user's click on one of the two buttons.
Object src = evt.getSource();
if (src == newGameButton)
doNewGame();
else if (src == resignButton)
doResign();
}
void doNewGame()
{
// Begin a new game.
if (gameInProgress == true)
{
// This should not be possible, but it doens't
// hurt to check.
message.setText("Finish the current game first!");
return;
}
board.setUpGame(); // Set up the pieces.
currentPlayer = PortalsData.WHITE; // WHITE moves first.
legalMoves = board.getLegalMoves(PortalsData.WHITE); // Get WHITE's legal moves.
selectedRow = -1; // WHITE has not yet selected a piece to move.
message.setText("Red: Make your move.");
gameInProgress = true;
newGameButton.setEnabled(false);
resignButton.setEnabled(true);
repaint();
}
void doResign()
{
// Current player resigns. Game ends. Opponent wins.
if (gameInProgress == false)
{
message.setText("There is no game in progress!");
return;
}
if (currentPlayer == PortalsData.WHITE)
gameOver("WHITE resigns. BLACK wins.");
else
gameOver("BLACK resigns. WHITE wins.");
}
void gameOver(String str)
{
// The game ends. The parameter, str, is displayed as a message
// to the user. The states of the buttons are adjusted so playes
// can start a new game.
message.setText(str);
newGameButton.setEnabled(true);
resignButton.setEnabled(false);
gameInProgress = false;
}
void doClickSquare(int row, int col)
{
// This is called by mousePressed() when a player clicks on the
// square in the specified row and col. It has already been checked
// that a game is, in fact, in progress.
/* If the player clicked on one of the pieces that the player
can move, mark this row and col as selected and return. (This
might change a previous selection.) Reset the message, in
case it was previously displaying an error message. */
for (int i = 0; i < legalMoves.length; i++)
if (legalMoves[i].fromRow == row && legalMoves[i].fromCol == col)
{
selectedRow = row;
selectedCol = col;
if (currentPlayer == PortalsData.WHITE)
message.setText("WHITE: Make your move.");
else
message.setText("BLACK: Make your move.");
repaint();
return;
}
/* If no piece has been selected to be moved, the user must first
select a piece. Show an error message and return. */
if (selectedRow < 0)
{
message.setText("Click the piece you want to move.");
return;
}
/* If the user clicked on a squre where the selected piece can be
legally moved, then make the move and return. */
for (int i = 0; i < legalMoves.length; i++)
if (legalMoves[i].fromRow == selectedRow && legalMoves[i].fromCol == selectedCol
&& legalMoves[i].toRow == row && legalMoves[i].toCol == col)
{
doMakeMove(legalMoves[i]);
return;
}
/* If we get to this point, there is a piece selected, and the square where
the user just clicked is not one where that piece can be legally moved.
Show an error message. */
message.setText("Click the square you want to move to.");
} // end doClickSquare()
void doMakeMove(PortalsMoves move)
{
// This is called when the current player has chosen the specified
// move. Make the move, and then either end or continue the game
// appropriately.
board.makeMove(move);
/* If the move was a jump, it's possible that the player has another
jump. Check for legal jumps starting from the square that the player
just moved to. If there are any, the player must jump. The same
player continues moving.
*/
/* The current player's turn is ended, so change to the other player.
Get that player's legal moves. If the player has no legal moves,
then the game ends. */
if (currentPlayer == PortalsData.WHITE)
{
currentPlayer = PortalsData.BLACK;
legalMoves = board.getLegalMoves(currentPlayer);
if (legalMoves == null)
gameOver("BLACK has no moves. WHITE wins.");
else if (legalMoves[0].isJump())
message.setText("BLACK: Make your move. You must jump.");
else
message.setText("BLACK: Make your move.");
}
else
{
currentPlayer = PortalsData.WHITE;
legalMoves = board.getLegalMoves(currentPlayer);
if (legalMoves == null)
gameOver("WHITE has no moves. BLACK wins.");
else if (legalMoves[0].isJump())
message.setText("WHITE: Make your move. You must jump.");
else
message.setText("WHITE: Make your move.");
}
/* Set selectedRow = -1 to record that the player has not yet selected
a piece to move. */
selectedRow = -1;
/* As a courtesy to the user, if all legal moves use the same piece, then
select that piece automatically so the use won't have to click on it
to select it. */
if (legalMoves != null)
{
boolean sameStartSquare = true;
for (int i = 1; i < legalMoves.length; i++)
if (legalMoves[i].fromRow != legalMoves[0].fromRow
|| legalMoves[i].fromCol != legalMoves[0].fromCol)
{
sameStartSquare = false;
break;
}
if (sameStartSquare)
{
selectedRow = legalMoves[0].fromRow;
selectedCol = legalMoves[0].fromCol;
}
}
/* Make sure the board is redrawn in its new state. */
repaint();
} // end doMakeMove();
public void update(Graphics g) {
// The paint method completely redraws the canvas, so don't erase
// before calling paint().
paint(g);
}
public void paint(Graphics g) {
// Draw checkerboard pattern in gray and lightGray. Draw the
// checkers. If a game is in progress, hilite the legal moves.
/* Draw a two-pixel black border around the edges of the canvas. */
g.setColor(Color.black);
g.drawRect(0,0,getSize().width-2,getSize().height-2);
g.drawRect(1,1,getSize().width-6,getSize().height-6);
/* Draw the squares of the checkerboard and the checkers. */
for (int row = 0; row < 8; row++)
for (int col = 0; col < 8; col++) {
if ( row % 2 == col % 2)
g.setColor(Color.lightGray);
else
g.setColor(Color.gray);
g.fillRect(4 + col*40, 4 + row*40, 40, 40);
if(row == 7)
{
g.setColor(Color.blue);
g.fillRect(4 + 40, 4 + 200, 40, 40);
g.fillRect(4 + 240, 4 + 80, 40, 40);
g.setColor(Color.cyan);
g.fillRect(4 + 240, 4 + 200, 40, 40);
g.fillRect(4 + 40, 4 + 80, 40, 40);
g.setColor(Color.red);
g.fillRect(4 + 120, 4 + 80, 40, 40);
g.fillRect(4 + 160, 4 + 200, 40, 40);
g.setColor(Color.orange);
g.fillRect(4 + 160, 4 + 80, 40, 40);
g.fillRect(4 + 120, 4 + 200, 40, 40);
g.setColor(Color.green);
g.fillRect(4 + 80, 4 + 160, 40, 40);
g.fillRect(4 + 200, 4 + 120, 40, 40);
g.setColor(Color.yellow);
g.fillRect(4 + 80, 4 + 120, 40, 40);
g.fillRect(4 + 200, 4 + 160, 40, 40);
}
}
for (int row = 0; row < 8; row++)
for (int col = 0; col < 8; col++)
{
switch (board.pieceAt(row,col))
{
case PortalsData.WHITE:
g.setColor(Color.white);
g.fillOval(8 + col*40, 8 + row*40, 32, 32);
break;
case PortalsData.BLACK:
g.setColor(Color.black);
g.fillOval(8 + col*40, 8 + row*40, 32, 32);
break;
case PortalsData.WHITE_SPLIT:
g.setColor(Color.white);
g.fillOval(8 + col*40, 8 + row*40, 32, 32);
g.setColor(Color.black);
g.drawString("S", 14 + col*40, 32 + row*40);
break;
case PortalsData.BLACK_SPLIT:
g.setColor(Color.black);
g.fillOval(8 + col*40, 8 + row*40, 32, 32);
g.setColor(Color.white);
g.drawString("S", 14 + col*40, 32 + row*40);
break;
}
}
/* If a game is in progress, hilite the legal moves. Note that legalMoves
is never null while a game is in progress. */
if (gameInProgress) {
// First, draw a cyan border around the pieces that can be moved.
g.setColor(Color.cyan);
for (int i = 0; i < legalMoves.length; i++) {
g.drawRect(4 + legalMoves[i].fromCol*40, 4 + legalMoves[i].fromRow*40, 38, 38);
}
// If a piece is selected for moving (i.e. if selectedRow >= 0), then
// draw a 2-pixel white border around that piece and draw green borders
// around eacj square that that piece can be moved to.
if (selectedRow >= 0) {
g.setColor(Color.white);
g.drawRect(4 + selectedCol*40, 4 + selectedRow*40, 38, 38);
g.drawRect(6 + selectedCol*40, 6 + selectedRow*40, 34, 34);
g.setColor(Color.green);
for (int i = 0; i < legalMoves.length; i++) {
if (legalMoves[i].fromCol == selectedCol && legalMoves[i].fromRow == selectedRow)
g.drawRect(4 + legalMoves[i].toCol*40, 4 + legalMoves[i].toRow*40, 38, 38);
}
}
}
} // end paint()
public Dimension getPreferredSize() {
// Specify desired size for this component. Note:
// the size MUST be 164 by 164.
return new Dimension(328, 328);
}
public Dimension getMinimumSize() {
return new Dimension(328, 328);
}
public void mousePressed(MouseEvent evt) {
// Respond to a user click on the board. If no game is
// in progress, show an error message. Otherwise, find
// the row and column that the user clicked and call
// doClickSquare() to handle it.
if (gameInProgress == false)
message.setText("Click \"New Game\" to start a new game.");
else {
int col = (evt.getX() - 2) / 40;
int row = (evt.getY() - 2) / 40;
if (col >= 0 && col < 8 && row >= 0 && row < 8)
doClickSquare(row,col);
}
}
public void mouseReleased(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
} // end class SimpleCheckerboardCanvas