1 void doClickSquare(int row, int col) {
2 // This is called by mousePressed() when a player clicks
3 // on the square in the specified row and col. It has already
4 // been checked that a game is, in fact, in progress.
5
6 /* Check that the user clicked an empty square. If not, show an
7 error message and exit. */
8
9 if ( board[row][col] != EMPTY ) {
10 if (currentPlayer == BLACK)
11 message.setText("BLACK: Please click an empty square.");
12 else
13 message.setText("WHITE: Please click an empty square.");
14 return;
15 }
16
17 /* Make the move. Check if the board is full or if the move
18 is a winning move. If so, the game ends. If not, then it's
19 the other user's turn. */
20
21 board[row][col] = currentPlayer; // Make the move.
22 Graphics g = getGraphics();
23 drawPiece(g, currentPlayer, row, col); // Draw the new piece.
24 g.dispose();
25
26 if (winner(row,col)) { // First, check for a winner.
27 if (currentPlayer == WHITE)
28 gameOver("WHITE wins the game!");
29 else
30 gameOver("BLACK wins the game!");
31 return;
32 }
33
34 boolean emptySpace = false; // Check if the board is full.
35 for (int i = 0; i < 13; i++)
36 for (int j = 0; j < 13; j++)
37 if (board[i][j] == EMPTY)
38 emptySpace = true; // The board contains an empty space.
39 if (emptySpace == false) {
40 gameOver("The game ends in a draw.");
41 return;
42 }
43
44 /* Continue the game. It's the other player's turn. */
45
46 if (currentPlayer == BLACK) {
47 currentPlayer = WHITE;
48 message.setText("WHITE: Make your move.");
49 }
50 else {
51 currentPlayer = BLACK;
52 message.setText("BLACK: Make your move.");
53 }
54
55 } // end doClickSquare()
56
57
1 int ct = 1; // Number of pieces in a row belonging to the player.
2
3 int r, c; // A row and column to be examined
4
5 r = row + dirX; // Look at square in specified direction.
6 c = col + dirY;
7 while ( r >= 0 && r < 13 && c >= 0 && c < 13
8 && board[r][c] == player ) {
9 // Square is on the board, and it
10 // contains one of the players's pieces.
11 ct++;
12 r += dirX; // Go on to next square in this direction.
13 c += dirY;
14 }
15
16 r = row - dirX; // Now, look in the opposite direction.
17 c = col - dirY;
18 while ( r >= 0 && r < 13 && c >= 0 && c < 13
19 && board[r][c] == player ) {
20 ct++;
21 r -= dirX; // Go on to next square in this direction.
22 c -= dirY;
23 }
24
25
1 public void paint(Graphics g) {
2
3 /* Draw grid lines in darkGray. */
4
5 g.setColor(Color.darkGray);
6 for (int i = 1; i < 13; i++) {
7 g.drawLine(1 + 13*i, 0, 1 + 13*i, getSize().height);
8 g.drawLine(0, 1 + 13*i, getSize().width, 1 + 13*i);
9 }
10
11 /* Draw a two-pixel black border around the edges of the board. */
12
13 g.setColor(Color.black);
14 g.drawRect(0,0,getSize().width-1,getSize().height-1);
15 g.drawRect(1,1,getSize().width-3,getSize().height-3);
16
17 /* Draw the pieces that are on the board. */
18
19 for (int row = 0; row < 13; row++)
20 for (int col = 0; col < 13; col++)
21 if (board[row][col] != EMPTY)
22 drawPiece(g, board[row][col], row, col);
23
24 } // end paint()
25
1 private boolean winner(int row, int col) {
2 // This is called just after a piece has been played on the
3 // square in the specified row and column. It determines
4 // whether that was a winning move by counting the number
5 // of squares in a line in each of the four possible
6 // directions from (row,col). If there are 5 squares (or more)
7 // in a row in any direction, then the game is won.
8
9 if (count( board[row][col], row, col, 1, 0 ) >= 5)
10 return true;
11 if (count( board[row][col], row, col, 0, 1 ) >= 5)
12 return true;
13 if (count( board[row][col], row, col, 1, -1 ) >= 5)
14 return true;
15 if (count( board[row][col], row, col, 1, 1 ) >= 5)
16 return true;
17
18 /* When we get to this point, we know that the game is not won. */
19
20 return false;
21
22 } // end winner()
23
1 private void drawPiece(Graphics g, int piece, int row, int col) {
2 // Draw a piece in the square at (row,col). The color
3 // is specified by the "piece" parameter, which should be
4 // either BLACK or WHITE.
5 if (piece == WHITE)
6 g.setColor(Color.white);
7 else
8 g.setColor(Color.black);
9 g.fillOval(3 + 13*col, 3 + 13*row, 10, 10);
10 }
11
1 dirX dirY Why?
2 ---- ---- --------------------------------
3 horizontal direction 1 0 Only x changes.
4 vertical direction 0 1 Only y changes.
5 first diagonal 1 1 Both x and y change.
6 second diagonal 1 -1 Change in opposing directions.
7