Tuesday, September 25, 2012

Chess Game Design


Here give an example about an object oriented design of a chess game.

The design can be modularized into four parts: board, piece sets, game and player.



Board

A Board class has an attribute of Squares Array (8x8) and PieceSets (black and white).

A Board class also has an attribute of "pieceSetOnTop". The attribute helps to figure the piece moves that are direction-restricted.


PieceSets

A PieceSet class has an attribute of a List<Piece>. The size of the List<Piece> is initially set to 16.

A Piece class has two attributes: color and placeAt (i.e. located at which square).

A Piece class is an abstract class. The extended classes (Pawn, King, Queen, Rook, Knight, Bishop) implements the abstracted operations:
  • validMoves() - The valid movement for a Piece
  • attackSquares() - The squares that a Piece can attack
  • captureFreeMove - The squares that a Piece can move to without being captured. 
  • toBeCaptured() - The boolean indicates whether a Piece is going to be captured.

The validMoves() operation implements the movement rules. For example, the validMoves of a Pawn class ensures that the Pawn can only move in the direction towards the opponent side. A Pawn class has additional attributes of promoted and promotedTo, which describes the movement/conversion rule of a Pawn at reaching the end of an opponent side and at the conversion about the piece that a Pawn converted to.

Game

A Game class controls the flow of a game. The class has attributes:
  • playedMoves - Keep a record of moves
  • turn - Indicate either it is a Black's turn or a White's turn
  • players - Represent the two players, this can be Human/Human, Computer/Computer or Human/Computer
  • result - Indicate the result of a game
  • checkStatus - Indicate which side is being checked or checkmated

Player

A Player class represents a Player. A Player has two attributes:
  • pieceColor - The color that used by a Player
  • engine - The engine that makes the moves. This can be a human or a computer


6 comments:

  1. I liked the way you designed but i have few questions ?

    1. didn't get use of pieceSetOnTop as u explained , please brief more if possible ?
    2. enum of piecetype is okk but where it is used ?
    3.enum MoveDirection is okk but pawn can never move backward ? also where is this enum is used ?
    4.what is exact use of capturePiece in Move ?

    Thanks for your effort.
    Shashank

    ReplyDelete
    Replies
    1. 1. The attribute "pieceSetOnTop" has a value of either "Black" or "White". The Black value means the Black PieceSet is located in the top two rows while the White PieceSet is located in the bottom two rows.

      2. PieceType is an attribute of Piece that determines the type of Piece. The attribute can be used anywhere that is appropriate.

      3. The Board.pieceOnTop attribute is used to define the MoveDirection of a Pawn. When the Board.pieceOnTop is black, the possible moves of black Pawn's are moving downward. The moveDirection of a Pawn is properly defined to ensure the move is legitmate.

      4. The capturePiece in Move indicates a possible opponent piece captured after the move.

      Delete
  2. I liked the image you have displayed, and I have used it as is. Please refer my blog here.Thanks.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Whys is the pieceType enum required if we can have the specific Piece classes defined (e.g King)?

    ReplyDelete