[ Exercises | Chapter Index | Main Index ]

## Solution for Programming Exercise 11.6

This page contains a sample solution to one of the exercises from Introduction to Programming Using Java.

### Exercise 11.6:

The sample program Checkers.java from Subsection 7.5.3 lets two players play checkers. It would be nice if, in the middle of a game, the state of the game could be saved to a file. Later, the file could be read back into the file to restore the game and allow the players to continue. Add the ability to save and load files to the checkers program. Design a simple text-based format for the files. Here is a picture of my solution to this exercise, showing that Load and Save buttons have been added:

It's a little tricky to completely restore the state of a game. The program has a variable board of type CheckersData that stores the current contents of the board, and it has a variable currentPlayer of type int that indicates whether Red or Black is currently moving. This data must be stored in the file when a file is saved. When a file is read into the program, you should read the data into two local variables newBoard of type CheckersData and newCurrentPlayer of type int. Once you have successfully read all the data from the file, you can use the following code to set up the program state correctly. This code assumes that you have introduced two new variables saveButton and loadButton of type Button to represent the "Save Game" and "Load Game" buttons:

    board = newBoard;  // Set up game with data read from file.
    currentPlayer = newCurrentPlayer;
    legalMoves = board.getLegalMoves(currentPlayer);
    selectedRow = -1;
    gameInProgress = true;
    newGameButton.setDisable(true);
    loadButton.setDisable(true);
    saveButton.setDisable(false);
    resignButton.setDisable(false);
    if (currentPlayer == CheckersData.RED)
       message.setText("Game loaded -- it's RED's move.");
    else
       message.setText("Game loaded -- it's BLACK's move.");
    drawBoard();

Discussion

Many different formats could be used to represent the data for a checkers game in text form. In the program, the board data is an 8-by-8 two-dimensional array of integers. One way to represent the board would be to simply output 64 integers. However, the possible values in the array are given by constants CheckersData.RED, CheckersData.BLACK, CheckersData.RED_KING, CheckersData.BLACK_KING, and CheckersData.EMPTY, and it would be nice to use a representation that uses the meaning of these constants. I decided to represent the possible values with individual characters. An empty square is represented by '.'; a regular red piece by 'r'; a regular black piece by 'b'; a red king by 'R'; and a black king by 'B'. The contents of the 8-by-8 board are then represented by 8 lines with 8 characters on each line, separated by spaces. The spaces are there so that the characters can be read back individually from the file using a Scanner. I added a line at the beginning containing the name of the program "CheckersWithFiles"; this makes it easy to check that a file that the user selects for input is of the correct type. The last line of the file contains the string "Red" if it is Red's turn to move or "Black" if it is Black's turn. Here is a sample file created by my program:

    CheckersWithFiles
    b . b . . . b .
    . . . . . b . b
    . . . . b . . .
    . . . . . . . .
    . . b . . . r .
    . . . b . r . .
    r . . . . . . .
    . r . B . r . r
    Red

I wrote methods doSave() and doLoad() to save and load files. They are modeled directly on the sample save and open methods that were presented in this chapter. The doSave() method uses a PrintWriter to write the file. The data is taken from the variables board and currentPlayer, as described in the exercise. This is very straightforward:

    out.println("CheckersWithFiles"); // Identifies file as a Checkers game.
    for (int row = 0; row < 8; row++) {
       for (int col = 0; col < 8; col++) {
          int piece = board.pieceAt(row, col);  // Contents of one square of board.
          switch (piece) {
             case CheckersData.EMPTY:      out.print(". ");  break;
             case CheckersData.RED:        out.print("r ");  break;
             case CheckersData.BLACK:      out.print("b ");  break;
             case CheckersData.RED_KING:   out.print("R ");  break;
             case CheckersData.BLACK_KING: out.print("B ");  break;
          }
       }
       out.println();
    }
    out.println(currentPlayer == CheckersData.RED ? "Red" : "Black");
    out.flush();
    out.close();
    if (out.checkError())
       throw new IOException("Some error occurred while saving the file.");

Reading the data back in is not much harder. I used a Scanner named in to read the data from the file. The method in.nextLine() can be used to read the first line of text; in.next() is used to read the character that represent the pieces on the board, as strings; and in.next() is also used to read the last word in the file, which will be "Red" or "Black". Remember that in.next() will skip over any spaces and end-of-lines before returning a non-blank token string; this makes it very easy to read the data format that I chose for the file. Here is the section of my doLoad() method that reads the data. It has a little more error-checking than you might have used in your own solution:

    Scanner in;
    try {
        in = new Scanner(selectedFile);
    }
    catch (Exception e) {
        Alert errorAlert = new Alert(Alert.AlertType.ERROR,
                "Sorry, but an error occurred while\n" +
                        "trying to open the file:\n" + e);
        errorAlert.showAndWait();
        return;
    }
    if (selectedFile == null)
        return;  // user canceled
    try {
        CheckersData newBoard = new CheckersData();
        int newCurrentPlayer;
        String programNameFromFile = in.nextLine();
        if (! programNameFromFile.equals("CheckersWithFiles"))
            throw new Exception("Selected file does not contain a checkers game.");
        for (int row = 0; row < 8; row++) {
            for (int col = 0; col < 8; col++) {
                String pieceCode = in.next();
                switch (pieceCode) {
                case ".": newBoard.setPieceAt(row, col, CheckersData.EMPTY); break;
                case "r": newBoard.setPieceAt(row, col, CheckersData.RED); break;
                case "b": newBoard.setPieceAt(row, col, CheckersData.BLACK); break;
                case "R": newBoard.setPieceAt(row, col, CheckersData.RED_KING); break;
                case "B": newBoard.setPieceAt(row, col, CheckersData.BLACK_KING); break;
                default: throw new Exception("Illegal board data found in file.");
                }
                if (row % 2 != col % 2 &&
                        newBoard.pieceAt(row, col) != CheckersData.EMPTY)
                    throw new Exception("Illegal board data found in file.");
            }
            in.nextLine();  // Discard rest of line -- not really necessary.
        }
        String currentPlayerString = in.next();
        in.close(); // Done reading data from the file.
        if (currentPlayerString.equals("Red"))
            newCurrentPlayer = CheckersData.RED;
        else if (currentPlayerString.equals("Black"))
            newCurrentPlayer = CheckersData.BLACK;
        else
            throw new Exception("Illegal current player found in file.");
        board = newBoard;  // Set up game with data read from file.
        currentPlayer = newCurrentPlayer;
        legalMoves = board.getLegalMoves(currentPlayer);
        selectedRow = -1;
        gameInProgress = true;
        newGameButton.setDisable(true);
        loadButton.setDisable(true);
        saveButton.setDisable(false);
        resignButton.setDisable(false);
        if (currentPlayer == CheckersData.RED)
            message.setText("Game loaded -- it's RED's move.");
        else
            message.setText("Game loaded -- it's BLACK's move.");
        drawBoard();

Note that after successfully reading data from the file, this method uses the code given in the exercise for restoring the state of the program to match the state of the game that has been read from the file.

Aside from writing the doSave() and doLoad methods, you must also add "Save Game" and "Load Game" buttons to the program, and you have to decide when they should be enabled and disabled. In my program, the user can save a game only when a game is actually in progress. The "Save" button is enabled as soon as a move is made in a game (saveButton.setDisable(false) is actually called every time a move is made, but only the first call matters). The "Save" button is disabled in the gameOver() method, which is always called when a game ends. As for the "Load" button, it is disabled when a game is in progress, and is enabled when a game ends. You can see all of the changes that I made in the solution below.

The Solution

The checkers program is very long. All the changes from the original version are in the first 325 lines and are shown in red.

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    import javafx.scene.layout.Pane;
    import javafx.scene.canvas.Canvas;
    import javafx.scene.canvas.GraphicsContext;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.paint.Color;
    import javafx.scene.text.Font;
    import javafx.scene.text.FontWeight;
    import javafx.scene.input.MouseEvent;

    import javafx.scene.control.Alert;
    import javafx.stage.FileChooser;
    import java.io.*;
    import java.util.Scanner;

    import java.util.ArrayList;

    /**
     * This panel lets two users 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 player can make no more
     * moves, the game ends.
     *
     * The state of an ongoing game can be saves to a file at any point,
     * and the resulting file can be loaded back into the program to
     * restore the state of the game.  Load is only possible when no
     * game is in progress.  "Save Game" and "Load Game" buttons are
     * provided for saving and loading game state.
     */
    public class CheckersWithFiles extends Application {

        public static void main(String[] args) {
            launch(args);
        }

        //---------------------------------------------------------------------

        CheckersBoard board; // A canvas on which a checker board is drawn,
                             // defined by a static nested subclass.  Much of
                             // the game logic is defined in this class.

        private Button newGameButton;  // Button for starting a new game.

        private Button resignButton;   // Button that a player can use to end
                                       // the game by resigning.

        private Button saveButton;     // For saving game state to a file.
        private Button loadButton;     // For loading a saved game.

        private Label message;  // Label for displaying messages to the user.

        private Stage window;  // The program window (used as parent for file dialog).

        /**
         * The constructor creates the Board (which in turn creates and manages
         * the buttons and message label), adds all the components, and sets
         * the bounds of the components.  A null layout is used.  (This is
         * the only thing that is done in the main Checkers class.)
         */
        public void start(Stage stage) {

            window = stage;

            /* Create the label that will show messages. */

            message = new Label("Click \"New Game\" to begin.");
            message.setTextFill( Color.rgb(100,255,100) ); // Light green.
            message.setFont( Font.font(null, FontWeight.BOLD, 18) );

            /* Create the buttons and the board.  The buttons MUST be
             * created first, since they are used in the CheckerBoard
             * constructor! */

            newGameButton = new Button("New Game");
            resignButton = new Button("Resign");
            saveButton = new Button("Save Game");
            loadButton = new Button("Load Game");

            board = new CheckersBoard(); // a subclass of Canvas, defined below
            board.drawBoard();  // draws the content of the checkerboard

            /* Set up ActionEvent handlers for the buttons and a MousePressed handler
             * for the board.  The handlers call instance methods in the board object. */

            newGameButton.setOnAction( e -> board.doNewGame() );
            resignButton.setOnAction( e -> board.doResign() );
            saveButton.setOnAction( e -> board.doSave() );
            loadButton.setOnAction( e -> board.doLoad() );
            board.setOnMousePressed( e -> board.mousePressed(e) );

            /* Set the location of each child by calling its relocate() method */

            board.relocate(20,20);
            newGameButton.relocate(370, 100);
            loadButton.relocate(370,150);
            saveButton.relocate(370,200);
            resignButton.relocate(370,250);
            message.relocate(20, 370);

            /* Set the sizes of the buttons.  For this to have an effect, make
             * the butons "unmanaged."  If they are managed, the Pane will set
             * their sizes. */

            resignButton.setManaged(false);
            resignButton.resize(100,30);
            loadButton.setManaged(false);
            loadButton.resize(100,30);
            saveButton.setManaged(false);
            saveButton.resize(100,30);
            newGameButton.setManaged(false);
            newGameButton.resize(100,30);

            /* Create the Pane and give it a preferred size.   If the
             * preferred size were not set, the unmanaged buttons would
             * not be included in the Pane's computed preferred size. */

            Pane root = new Pane();

            root.setPrefWidth(500);
            root.setPrefHeight(420);

            /* Add the child nodes to the Pane and set up the rest of the GUI */

            root.getChildren().addAll(board, newGameButton, loadButton,
                                               saveButton, resignButton, message);
            root.setStyle("-fx-background-color: darkgreen; "
                               + "-fx-border-color: darkred; -fx-border-width:3");
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.setResizable(false);
            stage.setTitle("Checkers!");
            stage.show();

        } // end start()

        // --------------------  Nested Classes -------------------------------

        /**
         * 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.)
         */
        private static class CheckersMove {
            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.

        /**
         * This canvas displays a 320-by-320 checkerboard pattern with
         * a 2-pixel dark red border.  The canvas will be exactly
         * 324-by-324 pixels. This class contains methods that are
         * called in response to a mouse click on the canvas and
         * in response to clicks on the New Game and Resign buttons.
         * Note that the "New Game" and "Resign" buttons must be
         * created before the Board constructor is called, since
         * the constructor references the buttons (in the call to doNewGame()).
         */
        private class CheckersBoard extends Canvas {

            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.

            /**
             * Constructor.  Creates a CheckersData to represent the
             * contents of the checkerboard, and calls doNewGame to
             * start the first game.
             */
            CheckersBoard() {
                super(324,324);  // canvas is 324-by-324 pixels
                board = new CheckersData();
                doNewGame();
            }

            /**
             * Save the current state of the game to a user-selected file.
             */
            void doSave() {
                FileChooser fileDialog = new FileChooser();
                fileDialog.setInitialFileName("checkers.txt");
                fileDialog.setInitialDirectory(new File(System.getProperty("user.home")));
                fileDialog.setTitle("Select file where game should be saved");
                File selectedFile = fileDialog.showSaveDialog(window);
                if (selectedFile == null)
                    return;  // user canceled
                PrintWriter out;
                try {
                    FileWriter stream = new FileWriter(selectedFile);
                    out = new PrintWriter( stream );
                }
                catch (Exception e) {
                    Alert errorAlert = new Alert(Alert.AlertType.ERROR,
                            "Sorry, but an error occurred while\n" +
                                    "trying to open the file:\n" + e);
                    errorAlert.showAndWait();
                    return;
                }
                try {
                    out.println("CheckersWithFiles"); // Identifies file as a Checkers game.
                    for (int row = 0; row < 8; row++) {
                        for (int col = 0; col < 8; col++) {
                            int piece = board.pieceAt(row, col);
                            switch (piece) {
                            case CheckersData.EMPTY:      out.print(". ");  break;
                            case CheckersData.RED:        out.print("r ");  break;
                            case CheckersData.BLACK:      out.print("b ");  break;
                            case CheckersData.RED_KING:   out.print("R ");  break;
                            case CheckersData.BLACK_KING: out.print("B ");  break;
                            }
                        }
                        out.println();
                    }
                    out.println(currentPlayer == CheckersData.RED ? "Red" : "Black");
                    out.flush();
                    out.close();
                    if (out.checkError())
                        throw new IOException("Some error occurred while saving the file.");
                }
                catch (Exception e) {
                    Alert errorAlert = new Alert(Alert.AlertType.ERROR,
                            "Sorry, but an error occurred while\n" +
                                    "trying to write data to the file:\n" + e);
                    errorAlert.showAndWait();
                }
            }

            /**
             * Load a saved game from a file.
             */
            void doLoad() {
                FileChooser fileDialog = new FileChooser();
                fileDialog.setInitialDirectory(new File(System.getProperty("user.home")));
                fileDialog.setTitle("Select file where game was saved");
                File selectedFile = fileDialog.showOpenDialog(window);
                if (selectedFile == null)
                    return; // user canceled.
                Scanner in;
                try {
                    in = new Scanner(selectedFile);
                }
                catch (Exception e) {
                    Alert errorAlert = new Alert(Alert.AlertType.ERROR,
                            "Sorry, but an error occurred while\n" +
                                    "trying to open the file:\n" + e);
                    errorAlert.showAndWait();
                    return;
                }
                if (selectedFile == null)
                    return;  // user canceled
                try {
                    CheckersData newBoard = new CheckersData();
                    int newCurrentPlayer;
                    String programNameFromFile = in.nextLine();
                    if (! programNameFromFile.equals("CheckersWithFiles"))
                        throw new Exception("Selected file does not contain a checkers game.");
                    for (int row = 0; row < 8; row++) {
                        for (int col = 0; col < 8; col++) {
                            String pieceCode = in.next();
                            switch (pieceCode) {
                            case ".": newBoard.setPieceAt(row, col, CheckersData.EMPTY); break;
                            case "r": newBoard.setPieceAt(row, col, CheckersData.RED); break;
                            case "b": newBoard.setPieceAt(row, col, CheckersData.BLACK); break;
                            case "R": newBoard.setPieceAt(row, col, CheckersData.RED_KING); break;
                            case "B": newBoard.setPieceAt(row, col, CheckersData.BLACK_KING); break;
                            default: throw new Exception("Illegal board data found in file.");
                            }
                            if (row % 2 != col % 2 &&
                                    newBoard.pieceAt(row, col) != CheckersData.EMPTY)
                                throw new Exception("Illegal board data found in file.");
                        }
                        in.nextLine();  // Discard rest of line -- not really necessary.
                    }
                    String currentPlayerString = in.next();
                    in.close(); // Done reading data from the file.
                    if (currentPlayerString.equals("Red"))
                        newCurrentPlayer = CheckersData.RED;
                    else if (currentPlayerString.equals("Black"))
                        newCurrentPlayer = CheckersData.BLACK;
                    else
                        throw new Exception("Illegal current player found in file.");
                    board = newBoard;  // Set up game with data read from file.
                    currentPlayer = newCurrentPlayer;
                    legalMoves = board.getLegalMoves(currentPlayer);
                    selectedRow = -1;
                    gameInProgress = true;
                    newGameButton.setDisable(true);
                    loadButton.setDisable(true);
                    saveButton.setDisable(false);
                    resignButton.setDisable(false);
                    if (currentPlayer == CheckersData.RED)
                        message.setText("Game loaded -- it's RED's move.");
                    else
                        message.setText("Game loaded -- it's BLACK's move.");
                    drawBoard();
                }
                catch (Exception e) {
                    Alert errorAlert = new Alert(Alert.AlertType.ERROR,
                            "Sorry, but an error occurred while\n" +
                                    "trying to read the data:\n" + e);
                    errorAlert.showAndWait();
                }
            }

            /**
             * Start a new game.  This method is called when the Board is first
             * created and when the "New Game" button is clicked.  Event handling
             * is set up in the start() method in the main class.
             */
            void doNewGame() {
                if (gameInProgress == true) {
                        // This should not be possible, but it doesn'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.setDisable(true);
                loadButton.setDisable(false);
                saveButton.setDisable(true);
                resignButton.setDisable(false);
                drawBoard();
            }

            /**
             * Current player resigns.  Game ends.  Opponent wins.  This method is
             * called when the user clicks the "Resign" button.  Event handling is
             * set up in the start() method in the main class.
             */
            void doResign() {
                if (gameInProgress == false) {  // Should be impossible.
                    message.setText("There is no game in progress!");
                    return;
                }
                if (currentPlayer == CheckersData.RED)
                    gameOver("RED resigns.  BLACK wins.");
                else
                    gameOver("BLACK resigns.  RED wins.");
            }

            /**
             * The game ends.  The parameter, str, is displayed as a message
             * to the user.  The states of the buttons are adjusted so players
             * can start a new game.  This method is called when the game
             * ends at any point in this class.
             */
            void gameOver(String str) {
                message.setText(str);
                newGameButton.setDisable(false);
                loadButton.setDisable(false);
                saveButton.setDisable(true);
                resignButton.setDisable(true);
                gameInProgress = false;
            }

            /**
             * 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.
             */
            void doClickSquare(int row, int col) {

                /* 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.");
                        drawBoard();
                        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 square 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()

            /**
             * This is called when the current player has chosen the specified
             * move.  Make the move, and then either end or continue the game
             * appropriately.
             */
            void doMakeMove(CheckersMove move) {

                loadButton.setDisable(true);   // As soon as a move is made, disable Load.
                saveButton.setDisable(false);  //     and enable the Save.

                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;
                        drawBoard();
                        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 user 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. */

                drawBoard();

            }  // end doMakeMove();

            /**
             * Draw a checkerboard pattern in gray and lightGray.  Draw the
             * checkers.  If a game is in progress, highlight the legal moves.
             */
            public void drawBoard() {

                GraphicsContext g = getGraphicsContext2D();
                g.setFont( Font.font(18) );

                /* Draw a two-pixel black border around the edges of the canvas. */

                g.setStroke(Color.DARKRED);
                g.setLineWidth(2);
                g.strokeRect(1, 1, 322, 322);

                /* 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.setFill(Color.LIGHTGRAY);
                        else
                            g.setFill(Color.GRAY);
                        g.fillRect(2 + col*40, 2 + row*40, 40, 40);
                        switch (board.pieceAt(row,col)) {
                        case CheckersData.RED:
                            g.setFill(Color.RED);
                            g.fillOval(8 + col*40, 8 + row*40, 28, 28);
                            break;
                        case CheckersData.BLACK:
                            g.setFill(Color.BLACK);
                            g.fillOval(8 + col*40, 8 + row*40, 28, 28);
                            break;
                        case CheckersData.RED_KING:
                            g.setFill(Color.RED);
                            g.fillOval(8 + col*40, 8 + row*40, 28, 28);
                            g.setFill(Color.WHITE);
                            g.fillText("K", 15 + col*40, 29 + row*40);
                            break;
                        case CheckersData.BLACK_KING:
                            g.setFill(Color.BLACK);
                            g.fillOval(8 + col*40, 8 + row*40, 28, 28);
                            g.setFill(Color.WHITE);
                            g.fillText("K", 15 + col*40, 29 + row*40);
                            break;
                        }
                    }
                }

                /* If a game is in progress, highlight the legal moves.   Note that legalMoves
                 is never null while a game is in progress. */

                if (gameInProgress) {
                    /* First, draw a 4-pixel cyan border around the pieces that can be moved. */
                    g.setStroke(Color.CYAN);
                    g.setLineWidth(4);
                    for (int i = 0; i < legalMoves.length; i++) {
                        g.strokeRect(4 + legalMoves[i].fromCol*40,
                                                   4 + legalMoves[i].fromRow*40, 36, 36);
                    }
                    /* If a piece is selected for moving (i.e. if selectedRow >= 0), then
                        draw a yellow border around that piece and draw green borders
                        around each square that that piece can be moved to. */
                    if (selectedRow >= 0) {
                        g.setStroke(Color.YELLOW);
                        g.setLineWidth(4);
                        g.strokeRect(4 + selectedCol*40, 4 + selectedRow*40, 36, 36);
                        g.setStroke(Color.LIME);
                        g.setLineWidth(4);
                        for (int i = 0; i < legalMoves.length; i++) {
                            if (legalMoves[i].fromCol == selectedCol &&
                                                          legalMoves[i].fromRow == selectedRow) {
                                g.strokeRect(4 + legalMoves[i].toCol*40,
                                                          4 + legalMoves[i].toRow*40, 36, 36);
                            }
                        }
                    }
                }

            }  // end drawBoard()

            /**
             * 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.
             */
            public void mousePressed(MouseEvent evt) {
                if (gameInProgress == false)
                    message.setText("Click \"New Game\" to start a new game.");
                else {
                    int col = (int)((evt.getX() - 2) / 40);
                    int row = (int)((evt.getY() - 2) / 40);
                    if (col >= 0 && col < 8 && row >= 0 && row < 8)
                        doClickSquare(row,col);
                }
            }

        }  // end class Board

        /**
         * An object of this class holds data about a game of checkers.
         * It knows what kind of piece is on each square 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.
         */
        private static class CheckersData {

            /*  The following constants represent the possible contents of a square
                on the board.  The constants RED and BLACK also represent players
                in the game. */

            static final int
                        EMPTY = 0,
                        RED = 1,
                        RED_KING = 2,
                        BLACK = 3,
                        BLACK_KING = 4;

            int[][] board;  // board[r][c] is the contents of row r, column c.

            /**
             * Constructor.  Create the board and set it up for a new game.
             */
            CheckersData() {
                board = new int[8][8];
                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.
             */
            void setUpGame() {
                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()

            /**
             * Return the contents of the square in the specified row and column.
             */
            int pieceAt(int row, int col) {
                return board[row][col];
            }

            /**
             * 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.
             */
            void setPieceAt(int row, int col, int piece) {
                board[row][col] = piece;
            }

            /**
             * Make the specified move.  It is assumed that move
             * is non-null and that the move it represents is legal.
             */
            void makeMove(CheckersMove move) {
                makeMove(move.fromRow, move.fromCol, move.toRow, move.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 to
             * the last row on the opponent's side of the board, the
             * piece becomes a king.
             */
            void makeMove(int fromRow, int fromCol, int toRow, int toCol) {
                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;
            }

            /**
             * Return an array containing all the legal CheckersMoves
             * for the specified 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.
             */
            CheckersMove[] getLegalMoves(int player) {

                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;

                ArrayList<CheckersMove> moves = new ArrayList<CheckersMove>();

                /*  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 ArrayList.
                 */

                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.add(new CheckersMove(row, col, row+2, col+2));
                            if (canJump(player, row, col, row-1, col+1, row-2, col+2))
                                moves.add(new CheckersMove(row, col, row-2, col+2));
                            if (canJump(player, row, col, row+1, col-1, row+2, col-2))
                                moves.add(new CheckersMove(row, col, row+2, col-2));
                            if (canJump(player, row, col, row-1, col-1, row-2, col-2))
                                moves.add(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 regular 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 ArrayList.
                 */

                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.add(new CheckersMove(row,col,row+1,col+1));
                                if (canMove(player,row,col,row-1,col+1))
                                    moves.add(new CheckersMove(row,col,row-1,col+1));
                                if (canMove(player,row,col,row+1,col-1))
                                    moves.add(new CheckersMove(row,col,row+1,col-1));
                                if (canMove(player,row,col,row-1,col-1))
                                    moves.add(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 ArrayList 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] = moves.get(i);
                    return moveArray;
                }

            }  // end getLegalMoves

            /**
             * 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.
             */
            CheckersMove[] getLegalJumpsFrom(int player, int row, int col) {
                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;
                ArrayList<CheckersMove> moves = new ArrayList<CheckersMove>();
                       // The legal jumps will be stored in this list.
                if (board[row][col] == player || board[row][col] == playerKing) {
                    if (canJump(player, row, col, row+1, col+1, row+2, col+2))
                        moves.add(new CheckersMove(row, col, row+2, col+2));
                    if (canJump(player, row, col, row-1, col+1, row-2, col+2))
                        moves.add(new CheckersMove(row, col, row-2, col+2));
                    if (canJump(player, row, col, row+1, col-1, row+2, col-2))
                        moves.add(new CheckersMove(row, col, row+2, col-2));
                    if (canJump(player, row, col, row-1, col-1, row-2, col-2))
                        moves.add(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] = moves.get(i);
                    return moveArray;
                }
            }  // end getLegalMovesFrom()

            /**
             * 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).
             */
            private boolean canJump(int player, int r1, int c1, int r2, int c2, int r3, int 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()

            /**
             * 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.
             */
            private boolean canMove(int player, int r1, int c1, int r2, int c2) {

                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;  // Regular 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

    } // end class CheckersWithFiles

[ Exercises | Chapter Index | Main Index ]