Monday, 29 October 2018

Update Chess Game

The following is a revisit of the old assignment from MMU

Introduction

The purpose of this to update the old assignment, it's a good practice for me to practice my code discipline and learn new code paradigm. The update is depend on my experience, things I learn along the way.
The aim Chess Game assignment is still the same, is to create a chess application without use any library. The following is the few example of explicit and implicit rule :

Explicit rules

  • Apply design pattern
  • It require to display the Chess Board
  • Each type will have different move list
  • Each player will have their own Chess Piece set
  • Each player will have their own turns
  • If player able to pin opponent Star chess piece it is a game over.

Implicit rules

  • Player unable to move a chess piece if he/she click outside the board or chess box not belong to the chess piece move list
  • If player press chess box not belong to any chess piece, his/her will not end the turn

UML


Coordinate

Since many classes use 2 point as coordination reference (x-axis & y-axis), therefore I create a Coordinate to hold both value.

Class Summary

CPDatabase The class is singleton, to prevent duplicate object and only have a one instance. addChessPiece function is to add a chess piece into the m_list, the purpose of this class to allow check all active chess piece on the board instead checking each chess box.

ClassBox

Chess box is using extend of JButton class, this allow to use all their method and added extra new method. In brief, the chess box have the additional action listener. The different is where if the chess box is empty or not. If the chess piece is included inside the chess box. It will react a different from the empty chess box, the react will be different between each chess piece. m_position is to hold the chess box position

ChessBoard

This class will be handling the chess board. The size is will be refer on [ROW] x [COLUMN] size enum. The class itself it is a singleton and will be returning a JPanel component. The component will be holding all the chess boxes. m_movelist will be holding all possible way the chess move will be move, the m_movelist is will be refer on the chess piece it self. By doing this allow, the board all care on the possibility of the chess piece action only. The chess box will be change color(RED) dependent on the m_movelist, this all to reset chess box color more efficient instead recolor it one by one. ChessBoard will be act as View component since it only responsible for the GUI.

ChessPiece

For this class I use a builder. Inside the builder it require an owner. The chess piece is can add a m_chessname and able to add multiple movelist by using addMovelist. If addVerticalMove() and addHorivontalMove() is set true, on the each turn will self check it self coordinate and calculate the possibility of movelist.

Example



The slightly thick red border is where chess piece located.

ChessPlayer

ChessPlayer only hold m_name and m_status. m_status hold Status that can be start,moving or end.

Main

The following is the initialize for the board.
to keep everything neat and simple, my design is to require to pass through the ChessBoard. After chesspiece or player it require to be initialize inside the chessboard.

To can be added

The following is the possibility for the next iteration:
  • Since the chessboard only return GUI component we able to added more GUI.
  • We can have a GUI list of all chess piece since we provide CPDatabase.
  • In ChessPiece we can add a diagonal, similar how horizontal and vertical process

Download

Chess Game with source code
Chess Game Jar