Write a JFrame program to create a limited version of chess that uses three types of listeners to interact with the program. A key listener will be added to allow the user to select a chess piece when a certain key is pressed. A mouse listener will be added to allow the user to press the mouse to place a selected chess piece on a particular square on the chessboard. An action listener will be added to implement a timer whereupon the expiry of the timer will cause the chess piece to be moved to one of the valid squares on the chessboard that it is able to move to according to standard chess rules.

Using the chessboard created for Project 3 (without the chess piece images on the board), you are to initially display a blank chessboard (i.e., no pieces are initially placed on the chessboard) that will allow the user to select a chess piece, place it anywhere on the chessboard, display the squares that it can move to according to chess rules, and finally, randomly select one of these valid squares and move the chess piece to that square. Only one piece is on the chessboard at a time; there is no interaction between pieces (unless extra credit work is attempted).

You will use supplied images of the various chess pieces, along with an image of a red “X” for the valid squares to which a chess piece can move, to display on the chessboard based on the user input and implemented listeners.

The program will be organized into ten Java files. The hierarchy of chess piece classes is as follows: See image.

ChessPiece is the abstract superclass of this group of classes. Its constructor sets the instance Color variable color of the chess piece to Color.BLACK or Color.WHITE. Most of your accessor/mutator (setter or getter) methods should be found here. The ChessPiece class also contains the abstract method called moves() that returns points for all possible squares for a chess piece may move to based on its location on the chessboard. You should also consider a field called location, along with its accessor and mutator methods, to keep track of its current location on the chessboard.

PieceTypes is a class that simply contains the enum called pieces defined as follows for use by concrete classes to set the piece type when instantiating a chess piece object:

public static enum pieces { pawn , king , knight , bishop , rook , queen };

Pawn is a subclass of ChessPiece. Its constructor calls its parent constructor to set its color to either Color.BLACK or Color.WHITE and sets the piece type to pawn. White pawns move up, while black pawns move down one square only, except if they are on row 6 or 1, respectively, where they can move either one or two squares forward. The Pawn class also overrides the abstract method moves() in ChessPiece with its valid moves based on its location on the chessboard. See image.

King is a subclass of ChessPiece. Its constructor calls its parent constructor to set its color to either Color.BLACK or Color.WHITE and sets the piece type to king. Kings are able to move one square in any direction. The King class also overrides the abstract method moves() in ChessPiece with its valid moves based on its location on the chessboard. See image.

Knight is a subclass of ChessPiece. Its constructor calls its parent constructor to set its color to either Color.BLACK or Color.WHITE and sets the piece type to knight. Knights are able to move either two squares in one direction and then one square in another or vice versa. The Knight class also overrides the abstract method moves() in ChessPiece with its valid moves based on its location on the chessboard. See image.

LinePiece is an abstract subclass of ChessPiece. Its constructor calls its parent constructor to set the color of the chess piece to either Color.BLACK or Color.WHITE. The LinePIece class also contains implemented methods diagonalMoves() and squareMoves() to determine the possible diagonal or straight line moves a chess piece can make. Both of these methods returns points for all of the squares in the respective direction that a chess piece may move to based on its location on the chessboard.

Bishop is a subclass of LinePiece. Its constructor calls its parent constructor to set its color to either Color.BLACK or Color.WHITE and sets the piece type to bishop. Bishops are able to move diagonal in any direction. The Bishop class also uses the diagonalMoves() method in its overriding of the abstract method moves() in ChessPiece with its valid moves based on its location on the chessboard. See image.

Rook is a subclass of LinePiece. Its constructor calls its parent constructor to set its color to either Color.BLACK or Color.WHITE and sets the piece type to rook. Rooks are able to move in a straight line in any direction. The Rook class also uses the squareMoves() method in its overriding of the abstract method moves() in ChessPiece with its valid moves based on its location on the chessboard. See image.

Queen is a subclass of LinePiece. Its constructor calls its parent constructor to set its color to either Color.BLACK or Color.WHITE and sets the piece type to queen. Queens are able to move either in a straight line or a diagonal in any direction. The Queen class also uses both the squareMoves() and diagonalMoves() methods in its overriding of the abstract method moves() in ChessPiece with its valid moves based on its location on the chessboard. See image.

The ChessDemo class is responsible for setting up the JFrame, implementing the listeners, and drawing the chessboard in its various stages using the supplied images. Its main() method will instantiate and set up the JFrame. Its constructor should instantiate a JPanel with a GridLayout to hold the chessboard. Additionally, its constructor should instantiate and add the key, mouse, and timer listeners either the panel itself or JButtons/JLabels as applicable.

  • A private class inside ChessDemo called GridListener will implement the MouseListener interface. When the mouse is pressed inside a square on the chessboard, a user-selected chess piece will be placed on that square. Additionally, each square representing a possible move for that chess piece based on its current location will be updated with the red “X” image. The timer should also be started or restarted, as appropriate, inside this event handler.
  • A private class inside ChessDemo called PieceListener will implement the KeyListener interface. You should instantiate the appropriate chess pieces based on the character typed. Use the following to determine which chess piece to activate:
p White Pawn P Black Pawn
n White Knight N Black Knight
r White Rook R Black Rook
b White Bishop B Black Bishop
q White Queen Q Black Queen
k White King K Black King
  • All other keys typed should be handled appropriately, such as displaying an error message to the console.
  • A private class inside ChessDemo called TimerListener will implement the ActionListener interface. Upon expiry of a 5 second timer, the active chess piece on the chessboard will randomly move to one of the possible moves highlighted by the red “X”s. Be careful to manage the timer appropriately. It should only expire 5 seconds after the currently active chess piece has been placed on the chessboard with the red “X”s highlighting possible moves.

For error handling or messages to the user, you may use the console. Also note that chess pieces may be placed on the chessboard in succession (with or without selecting a new chess piece), meaning that your program should work if, for example, a white queen, black rook, black rook, and white knight are placed on the chessboard during the same program run when only 3 characters (“q”, “R”, and “n”) were typed to select a chess piece. You should also remember to clear the chessboard as applicable so that only one chess piece (including related possible moves) is active on the chessboard at a time.

Some suggestions for bonus points/extra credit on this project are, but not limited to, the following:

  • Add a check to make sure that pawns are not allowed on the back row of their respective side (black pawns not allowed on row 0 and white pawns are not allowed on row 7).
  • When a pawn reaches the opponent’s side (row 0 for white pawns or row 7 for black pawns), whether placed there manually or moved there based upon the timer expiry, display a queen chess piece instead of the pawn.
  • Don’t clear the board after each placement of a chess piece, but leave the chess piece there and add another chess piece (with applicable possible moves) that includes interaction with the existing chess piece(s), including capture(s).

If you have other ideas for suggestions, please let me know so that I confirm and validate your idea. If you do implement bonus work in this project, you must still ensure that you fulfill the requirements of this program. If you have any questions about this policy, please see me.

If you are having trouble updating (not instantiating) a particular JLabel/JButton with one of the supplied images, you can try using something similar to the following exception- based statement to set the icon:

try {
grid[row][col].setIcon( new ImageIcon(ImageIO. read ( new
File(“BPn.png”))));
} catch (IOException e1) {
e1.printStackTrace();
}

Test your program with many different scenarios to make sure that all “paths” through your code are correct. Be sure to pay special attention to scenarios that could result in exceptions, such as a null value or out of bounds value. Be sure your program is properly documented using comments where needed, especially as discussed in class in the header, for variables, and for specific blocks of code. Code that is excessively hard to read (because of lack of proper indention, comments, or otherwise) will have points deducted. If you have any questions about what is expected regarding the program requirements, please let me know.

Academic Honesty!
It is not our intention to break the school's academic policy. Projects posted are only used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly fill out the form. Please provide a valid email address and we'll get back to you in less than 24 hours. We will be sending an invoice through PayPal upon confirmation. We are a non profit organization however we need an amount to keep this organization running, and to be able to complete our research and development.