Chess Bin

Computer Chess Information and Resources

Chess Piece Representation

The first 2 tasks in creating a chess engine are the description of the Chess Board and the Chess Pieces.  This page I will discuss my C# representation of the Chess Piece. 

Throughout the chess engine many decisions will have to be made based on 2 concepts.  The chess piece type and it’s color.  For this purpose I have declared two enumerated types, chess piece color and chess piece type:

public enum ChessPieceColor
{
    White,
    Black
}

public enum ChessPieceType
{
    King,
    Queen,
    Rook,
    Bishop,
    Knight,
    Pawn,
    None
}

Originally these two enumerated types were located inside the Chess Piece class, however I later realized that these constructs must be available to assemblies outside of the chess engine, and therefore must be made public.  Since I found that internal classes perform faster than public classes, the only way to make my Chess Piece class internal is to remove the enumerated types out of the Chess Piece class.

The next concept that will be required is the idea of a Chess Board position.  This will be needed in order to list valid moves for a chess piece inside the Chess Piece class.  In the current version of my chess engine, board position coordinates are stored as a single byte, 0 for A8 and 63 for A1.

Originally I stored chess board positions as a set of two bytes.  The first byte was used to represent the column and second byte for the row.  This made my chess engine easier to understand but cost me approximately 30% more in performance.

The class representing my Chess Pieces will be declared as internal sealed:

internal sealed class Piece

During my performance testing I found that the sealed and internal keywords improved speed significantly.  I also found that private methods and objects perform much faster so I strongly suggest using the private keyword as much as possible. 

To make descriptions of my chess pieces easier and more strongly typed, I will be using the above defined enumerated types as representations of the chess piece color and type.


internal ChessPieceColor PieceColor;
internal ChessPieceType PieceType;

The next line of code will describe the piece value used in the evaluation of positions.  Obviously each piece type will have a different value; a Queen is worth more than a pawn etc.

internal short PieceValue;
In order to keep track the value of pieces that are currently attacking and defending this chess piece we will declare the following 2 variables:


internal short AttackedValue;
internal short DefendedValue;

Each piece will have what I call the Piece Action Value, this is the value added or subtracted from the score when the chess piece is either attacking or defending another chess piece.  Different chess piece types will have different action values.  This follows the logic that it is better to risk a pawn than it is to risk a queen.

internal short PieceActionValue;

The next line describes if the chess piece is currently selected on the board by the user.  This will help later when coding a graphical user interface.


internal bool Selected;
The following variable desribes if our chess piece has been moved.  This will be very usefull in the evaluation function where we can give penalties or bonueses if the chess piece has not yet made a move.


internal bool Moved;

Each piece will also contain a list of valid moves, or board positions that the piece can move too. 


internal Stack<byte> ValidMoves;

Constructors 

There are 2 constructors in the Chess Piece class

Copy Constructor, which is needed during move generation.

internal Piece(Piece piece)
{
    PieceColor = piece.PieceColor;
    PieceType = piece.PieceType;
    Moved = piece.Moved;
    PieceValue = piece.PieceValue;
   
    if (piece.ValidMoves != null)
        LastValidMoveCount = piece.ValidMoves.Count;                     
}

Constructor used to initiate the chess board with chess pieces.

internal Piece(ChessPieceType chessPiece, ChessPieceColor chessPieceColor)
{
    PieceType = chessPiece;
    PieceColor = chessPieceColor;

    ValidMoves = new Stack<byte>();

    PieceValue = CalculatePieceValue(PieceType);
}


Methods

Calculate Piece Value and is used during object construction to calculate and record the chess piece’s value.

private static short CalculatePieceValue(ChessPieceType pieceType)
{
    switch (pieceType)
    {
        case ChessPieceType.Pawn:
            {
                return 100;
               
            }
        case ChessPieceType.Knight:
            {
                return 320;
            }
        case ChessPieceType.Bishop:
            {
                return 325;
            }
        case ChessPieceType.Rook:
            {
                return 500;
            }

        case ChessPieceType.Queen:
            {
                return 975;
            }

        case ChessPieceType.King:
            {
                return 32767;
            }
        default:
            {
                return 0;
            }
    }
}

The Piece Action value, the added or subtracted from the score when the chess piece is either attacking or defending another chess piece is also calculated on construction, using the following method:

private static short CalculatePieceActionValue(ChessPieceType pieceType)
{
    switch (pieceType)
    {
        case ChessPieceType.Pawn:
            {
                return 6;
            }
        case ChessPieceType.Knight:
            {
                return 3;
            }
        case ChessPieceType.Bishop:
            {
                return 3;
            }
        case ChessPieceType.Rook:
            {
                return 2;
            }
        case ChessPieceType.Queen:
            {
                return 1;
            }
        case ChessPieceType.King:
            {
                return 1;
            }
        default:
            {
                return 0;
            }
    }
}

Note that the above method is designed to encourage the computer to protect and attack using the lower valued pieces first. 

That is the end of the chess piece class.  If you are interested in starting your own chess engine try my free chess engine starter kit that contains all the code necessary for you to start your own chess game.

Next post we’ll look at the chess board square and chess board representation.