-
Notifications
You must be signed in to change notification settings - Fork 5
/
Checkers.java
730 lines (622 loc) · 23.7 KB
/
Checkers.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
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
730
package project;
/*
This applet lets two uses play checkers against each other.
Red always starts the game. If a player can jump an opponent's
piece, then the player must jump. When a plyer can make no more
moves, the game ends.
This file defines four classes: the main applet class, Checkers;
CheckersCanvas, CheckersMove, and CheckersData.
(This is not very good style; the other classes really should be
nested classes inside the Checkers class.)
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.Vector;
public class Checkers extends Applet {
/*
* The main applet class only lays out the applet. The work of the game is
* all done in the CheckersCanvas object. Note that the Buttons and Label
* used in the applet are defined as instance variables in the
* CheckersCanvas class. The applet class gives them their visual appearance
* and sets their size and positions.
*/
public void init() {
setLayout(null); // I will do the layout myself.
setBackground(new Color(0, 150, 0)); // Dark green background.
/* Create the components and add them to the applet. */
CheckersCanvas board = new CheckersCanvas();
// Note: The constructor creates the buttons board.resignButton
// and board.newGameButton and the Label board.message.
add(board);
board.newGameButton.setBackground(Color.lightGray);
add(board.newGameButton);
board.resignButton.setBackground(Color.lightGray);
add(board.resignButton);
board.message.setForeground(Color.green);
board.message.setFont(new Font("Serif", Font.BOLD, 14));
add(board.message);
/*
* Set the position and size of each component by calling its
* setBounds() method.
*/
board.setBounds(20, 20, 164, 164); // Note: size MUST be 164-by-164 !
board.newGameButton.setBounds(210, 60, 100, 30);
board.resignButton.setBounds(210, 120, 100, 30);
board.message.setBounds(0, 200, 330, 30);
}
} // end class Checkers
class CheckersCanvas extends Canvas implements ActionListener, MouseListener {
// 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.
CheckersData 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 CheckersData.RED and CheckersData.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.
CheckersMove[] legalMoves; // An array containing the legal moves for the
// current player.
public CheckersCanvas() {
// 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);
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 CheckersData();
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 = CheckersData.RED; // RED moves first.
legalMoves = board.getLegalMoves(CheckersData.RED); // Get RED's legal
// moves.
selectedRow = -1; // RED 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 == CheckersData.RED)
gameOver("RED resigns. BLACK wins.");
else
gameOver("BLACK resigns. RED winds.");
}
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 == CheckersData.RED)
message.setText("RED: 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(CheckersMove move) {
// Thiis 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.
*/
if (move.isJump()) {
legalMoves = board.getLegalJumpsFrom(currentPlayer, move.toRow,
move.toCol);
if (legalMoves != null) {
if (currentPlayer == CheckersData.RED)
message.setText("RED: You must continue jumping.");
else
message.setText("BLACK: You must continue jumping.");
selectedRow = move.toRow; // Since only one piece can be moved,
// select it.
selectedCol = move.toCol;
repaint();
return;
}
}
/*
* 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 == CheckersData.RED) {
currentPlayer = CheckersData.BLACK;
legalMoves = board.getLegalMoves(currentPlayer);
if (legalMoves == null)
gameOver("BLACK has no moves. RED wins.");
else if (legalMoves[0].isJump())
message.setText("BLACK: Make your move. You must jump.");
else
message.setText("BLACK: Make your move.");
} else {
currentPlayer = CheckersData.RED;
legalMoves = board.getLegalMoves(currentPlayer);
if (legalMoves == null)
gameOver("RED has no moves. BLACK wins.");
else if (legalMoves[0].isJump())
message.setText("RED: Make your move. You must jump.");
else
message.setText("RED: 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 - 1, getSize().height - 1);
g.drawRect(1, 1, getSize().width - 3, getSize().height - 3);
/* 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(2 + col * 20, 2 + row * 20, 20, 20);
switch (board.pieceAt(row, col)) {
case CheckersData.RED:
g.setColor(Color.red);
g.fillOval(4 + col * 20, 4 + row * 20, 16, 16);
break;
case CheckersData.BLACK:
g.setColor(Color.black);
g.fillOval(4 + col * 20, 4 + row * 20, 16, 16);
break;
case CheckersData.RED_KING:
g.setColor(Color.red);
g.fillOval(4 + col * 20, 4 + row * 20, 16, 16);
g.setColor(Color.white);
g.drawString("K", 7 + col * 20, 16 + row * 20);
break;
case CheckersData.BLACK_KING:
g.setColor(Color.black);
g.fillOval(4 + col * 20, 4 + row * 20, 16, 16);
g.setColor(Color.white);
g.drawString("K", 7 + col * 20, 16 + row * 20);
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(2 + legalMoves[i].fromCol * 20,
2 + legalMoves[i].fromRow * 20, 19, 19);
}
// 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(2 + selectedCol * 20, 2 + selectedRow * 20, 19, 19);
g.drawRect(3 + selectedCol * 20, 3 + selectedRow * 20, 17, 17);
g.setColor(Color.green);
for (int i = 0; i < legalMoves.length; i++) {
if (legalMoves[i].fromCol == selectedCol
&& legalMoves[i].fromRow == selectedRow)
g.drawRect(2 + legalMoves[i].toCol * 20,
2 + legalMoves[i].toRow * 20, 19, 19);
}
}
}
} // end paint()
public Dimension getPreferredSize() {
// Specify desired size for this component. Note:
// the size MUST be 164 by 164.
return new Dimension(164, 164);
}
public Dimension getMinimumSize() {
return new Dimension(164, 164);
}
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) / 20;
int row = (evt.getY() - 2) / 20;
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) {
}
} // end class SimpleCheckerboardCanvas
class CheckersMove {
// A CheckersMove object represents a move in the game of Checkers.
// It holds the row and column of the piece that is to be moved
// and the row and column of the square to which it is to be moved.
// (This class makes no guarantee that the move is legal.)
int fromRow, fromCol; // Position of piece to be moved.
int toRow, toCol; // Square it is to move to.
CheckersMove(int r1, int c1, int r2, int c2) {
// Constructor. Just set the values of the instance variables.
fromRow = r1;
fromCol = c1;
toRow = r2;
toCol = c2;
}
boolean isJump() {
// Test whether this move is a jump. It is assumed that
// the move is legal. In a jump, the piece moves two
// rows. (In a regular move, it only moves one row.)
return (fromRow - toRow == 2 || fromRow - toRow == -2);
}
} // end class CheckersMove.
class CheckersData {
// An object of this class holds data about a game of checkers.
// It knows what kind of piece is on each sqaure of the checkerboard.
// Note that RED moves "up" the board (i.e. row number decreases)
// while BLACK moves "down" the board (i.e. row number increases).
// Methods are provided to return lists of available legal moves.
/*
* The following constants represent the possible contents of a square on
* the board. The constants RED and BLACK also represent players in the
* game.
*/
public static final int EMPTY = 0, RED = 1, RED_KING = 2, BLACK = 3,
BLACK_KING = 4;
private int[][] board; // board[r][c] is the contents of row r, column c.
public CheckersData() {
// Constructor. Create the board and set it up for a new game.
board = new int[8][8];
setUpGame();
}
public void setUpGame() {
// Set up the board with checkers in position for the beginning
// of a game. Note that checkers can only be found in squares
// that satisfy row % 2 == col % 2. At the start of the game,
// all such squares in the first three rows contain black squares
// and all such squares in the last three rows contain red squares.
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (row % 2 == col % 2) {
if (row < 3)
board[row][col] = BLACK;
else if (row > 4)
board[row][col] = RED;
else
board[row][col] = EMPTY;
} else {
board[row][col] = EMPTY;
}
}
}
} // end setUpGame()
public int pieceAt(int row, int col) {
// Return the contents of the square in the specified row and column.
return board[row][col];
}
public void setPieceAt(int row, int col, int piece) {
// Set the contents of the square in the specified row and column.
// piece must be one of the constants EMPTY, RED, BLACK, RED_KING,
// BLACK_KING.
board[row][col] = piece;
}
public void makeMove(CheckersMove move) {
// Make the specified move. It is assumed that move
// is non-null and that the move it represents is legal.
makeMove(move.fromRow, move.fromCol, move.toRow, move.toCol);
}
public void makeMove(int fromRow, int fromCol, int toRow, int toCol) {
// Make the move from (fromRow,fromCol) to (toRow,toCol). It is
// assumed that this move is legal. If the move is a jump, the
// jumped piece is removed from the board. If a piece moves
// the last row on the opponent's side of the board, the
// piece becomes a king.
board[toRow][toCol] = board[fromRow][fromCol];
board[fromRow][fromCol] = EMPTY;
if (fromRow - toRow == 2 || fromRow - toRow == -2) {
// The move is a jump. Remove the jumped piece from the board.
int jumpRow = (fromRow + toRow) / 2; // Row of the jumped piece.
int jumpCol = (fromCol + toCol) / 2; // Column of the jumped piece.
board[jumpRow][jumpCol] = EMPTY;
}
if (toRow == 0 && board[toRow][toCol] == RED)
board[toRow][toCol] = RED_KING;
if (toRow == 7 && board[toRow][toCol] == BLACK)
board[toRow][toCol] = BLACK_KING;
}
public CheckersMove[] getLegalMoves(int player) {
// Return an array containing all the legal CheckersMoves
// for the specfied player on the current board. If the player
// has no legal moves, null is returned. The value of player
// should be one of the constants RED or BLACK; if not, null
// is returned. If the returned value is non-null, it consists
// entirely of jump moves or entirely of regular moves, since
// if the player can jump, only jumps are legal moves.
if (player != RED && player != BLACK)
return null;
int playerKing; // The constant representing a King belonging to player.
if (player == RED)
playerKing = RED_KING;
else
playerKing = BLACK_KING;
Vector moves = new Vector(); // Moves will be stored in this vector.
/*
* First, check for any possible jumps. Look at each square on the
* board. If that square contains one of the player's pieces, look at a
* possible jump in each of the four directions from that square. If
* there is a legal jump in that direction, put it in the moves vector.
*/
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (board[row][col] == player || board[row][col] == playerKing) {
if (canJump(player, row, col, row + 1, col + 1, row + 2,
col + 2))
moves.addElement(new CheckersMove(row, col, row + 2,
col + 2));
if (canJump(player, row, col, row - 1, col + 1, row - 2,
col + 2))
moves.addElement(new CheckersMove(row, col, row - 2,
col + 2));
if (canJump(player, row, col, row + 1, col - 1, row + 2,
col - 2))
moves.addElement(new CheckersMove(row, col, row + 2,
col - 2));
if (canJump(player, row, col, row - 1, col - 1, row - 2,
col - 2))
moves.addElement(new CheckersMove(row, col, row - 2,
col - 2));
}
}
}
/*
* If any jump moves were found, then the user must jump, so we don't
* add any regular moves. However, if no jumps were found, check for any
* legal regualar moves. Look at each square on the board. If that
* square contains one of the player's pieces, look at a possible move
* in each of the four directions from that square. If there is a legal
* move in that direction, put it in the moves vector.
*/
if (moves.size() == 0) {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (board[row][col] == player
|| board[row][col] == playerKing) {
if (canMove(player, row, col, row + 1, col + 1))
moves.addElement(new CheckersMove(row, col,
row + 1, col + 1));
if (canMove(player, row, col, row - 1, col + 1))
moves.addElement(new CheckersMove(row, col,
row - 1, col + 1));
if (canMove(player, row, col, row + 1, col - 1))
moves.addElement(new CheckersMove(row, col,
row + 1, col - 1));
if (canMove(player, row, col, row - 1, col - 1))
moves.addElement(new CheckersMove(row, col,
row - 1, col - 1));
}
}
}
}
/*
* If no legal moves have been found, return null. Otherwise, create an
* array just big enough to hold all the legal moves, copy the legal
* moves from the vector into the array, and return the array.
*/
if (moves.size() == 0)
return null;
else {
CheckersMove[] moveArray = new CheckersMove[moves.size()];
for (int i = 0; i < moves.size(); i++)
moveArray[i] = (CheckersMove) moves.elementAt(i);
return moveArray;
}
} // end getLegalMoves
public CheckersMove[] getLegalJumpsFrom(int player, int row, int col) {
// Return a list of the legal jumps that the specified player can
// make starting from the specified row and column. If no such
// jumps are possible, null is returned. The logic is similar
// to the logic of the getLegalMoves() method.
if (player != RED && player != BLACK)
return null;
int playerKing; // The constant representing a King belonging to player.
if (player == RED)
playerKing = RED_KING;
else
playerKing = BLACK_KING;
Vector moves = new Vector(); // The legal jumps will be stored in this
// vector.
if (board[row][col] == player || board[row][col] == playerKing) {
if (canJump(player, row, col, row + 1, col + 1, row + 2, col + 2))
moves.addElement(new CheckersMove(row, col, row + 2, col + 2));
if (canJump(player, row, col, row - 1, col + 1, row - 2, col + 2))
moves.addElement(new CheckersMove(row, col, row - 2, col + 2));
if (canJump(player, row, col, row + 1, col - 1, row + 2, col - 2))
moves.addElement(new CheckersMove(row, col, row + 2, col - 2));
if (canJump(player, row, col, row - 1, col - 1, row - 2, col - 2))
moves.addElement(new CheckersMove(row, col, row - 2, col - 2));
}
if (moves.size() == 0)
return null;
else {
CheckersMove[] moveArray = new CheckersMove[moves.size()];
for (int i = 0; i < moves.size(); i++)
moveArray[i] = (CheckersMove) moves.elementAt(i);
return moveArray;
}
} // end getLegalMovesFrom()
private boolean canJump(int player, int r1, int c1, int r2, int c2, int r3,
int c3) {
// This is called by the two previous methods to check whether the
// player can legally jump from (r1,c1) to (r3,c3). It is assumed
// that the player has a piece at (r1,c1), that (r3,c3) is a position
// that is 2 rows and 2 columns distant from (r1,c1) and that
// (r2,c2) is the square between (r1,c1) and (r3,c3).
if (r3 < 0 || r3 >= 8 || c3 < 0 || c3 >= 8)
return false; // (r3,c3) is off the board.
if (board[r3][c3] != EMPTY)
return false; // (r3,c3) already contains a piece.
if (player == RED) {
if (board[r1][c1] == RED && r3 > r1)
return false; // Regular red piece can only move up.
if (board[r2][c2] != BLACK && board[r2][c2] != BLACK_KING)
return false; // There is no black piece to jump.
return true; // The jump is legal.
} else {
if (board[r1][c1] == BLACK && r3 < r1)
return false; // Regular black piece can only move downn.
if (board[r2][c2] != RED && board[r2][c2] != RED_KING)
return false; // There is no red piece to jump.
return true; // The jump is legal.
}
} // end canJump()
private boolean canMove(int player, int r1, int c1, int r2, int c2) {
// This is called by the getLegalMoves() method to determine whether
// the player can legally move from (r1,c1) to (r2,c2). It is
// assumed that (r1,r2) contains one of the player's pieces and
// that (r2,c2) is a neighboring square.
if (r2 < 0 || r2 >= 8 || c2 < 0 || c2 >= 8)
return false; // (r2,c2) is off the board.
if (board[r2][c2] != EMPTY)
return false; // (r2,c2) already contains a piece.
if (player == RED) {
if (board[r1][c1] == RED && r2 > r1)
return false; // Regualr red piece can only move down.
return true; // The move is legal.
} else {
if (board[r1][c1] == BLACK && r2 < r1)
return false; // Regular black piece can only move up.
return true; // The move is legal.
}
} // end canMove()
} // end class CheckersData