The Backbone Class ~~> Board Class

BOARD :

The board is a simply represented by an array of Spots.

 The Spot class performs the basic operations of changing the locations of the pieces through the functions occupy() and vacate(). It also has a couple of other methods like checking if it isOccupied()
etc.

This brings us to another important class .. Location. I had built this program not for commercial usage and so playing excellent was not a criteria(although it can beat a fritz at 1700-1850 rating ).
The location class encapsulates the row and column position of each piece and also helps in easier manipulation as it has a couple of in-built methods.

Some of the important methods of the board class are:-
  •  void addPiece(Piece piece,Location l); -- this method adds a piece to the board
  •  void executeMove(Move move); -- makes a move.This move further calls two other methods   a) void executeIndividualMove(Move move)                                                                                b)   void executeEnPassentMove(Move move)                                                                          Note :This mystery will be solved in the next blog post.The undoMove() counterparts are also        present.
  •  LinkedList<Piece> getPieces(PieceColor color); -- returns the list of all pieces of specified color
  •  Move getLastMove(); returns the last move that was executed
  •  boolean isCheckMate(PieceColor color)
  •  boolean isStaleMate(PieceColor color)
  •  boolean isKingInCheck(PieceColor color)
  • LinkedList<Move> getPossibleMoves(PieceColor color,boolean... arr);
  • LinkedList<Move> trimIllegalMoves(LinkedList<Move> lst)



Let me now explain a couple of classes so that the methods and there working becomes clear.
The first class will be the abstract class Piece.It implements the interface Movable which has mainly 2 methods :
   

    a)  LinkedList<Move> getMoveList(Board board)
    b) boolean isLegalMove(Location location)

The first method generates the move list of all possible moves FOR THAT PIECE.
The second method checks whether THAT PIECE can go to the specified location.

Both of these methods use other methods of the board class(mostly getter setter methods) that you can see in the source code for yourself.

These methods have been implemented by the subclasses of the Piece class.

Now let me clarify the workings of the methods mentioned earlier.
Firstly, how to get all the possible moves.
For that ,I iterate over the piece list and invoke the getMoveList()   method and add all the moves to a list.
Then , I pass this list to the trimIllegalMoves() method.This method now executes all the moves and checks whether in any of the position , isKingInCheck(), which  invokes the isLegalMove() method on all the opponent pieces , passing the Location of the king as the parameter.This is how a list of legal moves is obtained. Agreed, this is a bit time consuming, but it is highly readable and after all I am not going to a chess engine competition where my code has to be highly efficient. When I wrote it a year back, in class 8 , I did not have any idea of complexity classes or algorithm design and data structures.However , even with all this, the engine can analyse 100K positions per second at the least.

The methods isCheckMate() and  isStaleMate() are quite simple and also use the isKingInCheck() method.

The other methods in the Board class are pretty small, although numerous.
Most of them are getter setter or simple boolean  methods.

Thus the board class is the backbone of the framework of the chess engine.




Comments

Popular posts from this blog

Small-to-Large

Segment Tree Beats - An introduction

Getting prepared for RMO