Writing a BitBoard in Rust Pt. 2: The Game State
In the last blog post we created BitBoard
s and stored them inside a Position
.
To evaluate a position we need more than the positioning of the pieces sometimes though. En-passant is a good example
of this:
In this position capturing the pawn on g5 with En Passant is the best move if available since it allows us to promote a
pawn. Just looking at the board though we cannot be sure that en passant is a legal move.
Explanation⌗
So we need additional information in order to evaluate the position. This will be stored in the struct called State
,
which contains all necessary information to evaluate a position outside the pieces positions.
So it contains the:
- Castling rights
- Half move counter
- Side to move
- En passant(if possible)
An implementation can look like this:
In order to define castling rights efficiently we do it similar to Stockfish. We define an u8 to store the castling rights. The first 4 bits are unused and the latter ones are used to store one castling right.
And a helper struct to more easily use CastlingRights
, which I put in my defs.rs:
And we also need a way to represent squares in order to use en_passant_square, which we can nicely do with an enum in Rust:
And lastly we need to add the State
to the Position
:
Credits and further reading⌗
Many articles and projects have helped me in the writing of this Blog Post, notably: