Computer Chess Information and Resources

Originally the code in this post was part of the Chess Piece Motion class. However since I posted the ordinal code I have divided that class into 2 separate classes. Chess Piece Moves and Chess Piece Valid moves which is discussed here.

This class will be responsible for dynamically figuring out only the valid moves for the current chess board and assigning only the valid moves to each chess piece. This class will also figure out what pieces are attacking each other, is the king in check, has en passant occurred and assign the information to each piece for the purpose of score evaluation.

The Chess Piece Valid Moves class will be declared as follows:

internalstaticclass PieceValidMoves

To help us understand what is board squares are being attacked we will defina 2 arrays. One for storing board squares attacked by White, and one for Black. These arrays are crucial in figuring out things like valid moves for a king, since kings cannot move onto a square that is currently attacked by an opponent.

Furthermore we can't correctly check for the kings valid moves until we examine all other chess pieces. This is due to the fact that we won't know if the chess board square is attacked if we don't look at every single chess piece first. For this reason when we come across a king during our analysis, we don't analyze its possible moves but rather store its position for later analysis. The following 2 variables are used to store that information.

The Analyze Move method will perform a deep analysis or examination of the move itself and its effect on the chess board. For example this method will record an En Passant scenario as well as recording Checks and Kills. The Analyze Move method will return true if the move is considered not blocked (not resulting in a kill or blocked by another chess piece of the same color). Similarly it will return false if the move is blocked and movement cannot continue past this position. This is very important since this return value will be used to end a loop of moves in a certain direction. This method is called for all chess pieces other than pawns and kings.

Pawns behave slightly differently then regular pieces in that not all of their moves can result in a kill. A move straight ahead cannon result in a kill while a diagonal move can. For this reason there are two separate methods to analyze pawn moves. One Parent that loops through all available pawn moves and one child that analyzes each move at a time.

//If there no piece there I can potentialy killif (pcAttacked == null)return;

//Regardless of what is there I am attacking this squareif (pcMoving.PieceColor == ChessPieceColor.White)
{
WhiteAttackBoard[dstPos] = true;

//if that piece is the same colorif (pcAttacked.PieceColor == pcMoving.PieceColor)
{
pcAttacked.DefendedValue += pcMoving.PieceActionValue;return;
}//else piece is different so we are attacking
pcAttacked.AttackedValue += pcMoving.PieceActionValue;

//If this is a king set it in check if (pcAttacked.PieceType == ChessPieceType.King)
{
board.BlackCheck = true;
}else
{//Add this as a valid move
pcMoving.ValidMoves.Push(dstPos);
}
}else
{
BlackAttackBoard[dstPos] = true;

//if that piece is the same colorif (pcAttacked.PieceColor == pcMoving.PieceColor)
{return;
}

//If this is a king set it in check if (pcAttacked.PieceType == ChessPieceType.King)
{
board.WhiteCheck = true;
}else
{//Add this as a valid move
pcMoving.ValidMoves.Push(dstPos);
}
}

return;
}

Check Valid Moves King Castle Method handles the complicated castling scenarios by examining the chess board squares between the king and the rook to make sure they are empty, not attacked and that the rook and king are both in their starting positions.

//There are two ways to castle, scenario 1:if (board.Squares[7].Piece != null)
{//Check if the Right Rook is still in the correct positionif (board.Squares[7].Piece.PieceType == ChessPieceType.Rook
&& !board.Squares[7].Piece.Moved)
{if (board.Squares[7].Piece.PieceColor == king.PieceColor)
{//Move one column to right see if its empty

The last method in this class is the only non private method in this listing. The Generate Valid Moves method will be called directly by the chess engine to create valid moves for each chess board it examines. This method will loop through all chess pieces on the chess board and examine each possible move based on the chess piece’s position. Please note that we have already calculated each pieces move in the Chess Piece Moves class for every single position on the chess board. We now just have to figure out which one of those moves is valid and what their results will be. While we are looping through all of the chess pieces we also collect some information such as how many pawns are in each file, how many pawns are isolated. At the end of this method we will sum up this information and store it on the Chess Board.

//Now that all the pieces were examined we know if the king is in check
GenerateValidMovesKingCastle(board, board.Squares[WhiteKingPosition].Piece);
GenerateValidMovesKingCastle(board, board.Squares[BlackKingPosition].Piece);
}

This post will discuss Piece Moves class. This class is responsible for providing all available chess piece moves regardless of the state of the chess board. The information stored in this class will not change throughout the game play so it is static and calculated only once before the game starts. Having a set of possible moves for any chess piece at any position allows us to later to generate only the valid moves for each chess piece based on the current state of the board.

The Chess Piece Moves listing will contain a Valid Move Set struct. This struct will be used to store a set of moves available from a single position.

internalstruct PieceMoveSet
{internalreadonly List<byte> Moves;

internal PieceMoveSet(List<byte> moves)
{
Moves = moves;
}
}

Furthermore we will need some additional array to store all the above move sets for every position on the board.

For example KnightMoves[0]. Moves will return a Knight Moves available from position 0 or A8. KnightMoves[63] will return all of the possible moves for position 63 or H1.

Some chess pieces can move in a single direction for an undefined number of squares until they reach the end of the board or another chess piece. For this purpose moves sets for some pieces are divided into several arrays, each describing a move in a certain direction. This makes it easier to manage these movements in the Chess Piece Valid Moves Class by having the ability to loop through each array until a chess piece or the end of the board is reached and no further.

One other explanation is required around the Total Moves arrays. Example there is an array called KnightTotalMoves. This array will hold the number of moves available for every position on the chess board. This is a performance related addition as it allows me to replace all my foreach loops with regular for loops. It’s a small performance gain (1%-2%) but they all add up.

To make use of the above structs we will declare a static class called Piece Moves:

internal static class PieceMoves

To make life a bit easier, we will add a helper method called Position. This method will accept a chess board column and row, and return a single byte representing the chess board position. Usually we would not want to use a method like this because it will slow things down. However this method is only used when the Chess Engine starts when super fast performance is not really all that necessary.

Initiate Chess Piece Motion Class is called only once in the Chess Engine Constructor. It will construct all of the arrays and call the methods responsible for populating the Move Set arrays will all of the moves for each position on the board for each chess piece.

Set Moves methods are responsible for populating the Move Arrays with the moves available for each chess piece from a given position. I am not going to explain this much further. These methods basically add some predetermined positions to the arrays defined above. Again performance is not really all that key here, since these methods run only once when the Chess Engine starts.

This concludes the Chess Piece Moves class. To continue reading the chess piece motion series follow this link to the Chess Piece Valid Move class, that will discuss how my chess engine generates valid moves based on the current state of the chess board.