Starting the chess engine

Bringing it all together, starting the chess engine

This post will bring all of the previous sections together in the discussion of the chess engine class. At this time I will assume that you have already read the previous sections related to Chess Board Square, Chess Board and Chess Piece Representation as well as the Chess Piece Moves and Chess Piece Valid Moves. Today I will not provide a complete chess engine listing because we have not yet discussed move searching and Chess Board Evaluation. However at the end of this section we will have a basic chess engine that can:

  1. Track chess piece locations on the chess board.
  2. Provide a list of valid moves for each chess piece, including en passant and castling.
  3. Track whose move it is.
  4. Track move history.
  5. Setup a starting chess board.

This in theory once we create a graphical user interface this skeleton chess engine would allow you to play a two human player chess game.

Chess Engine class is declared as public sealed

public sealed class Engine

Chess Engine class will contain 3 members that will represent the current chess board, previous chess board whose move it currently is. Previous Chess Board will store the last chess board prior to the last move. Please notice that the Previous Chess Board member will potentially give us easy undo functionality.

internal Board ChessBoard;
internal Board PreviousChessBoard;

public ChessPieceColor WhoseMove
{
    get { return ChessBoard.WhoseMove; }
    set { ChessBoard.WhoseMove = value; }
}

The constructor is a bit complicated as it performs the following actions:

  • Instantiate above members and set the initial move to White
  • Initiate Chess Piece Motion (Pre-calculate all possible moves for all pieces on all chess board squares possible)
  • Assign Chess pieces to the chess board in the starting position of a standard chess game. •
  • Generate valid moves for the chess pieces in their current positions.
public Engine()
{
    ChessBoard = new Board();
    MoveHistory = new Stack<MoveContent>();
    RegisterStartingBoard();
    ChessBoard.WhoseMove = ChessPieceColor.White;    
    
    ChessPieceMoves.InitiateChessPieceMotion(); 
    PieceValidMoves.GenerateValidMoves(ChessBoard);
}

Notice the Constructor uses a method called Register Starting Board. This method constructs all the chess pieces necessary for the starting chess board and registers them with the chess board object. In the above code a helper method was used called Register Piece. This method assigns the created chess piece to the desired location on the chess board.

private void RegisterPiece(byte boardColumn, byte boardRow, ChessPiece Piece)
{
    byte position = (byte)(boardColumn + (boardRow * 8));    
    ChessBoard.Squares[position].Piece = Piece;

    return;
}

The remaining method that I will indroduce today is the MovePiece method. This code will allow you to move chess pieces around the chess board. The method will return true if the move was successful and false if the move was not valid.

public bool MovePiece(byte sourceColumn, byte sourceRow, 
         byte destinationColumn, byte destinationRow)
{
 byte srcPosition = (byte)(sourceColumn + (sourceRow * 8));
 byte dstPosition = (byte)(destinationColumn + (destinationRow * 8));
 Piece Piece = ChessBoard.Squares[srcPosition].Piece;

 PreviousChessBoard = new Board(ChessBoard);
 
 Board.MovePiece(ChessBoard, srcPosition, dstPosition, PromoteToPieceType);

 PieceValidMoves.GenerateValidMoves(ChessBoard);
 
 //If there is a check in place, check if this is still true;
 if (Piece.PieceColor == ChessPieceColor.White)
 {
  if (ChessBoard.WhiteCheck)
  {
   //Invalid Move
   ChessBoard = new Board(PreviousChessBoard);
   PieceValidMoves.GenerateValidMoves(ChessBoard);
   return false;
  }
 }
 else if (Piece.PieceColor == ChessPieceColor.Black)
 {
  if (ChessBoard.BlackCheck)
  {
   //Invalid Move
   ChessBoard = new Board(PreviousChessBoard);
   PieceValidMoves.GenerateValidMoves(ChessBoard);
   return false;
  }
 }

 MoveHistory.Push(ChessBoard.LastMove);

 return true;
}

Generating a Starting Chess Position

At this point we it would be nice if we were able to add some chess pieces to our chess board. Originally I wrote a method that would declare 32 chess pieces and assign them to the correct chess board square. However eventually I wanted to implement FEN notation into my chess engine. FEN notation is an easy way to describe chess board positions. It is somewhat of a standard in computer chess circles. Hence once I implemented a method that can read a FEN string and setup the chess board based on the FEN string values, I had an easy way to create my starting chess position.

ChessBoard = new Board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");

The full source code for the FEN methods can be found on the FEN Section.

To summarize, our chess engine class contains the current chess board (Board ChessBoard) as well as a copy of the chess board as it looked prior to the last move (Board PreviousChessBoard). The Chess Engine knows whose move it currently is (ChessPiece.ChessPieceColor WhoseMove). The constructor of the Chess Engine creates all the chess pieces required for a standard chess game and registers them with the chess board using the Register Piece method. The chess engine constructor will also Initiate Chess Piece Motion and Assign valid moves to each chess piece based on the pieces current position and the board layout. Moving chess pieces around the board is handled by the MovePiece method.

Want to skip to the end, download the full chess engine source code on GitHub