question archive JAVA)Problem Specification: Develop a program in Java using GUI components that will draw a checkerboard and place the checker pieces on it at their starting position as shown in Figure 1

JAVA)Problem Specification: Develop a program in Java using GUI components that will draw a checkerboard and place the checker pieces on it at their starting position as shown in Figure 1

Subject:Computer SciencePrice:3.87 Bought7

JAVA)Problem Specification: Develop a program in Java using GUI components that will draw a checkerboard and place the checker pieces on it at their starting position as shown in Figure 1. The board is made up of 64 alternating green and white squares which appear in 8 rows of 8. There are 32 green squares and 32 white squares. Each player places his pieces on the 12 green squares in the first three rows closest to the player. Each of these three rows should have a total of 4 checkers. Remember that checkers may only move in diagonal directions on the green squares. Since the board has 8 rows, 6 of the rows will be taken up by the players' checkers and the remaining two rows will be left open in the middle of the board. The pieces are specified by an 8-by-8 char array called boardStatus. The status of any given square is either r (red), b (balck), or e (empty) as shown in the figure below

The above design of the GUI involves three classes, CheckerPiece, CheckerBoard and CheckerGame. The specifications for these three classes are discussed below.

1. CheckerPiece Class:

This class represents a square in the checkerboard that extends the JComponent class.

It draws a square if it is empty (e) or a square with a black(b) and red(r) checkers if it is red or black respectively.

It overrides the paintComponent(Graphics g) method to draw.

You can use fillRect(), fillOval(), drawRect() methods to draw squares and checkers.

In this class, you will need to have the following instance variables and methods: o Instance variables: ? char status:

It stores the status of a square in the checkerboard. The valid values are 'r', 'b' and 'e'. ? int row, int column:

The row and column indices of the square in the checkerboard. For example, row is 0 and column is 0 for the top-left square in Figure 1. Also, row is 7 and column is 7 for the bottom-right square.

? You can declare as many instance variables and constants as you think necessary. For example, you can define constant variables for the height and width of the squares and checkers, also you can do the same and define constants for the x and y coordinates for the squares and checkers.

In Figure 1, the length of the side of the squares is 60 pixels and the length of the side of the checkers is 40 pixels. o

Methods: ?

public CheckerPiece(int row, int column, char status) The constructor that sets the row, column and status of a checker piece. It throws an IllegalArgumentException if the value of the row, column or status is invalid. For example, row and column need to be between 0 and 7, and the status can either be 'e', 'b' or 'r'. Also, throw an IllegalArgumentException if there is an attempt to put a red or black checker on an invalid square i.e. checkers cannot be placed on white squares. ?

public void paintComponent(Graphics g) You need to Override the paintComponent function from JComponent class to draw checkerboard squares with or without the checker piece based on the value of the status.

You can add as many helper methods or inner classes as you think necessary if you would like to make it functional .

2. CheckerBoard Class:

This class represents a checkerboard that contains 64 CheckerPiece objects (squares with/without checkers).

It extends the JPanel class and adds 64 CheckerPiece objects to the panel.

You can use the GridLayout, 8 x 8, to organize the CheckerPiece objects on the panel.

In this class, you will need to have the following instance variables and methods: o Instance variables: ? char [][] boardStatus: A two-dimensional character array, 8 x 8, that stores the status value for each of the squares. ?

You can declare as many instance variables and constants as you think necessary.

I have defined two constants, ROW and COLUMN for the boardStatus array.

Methods: ?

public CheckerBoard(char [][] boardStatus) The constructor that sets the boardStatus array using the received parameter.

It also uses this array to instantiate 64 CheckerPiece objects based on the row, column and status values.

It adds all these objects to the panel. As mentioned above, you can use the GridLayout, 8 x 8, to organize the CheckerPiece objects on the panel. ?

public void setBoardStatus(char [][] boardStatus) A mutator method that sets the boardStatus array using the received parameter.

You can call repaint() method after setting the array to redraw the checker pieces based on the new value. ?

public void setCheckerPiece(int row, int column, char status) A mutator method that sets the status value for the square specified by row and column. You can call repaint() method after setting the status to redraw the checker pieces based on the new value.

You can add as many helper methods or inner classes as you think necessary if you would like to make it functional

. 3. CheckerGame Class: This driver class represents the checker game window. The checker game window has a checkerboard along with the status bar and menu bar. The status bar is a JPanel object with two JLabels that show the number of red and black checkers remaining during the game and the name of the programmer, YOUR NAME, who developed this game. That being said, the CheckerGame class extends JFrame to create window and adds a CheckerBoard object (a subclass of JPanel) and a status panel. It also adds a menu bar to the window as shown in Figure 2.

In this class, you will need to have the following instance variables and methods:

Instance variables: ? char [][] boardStatus: A two-dimensional character array, 8 x 8, that will represent the status of the required checkerBoard. stores the status value for each of the squares. See figure 2 (left)

You can declare as many instance variables and constants as you think necessary. I didn't use any for the non-functional GUI.

Methods: ? public CheckerGame() The constructor that sets the layout to organize and add the CheckerBoard object (a JPanel), the status bar (a JPanel object) and the menu bar (a JMenu Object). You need to pass a two-dimensional char array with boardStatus, same as Figure 1 and 2 (left) when you create CheckerBoard object. You can use the BorderLayout to set the layout of the JFrame to organize the components. Also, set the title, size or dimension, default close operation, etc. for the JFrame window as shown in Figure 1 and Figure 2 . I have used 505 x 585 pixels dimension for the game window displayed in Figure 1 and 2. Add menus and menu items to the menu bar as shown in Figure 2, you are not required to add actionlistener for these menu items

? public static void main(String[] args) Create object of the CheckerGame class and make the window visible. You can add as many helper classes and methods as you deem necessary. The classes and methods specified above are good enough for the non-functional GUI of a checkerboard game.

Notes: Your Program should Throw an IllegalArgumentException in the following situations: o The CheckerPiece class has the responsibility to throw the exception:

if the status of the square is anything other than the three characters 'r', 'b', and 'e'.

if there is an attempt to put a red or black checker on an invalid square of the checkerboard (checkers are only allowed to be placed on the green squares).

make the following:

1. Make the game work following the game rules described in the link above and summarized in the following majors rules: ? It should be a two-player game where the player with black checkers will play first and then the player with Red. These turns will continue until the game is over. ? A player can not move one space diagonally to an empty square or jump over the opponent's checker piece. ? You need to update the board with every valid move the players make. ? To move a checker piece, the player has to click on that piece and then click the empty square where she/he wants to move that piece. Don't move if a player makes an invalid move.

2. Make the menu items work from the menu bar functional according to the following ? New: It starts a new game. Resets the checkerboard to initial configuration. ? Exit: Exit the game or program. ? Checker Game Rules: Display a JOptionPane message box that has the link to the game rules. ? About Checker Game App: Display a JOptionPane message box that has your name, email and the university name.

3. Update the status, you need to update the status label with the current count of black and red checkers as the game goes on. Also, declare the winner (result) once the game is over.

 

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Answer:

CheckerGame.java

 

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;


import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


/**
 * The CheckerGame application that works as a 2-player game with GUI
 */
public class CheckerGame extends JFrame implements MouseListener, ActionListener {


    //Fields
    private boolean turnBlack, captureMove, mustJump;
    private CheckerBoard cb;
    private JLabel statusLabel;
    private int picks;
    private CheckerPiece tbm, dest;
    private JOptionPane aboutPane, rulesPane;


    private static char[][] boardStatus = new char[][]{
        {'e', 'b', 'e', 'b', 'e', 'b', 'e', 'b'},
        {'b', 'e', 'b', 'e', 'b', 'e', 'b', 'e'},
        {'e', 'b', 'e', 'b', 'e', 'b', 'e', 'b'},
        {'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'},
        {'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'},
        {'r', 'e', 'r', 'e', 'r', 'e', 'r', 'e'},
        {'e', 'r', 'e', 'r', 'e', 'r', 'e', 'r'},
        {'r', 'e', 'r', 'e', 'r', 'e', 'r', 'e'},};


    /**
     * Default constructor for the class
     */
    public CheckerGame() {
        //Initializes fields and components
        picks = 0;
        turnBlack = true;
        mustJump = false;
        setSize(505, 585);
        cb = new CheckerBoard(boardStatus);
        addMouseEvent(cb);
        JMenuBar menuBar = new JMenuBar();
        JPanel statusPanel = new JPanel(new GridLayout(2, 1));
        aboutPane = new JOptionPane("About");


        //Set up the status bar
        statusLabel = new JLabel("New game! Black starts first.", JLabel.CENTER);
        JLabel informationLabel = new JLabel("This game was developed by YOUR NAME.", JLabel.CENTER);
        statusPanel.add(statusLabel);
        statusPanel.add(informationLabel);


        //Game menu
        JMenu gameMenu = new JMenu("Game");
        JMenuItem newItem = new JMenuItem("New");
        newItem.addActionListener(this);
        JMenuItem exitItem = new JMenuItem("Exit");
        exitItem.addActionListener(this);
        gameMenu.add(newItem);
        gameMenu.add(exitItem);


        //Help menu
        JMenu helpMenu = new JMenu("Help");
        JMenuItem rulesItem = new JMenuItem("Checker Game Rules");
        rulesItem.addActionListener(this);
        JMenuItem aboutItem = new JMenuItem("About Checker Game App");
        aboutItem.addActionListener(this);
        helpMenu.add(rulesItem);
        helpMenu.add(aboutItem);


        menuBar.add(gameMenu);
        menuBar.add(helpMenu);


        //Add components to the frame
        setJMenuBar(menuBar);
        add(cb, BorderLayout.CENTER);
        add(statusPanel, BorderLayout.SOUTH);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }


    //Main method
    public static void main(String[] args) {
        CheckerGame cg = new CheckerGame();
    }


    /**
     * Gets the status label
     *
     * @return the status label
     */
    public JLabel getStatusLabel() {
        return statusLabel;
    }


    /**
     * Sets the the checker to be moved
     *
     * @param toBeMoved the checker to be moved to set
     */
    public void setToBeMoved(CheckerPiece toBeMoved) {
        if (toBeMoved == null) {
            statusLabel.setText("You haven't picked a checker. Please pick a black one.");
            return;
        }
        switch (toBeMoved.validToBeMoved(turnBlack)) {
            case 2:
                statusLabel.setText("No checker to be moved. Please pick another one");
                break;
            case 3:
                statusLabel.setText("It's black turn. Please pick a black checker.");
                break;
            case 4:
                statusLabel.setText("It's red turn. Please pick a red checker.");
                break;
            case 1:
                this.tbm = toBeMoved;
                picks = 1;
                if (turnBlack) {
                    statusLabel.setText("A black checker was picked");
                } else {
                    statusLabel.setText("A red checker was picked");
                }
        }
    }


    /**
     * Sets the destination square, if can't, then displays message according to
     * the error code
     *
     * @param destination the destination square to set
     */
    public void setDestination(CheckerPiece dest) {
        if (dest == null) {
            statusLabel.setText("You haven't picked a checker. Please pick a black one.");
            return;
        }
        switch (dest.validDestination(tbm, cb.getBoardStatus())) {
            case 2:
                statusLabel.setText("The square is not empty. Please make another move");
                picks = 0;
                break;
            case 3:
                statusLabel.setText("Can only move diagonal. Please make another move");
                picks = 0;
                break;
            case 4:
                statusLabel.setText("Cannot move horizontally. Please make another move");
                picks = 0;
                break;
            case 5:
                statusLabel.setText("Cannot move backward. Please make another move");
                picks = 0;
                break;
            case 6:
                statusLabel.setText("Cannot move more than one space. Please make another move");
                picks = 0;
                break;
            case 1:
                this.dest = dest;
                picks = 2;
                if (dest.isACapture(tbm, cb.getBoardStatus())) {
                    captureMove = true;
                }
        }
    }


    /**
     * Captures a checker between the checker to be moved and the destination
     * square
     *
     * @param first the checker to be moved
     * @param second the destination square
     */
    private void capture(CheckerPiece first, CheckerPiece second) {
        if (first.getStatus() == 'r' || first.getStatus() == 'q') {
            cb.setBlack(cb.getBlack() - 1);
        }
        if (first.getStatus() == 'b' || first.getStatus() == 'k') {
            cb.setRed(cb.getRed() - 1);
        }
        //Removes the captured checker
        cb.setCheckerPiece((first.getRow() + second.getRow()) / 2,
                (first.getCol() + second.getCol()) / 2, 'e');
    }


    /**
     * Adds mouseEvent to every CheckerPiece object in the panel
     *
     * @param cb the checker board that has all the checker pieces
     */
    private void addMouseEvent(CheckerBoard cb) {
        for (int i = 0; i < cb.getBoardStatus().length; i++) {
            for (int j = 0; j < cb.getBoardStatus().length; j++) {
                ((CheckerPiece) cb.getComponent(i * 8 + j)).addMouseListener(this);
            }


        }
    }


    @Override
    public void mouseClicked(MouseEvent e) {
        CheckerPiece cp = (CheckerPiece) e.getComponent();
        statusLabel.setText(cb.toString());
        if (picks == 0) {
            setToBeMoved(cp);
        } else if (picks == 1) {
            setDestination(cp);
        }
        if (picks == 2 && tbm != null && dest != null) {
            if (mustJump() && !captureMove) {
                statusLabel.setText("You must make a jump if you can! Please make a jump move");
                resetFields();
            } else {
                moveChecker(tbm, dest);
                resetFields();
                if (turnBlack) {
                    statusLabel.setText(cb.toString() + "[Black turn]");
                } else {
                    statusLabel.setText(cb.toString() + "[Red turn]");
                }
            }
        }
        if (cb.notMoveable()) {
            endGame();
        }


    }


    /**
     * Moves the checker (captures if it's capturing)
     *
     * @param first the checker to be moved
     * @param second the destination square
     */
    private void moveChecker(CheckerPiece first, CheckerPiece second) {
        //Captures if it's capturing
        if (captureMove) {
            capture(first, second);
        }
        //Moves the checker to the new destination
        cb.setCheckerPiece(second.getRow(), second.getCol(), first.getStatus());
        cb.setCheckerPiece(first.getRow(), first.getCol(), 'e');
        cb.setCheckersState();
    }


    /**
     * Displays the winner and disables all moves afterwards
     */
    private void endGame() {
        if (turnBlack) {
            statusLabel.setText("Red won!");
        } else {
            statusLabel.setText("Black won!");
        }
        cb.setEnabled(false);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("New")) {
            reset();
        }
        if (e.getActionCommand().equals("Exit")) {
            dispose();
        }
        if (e.getActionCommand().equals("Checker Game Rules")) {
            JOptionPane.showMessageDialog(this, "For more information, use the link:\n"
                    + "#url", "Rules", JOptionPane.INFORMATION_MESSAGE);
        }
        if (e.getActionCommand().equals("About Checker Game App")) {
            JOptionPane.showMessageDialog(this, "Name, e-mail, University",
                    "About", JOptionPane.INFORMATION_MESSAGE);
        }
    }


    /**
     * Resets the fields to the original state
     */
    private void resetFields() {
        if ((captureMove && dest.isCapturable()) || (dest.isKing() && dest.isCapturable())) {
            picks = 1;
            tbm = dest;
            dest = null;
        } else {
            if (!mustJump || captureMove) {
                turnBlack = !turnBlack;
            }
            picks = 0;
            tbm = null;
            dest = null;
        }
        mustJump = mustJump();
        captureMove = false;
    }


    /**
     * Check if a capture move can be made
     *
     * @return true if a capture move can be moved, false otherwise
     */
    private boolean mustJump() {
        if (turnBlack && cb.getBlackCanCapture() != 0) {
            return true;
        }
        if (!turnBlack && cb.getRedCanCapture() != 0) {
            return true;
        }
        return false;
    }


    /**
     * Resets the checker board and all the related fields
     */
    private void reset() {
        cb.reset(boardStatus);
        statusLabel.setText("New Game! Black starts first.");
        mustJump = false;
        captureMove = false;
        tbm = null;
        dest = null;
        picks = 0;
        turnBlack = true;
    }


    @Override
    public void mousePressed(MouseEvent e) {
    }


    @Override
    public void mouseReleased(MouseEvent e) {
    }


    @Override
    public void mouseEntered(MouseEvent e) {
    }


    @Override
    public void mouseExited(MouseEvent e) {
    }
}


 

 

CheckerPiece.java

 

 

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


import javax.swing.JComponent;


/**
 * A class that represents a checker square on the board and draws the
 * appropriate square that matches it status
 */
public class CheckerPiece extends JComponent {


    //Fields
    private char status;
    private int row, col;
    private final int SIDE = 60, RADIUS = 40;
    private boolean capturable, moveable;


    /**
     * Construct a CheckerPiece object using the inputs
     *
     * @param row the row of the checker square (0 index)
     * @param col the column of the checker square (0 index)
     * @param status the status of the checker square (e, b, or r)
     */
    public CheckerPiece(int row, int col, char status) {
        if (row < 0 || row > 7) {
            throw new IllegalArgumentException();
        }
        if (col < 0 || col > 7) {
            throw new IllegalArgumentException();
        }
        if (status != 'r' && status != 'b' && status != 'e' && status != 'k' && status != 'q') {
            throw new IllegalArgumentException();
        }
        if ((col + row) % 2 == 0 && status != 'e') {
            throw new IllegalArgumentException();
        }
        if ((col + row) % 2 == 1 && status == 'e' && (row < 3 || row > 4)) {
            throw new IllegalArgumentException();
        }
        this.row = row;
        this.col = col;
        this.status = status;
    }


    @Override
    public void paintComponent(Graphics g) {
        if ((col + row) % 2 == 0) {
            g.setColor(Color.WHITE);
        } else {
            g.setColor(Color.GREEN);
        }


        g.fillRect(0, 0, SIDE, SIDE);
        g.setColor(Color.BLACK);
        g.drawRect(0, 0, SIDE, SIDE);


        if (status == 'e') {
            return;
        }
        if (status == 'b' || status == 'k') {
            g.setColor(Color.BLACK);
        }
        if (status == 'r' || status == 'q') {
            g.setColor(Color.RED);
        }


        g.fillOval(10, 10, RADIUS, RADIUS);
        g.setColor(Color.BLACK);
        if (this.isKing()) {
            g.setColor(Color.YELLOW);
        }
        g.drawOval(10, 10, RADIUS, RADIUS);


    }


    /**
     * Returns the status of the checker square
     *
     * @return the status
     */
    public char getStatus() {
        return status;
    }


    /**
     * Gets the status of the checker square
     *
     * @param status the status to set
     */
    public void setStatus(char status) {
        this.status = status;
        repaint();
    }


    /**
     * Gets the row of the checker square (0 index)
     *
     * @return the row
     */
    public int getRow() {
        return row;
    }


    /**
     * Gets the column of the checker square (0 index)
     *
     * @return the col
     */
    public int getCol() {
        return col;
    }


    /**
     * Gets the captrueable field of the checker piece
     *
     * @return the capturable field
     */
    public boolean isCapturable() {
        return capturable;
    }


    /**
     * Sets the captureable field of the checker set
     *
     * @param capturable the value for the capturable field to set
     */
    public void setCapturable(boolean capturable) {
        this.capturable = capturable;
    }


    /**
     * Gets the moveable field of the checker piece
     *
     * @return the value of the moveable
     */
    public boolean isMoveable() {
        return moveable;
    }


    /**
     * Sets the moveable field of the checker piece
     *
     * @param moveable the value of the moveable field to set
     */
    public void setMoveable(boolean moveable) {
        this.moveable = moveable;
    }


    /**
     * Check if a checker it's a king by checking the row of the checker
     *
     * @return true if the checker is a king, false otherwise
     */
    public boolean isKing() {
        if (status == 'b' & row == 7) {
            status = 'k';
            return true;
        }
        if (status == 'r' & row == 0) {
            status = 'q';
            return true;
        }
        if (status == 'k' || status == 'q') {
            return true;
        }
        return false;
    }


    /**
     * Checks if a CheckerPiece object is valid to be the moved. It's not valid
     * when the CheckerPiece is empty, or has the wrong color for the turn
     *
     * @param turnBlack boolean value shows whose turn it is
     * @return 1 if the checker piece is valid to be moved, false 2, 3, 4
     * otherwise
     */
    public int validToBeMoved(boolean turnBlack) {
        if (status == 'e') {
            return 2;
        }
        if (turnBlack && (status == 'r' || status == 'q')) {
            return 3;
        }
        if (!turnBlack && (status == 'b' || status == 'k')) {
            return 4;
        }
        return 1;
    }


    /**
     * Checks if a CheckerPiece object is valid to be the destination.
     *
     * @param tbm the checker piece that will be moved
     * @param boardStatus the board status
     * @return 1 if the checker piece is valid to be the destination; 2, 3, 4,
     * 5, 6 if not
     */
    public int validDestination(CheckerPiece tbm, char[][] boardStatus) {
        if (status != 'e') {
            return 2;
        }
        if ((row + col) % 2 == 0) {
            return 3;
        }
        if (tbm.getRow() == this.row) {
            return 4;
        }
        if (!tbm.isKing() && !isForward(tbm)) {
            return 5;
        }
        if (!isACapture(tbm, boardStatus) && !isValidMove(tbm)) {
            return 6;
        }
        return 1;
    }


    /**
     * Checks if the current checker is capturing another checker or not
     *
     * @param first the checker to be moved
     * @param second the destination square
     * @return true if a checker is being captured, false otherwise
     */
    public boolean isACapture(CheckerPiece tbm, char[][] boardStatus) {
        if (Math.abs(row - tbm.getRow()) != 2) {
            return false;
        }
        if (Math.abs(col - tbm.getCol()) != 2) {
            return false;
        }
        char mid = boardStatus[(tbm.getRow() + row) / 2][(tbm.getCol() + col) / 2];
        if (mid == status || mid == 'e') {
            return false;
        }
        return true;
    }


    /**
     * Checks if the move being made is valid or not (without capturing)
     *
     * @param first the checker to be moved
     * @param second the destination square
     * @return true if a checker is being captured, false otherwise
     */
    private boolean isValidMove(CheckerPiece tbm) {
        if (Math.abs(tbm.getRow() - row) != 1) {
            return false;
        }
        if (Math.abs(tbm.getCol() - col) != 1) {
            return false;
        }
        return true;
    }


    /**
     * Checks if the destination is considered as forward comparing to the
     * checker that will be moved
     *
     * @param tbm the checker that will be moved
     * @return true if it's forward, false otherwise
     */
    public boolean isForward(CheckerPiece tbm) {
        if (tbm.getStatus() == 'b' && row > tbm.getRow()) {
            return true;
        }
        if (tbm.getStatus() == 'r' && row < tbm.getRow()) {
            return true;
        }
        return false;


    }


    @Override
    public String toString() {
        return "CheckerPiece [status=" + status + ", row=" + row + ", col=" + col + ", capturable=" + capturable
                + ", moveable=" + moveable + "]";
    }


}

 

 

 

CheckerBoard.java

 

 

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Arrays;


import javax.swing.JButton;
import javax.swing.JPanel;


/**
 * A class that extends the JPanel to represents a checker board that draws the
 * entire checker board by drawing individual checker squares
 */
public class CheckerBoard extends JPanel {


    private int black, red, blackCanCapture, redCanCapture;
    private char[][] boardStatus;


    /**
     * Construct using inputs to create a CheckerBoard object and draw the
     * checker board
     *
     * @param boardStatus the 2D array that shows the status (b, r, or e) of the
     * checker squares by rows and columns
     */
    public CheckerBoard(char[][] boardStatus) {
        black = 12;
        red = 12;
        this.boardStatus = new char[8][8];
        setLayout(new GridLayout(8, 8));
        for (int i = 0; i < boardStatus.length; i++) {
            for (int j = 0; j < boardStatus[i].length; j++) {
                this.boardStatus[i][j] = boardStatus[i][j];
                add(new CheckerPiece(i, j, boardStatus[i][j]));
            }
        }
        setCheckersState();
    }


    /**
     * Sets the boardStatus field
     *
     * @param boardStatus a 2D arrays of characters ('b', 'r', or 'e') to set
     */
    public void setBoardStatus(char[][] boardStatus) {
        this.boardStatus = boardStatus;
    }


    /**
     * Sets the status of a checker square
     *
     * @param row the row of the checker square to set
     * @param col the column of the checker square to set
     * @param status the status of the checker square to set
     */
    public void setCheckerPiece(int row, int col, char status) {
        boardStatus[row][col] = status;
        ((CheckerPiece) this.getComponent(row * 8 + col)).setStatus(status);
    }


    /**
     * Gets the number of black checkers that can make a capture move
     *
     * @return the number of black checkers that can make a capture move
     */
    public int getBlackCanCapture() {
        return blackCanCapture;
    }


    /**
     * Gets the number of red checkers that can make a capture move
     *
     * @return the number of red checkers that can make a capture move
     */
    public int getRedCanCapture() {
        return redCanCapture;
    }


    /**
     * Gets the number of black checkers on the board
     *
     * @return the number of black checkers on the board
     */
    public int getBlack() {
        return black;
    }


    /**
     * Sets the number of black checkers on the board
     *
     * @param black the numbers of black checkers to set
     */
    public void setBlack(int black) {
        this.black = black;
    }


    /**
     * Gets the number of red checkers on the board
     *
     * @return the number of red checkers
     */
    public int getRed() {
        return red;
    }


    /**
     * Sets the number of red checkers on the board
     *
     * @param red the red to set
     */
    public void setRed(int red) {
        this.red = red;
    }


    /**
     * Gets the boardStatus field
     *
     * @return the boardStatus
     */
    public char[][] getBoardStatus() {
        return boardStatus;
    }


    /**
     * Gets the CheckerPiece object using the row and column of that checker
     * square
     *
     * @param row the row of the checker square
     * @param col the column of the checker square
     * @return the CheckerPiece object that associated with the square
     */
    public CheckerPiece getCheckerPiece(int row, int col) {
        return ((CheckerPiece) this.getComponent(row * 8 + col));
    }


    /**
     * Resets the entire checker board back to the initial state
     *
     * @param boardStatus the 2D arrays that have the original status of the
     * checker squares
     */
    public void reset(char[][] boardStatus) {
        black = 12;
        red = 12;
        for (int i = 0; i < boardStatus.length; i++) {
            for (int j = 0; j < boardStatus[i].length; j++) {
                //Changes the status of the square if it's not the same as the beginning
                if (boardStatus[i][j] != this.boardStatus[i][j]) {
                    setCheckerPiece(i, j, boardStatus[i][j]);
                }
            }
        }
        setCheckersState();
    }


    /**
     * Checks if any more moves can be made
     *
     * @return true if no more moves can be made, false otherwise
     */
    public boolean notMoveable() {
        if (black == 0) {
            return true;
        }
        if (red == 0) {
            return true;
        }
        int redMoveable = 0, blackMoveable = 0;
        for (Component c : getComponents()) {
            CheckerPiece cp = (CheckerPiece) c;
            if (cp.isMoveable()) {
                if (cp.getStatus() == 'b' || cp.getStatus() == 'k') {
                    blackMoveable++;
                }
                if (cp.getStatus() == 'r' || cp.getStatus() == 'q') {
                    redMoveable++;
                }
            }
        }
        return redMoveable == 0 || blackMoveable == 0;
    }


    /**
     * Sets the captureable and moveable fields of every CheckePiece object
     */
    public void setCheckersState() {
        blackCanCapture = 0;
        redCanCapture = 0;
        for (Component c : getComponents()) {
            CheckerPiece cp = (CheckerPiece) c;
            cp.setCapturable(captureable(cp.getRow(), cp.getCol()));
            cp.setMoveable(moveable(cp.getRow(), cp.getCol()));
            if ((cp.getStatus() == 'b' || cp.getStatus() == 'k') && cp.isCapturable()) {
                blackCanCapture++;
            }
            if ((cp.getStatus() == 'r' || cp.getStatus() == 'q') && cp.isCapturable()) {
                redCanCapture++;
            }
        }
    }


    /**
     * Checks if a black checker is moveable
     *
     * @param row the row of the black checker
     * @param col the column of the black checker
     * @return true if the black checker is moveable, false otherwise
     */
    private boolean blackMoveable(int row, int col) {
        if (blackCaptureable(row, col)) {
            return true;
        }
        if (row + 1 <= 7) {
            if (col - 1 >= 0 && boardStatus[row + 1][col - 1] == 'e') {
                return true;
            }
            if (col + 1 <= 7 && boardStatus[row + 1][col + 1] == 'e') {
                return true;
            }
        }
        return false;
    }


    /**
     * Checks if a red checker is moveable
     *
     * @param row the row of the red checker
     * @param col the column of the red checker
     * @return true if the red checker is moveable, false otherwise
     */
    private boolean redMoveable(int row, int col) {
        if (redCaptureable(row, col)) {
            return true;
        }
        if (row - 1 >= 0) {
            if (col - 1 >= 0 && boardStatus[row - 1][col - 1] == 'e') {
                return true;
            }
            if (col + 1 <= 7 && boardStatus[row - 1][col + 1] == 'e') {
                return true;
            }
        }
        return false;
    }


    /**
     * Checks if a red checker can make a capture move
     *
     * @param row the row of the red checker
     * @param col the column of the red checker
     * @return true if the red checker can make capture move, false otherwise
     */
    private boolean redCaptureable(int row, int col) {
        if (row - 2 >= 0) {
            if (col - 2 >= 0 && boardStatus[row - 2][col - 2] == 'e' && boardStatus[row - 1][col - 1] == 'b') {
                redCanCapture++;
                return true;
            }
            if (col + 2 <= 7 && boardStatus[row - 2][col + 2] == 'e' && boardStatus[row - 1][col + 1] == 'b') {
                redCanCapture++;
                return true;
            }
        }
        return false;
    }


    /**
     * Checks if a black checker can make a capture move
     *
     * @param row the row of the black checker
     * @param col the column of the black checker
     * @return true if the black checker can make capture move, false otherwise
     */
    private boolean blackCaptureable(int row, int col) {
        if (row + 2 <= 7) {
            if (col - 2 >= 0 && boardStatus[row + 2][col - 2] == 'e' && boardStatus[row + 1][col - 1] == 'r') {
                return true;
            }
            if (col + 2 <= 7 && boardStatus[row + 2][col + 2] == 'e' && boardStatus[row + 1][col + 1] == 'r') {
                return true;
            }
        }
        return false;


    }


    /**
     * Checks if a king checker can make a capture move
     *
     * @param row the row of the king checker
     * @param col the column of the king checker
     * @return true if the king checker can make a capture move, false otherwise
     */
    private boolean kingCaptureable(int row, int col) {
        return blackCaptureable(row, col) || redCaptureable(row, col);
    }


    /**
     * Checks if a king checker is moveable
     *
     * @param row the row of the king checker
     * @param col the column of the king checker
     * @return true if a king checker is moveable, false otherwise
     */
    private boolean kingMoveable(int row, int col) {
        return blackMoveable(row, col) || redMoveable(row, col);
    }


    /**
     * Checks if a checker can make a capture move
     *
     * @param row the row of the checker
     * @param col the column of the checker
     * @return true if the checker can make a capture move, false otherwise
     */
    boolean captureable(int row, int col) {
        if (boardStatus[row][col] == 'b') {
            return blackCaptureable(row, col);
        }
        if (boardStatus[row][col] == 'r') {
            return redCaptureable(row, col);
        }
        if (boardStatus[row][col] == 'k' || boardStatus[row][col] == 'q') {
            return kingCaptureable(row, col);
        }
        return false;
    }


    /**
     * Checks is a checker is move able
     *
     * @param row the row of the checker
     * @param col the column of the checker
     * @return true if the checker is moveable, false otherwise
     */
    public boolean moveable(int row, int col) {
        if (boardStatus[row][col] == 'b') {
            return blackMoveable(row, col);
        }
        if (boardStatus[row][col] == 'r') {
            return redMoveable(row, col);
        }
        if (boardStatus[row][col] == 'k' || boardStatus[row][col] == 'q') {
            return kingMoveable(row, col);
        }
        return false;
    }


    @Override
    public String toString() {
        return "Black: " + black + ", Red: " + red + " ";
    }
}

please use this google drive link to download the answer file.

https://drive.google.com/file/d/1Stn5rhvtER17GzrM93tG6oudtXGM1XHA/view?usp=sharing

note: if you have any trouble in viewing/downloading the answer from the given link, please use this below guide to understand the whole process.

https://helpinhomework.org/blog/how-to-obtain-answer-through-google-drive-link

Related Questions