-
Notifications
You must be signed in to change notification settings - Fork 0
Pawn moves generation
This page contains descriptions of the general logic behind finding possible moves for pawns using bitboards.
- Understanding Bit-shifting - https://www.interviewcake.com/concept/java/bit-shift
- Understanding bitwise operations
- Understanding bitboards and the board representation used in this project - See board representation wiki
A white pawn has 4 possible moves that it can make initially:
2
3 1 4
P
It can move 1 or 2 steps forward as well as an attack on the diagonal left or right. Moving forward requires there to be no opponent or own pieces in front of it. If the pawn has already moved once, it can no longer move two squares forward per move. Attacking on the diagonal requires that an opponent piece is placed on that diagonal.
It can be summed up to these 4 moves:
- Single-push
- Double-push
- Diagonal left attack
- Diagonal right attack
In the pawn-bitboard below, we want to simulate all its possible moves. We do so by setting the bit at every possible move position from the pawns current position.
| White pawn bitboard (Current) |
|---|
|
Since there are 4 possible pawn moves these moves can basically be generated with 4 bitshift operations.
To generate the pawn moving one step forward, we can bitshift the pawn bitboard 8 to the left. (Pawn-bitboard << 8)
We also need to make sure there are currently no other pieces blocking this square. - (Pawn-bitboard << 8) & empty-squares
| (Pawn-bitboard << 8) | empty-squares | Single-push | ||
|---|---|---|---|---|
|
& |
|
= |
|
Generating double-push is not as simple as bit-shifting by 16. If we only bitshift 16, we do not account for a blocking piece on the square right in front. Therefore the possibility of moving two steps is dependent on if single-push is possible. If so we can bitshift an additional 8 bits to the left (single-push << 8).
Double-move is only possible if the pawn is located on its initial square. To account for this we can check if the result of single-push is on rank 3 ((single-push & rank3) << 8).
To also account for possible blocking pieces we use bit-wise on empty-squares ((Single-push & Rank-3) << 8) & Empty-squares.
| Single-push | Rank3 | (Single-push & Rank3) | ||
|---|---|---|---|---|
|
& |
|
= |
|
| ((Single-push & Rank3) << 8) | Empty-squares | Double-push | ||
|---|---|---|---|---|
|
& |
|
= |
|
Generating left diagonal attack is very similar to single push. Instead of bitshifting 8 to the left, we instead bitshift by 9. (Pawn-bitboard << 9) This simmulates the piece has moved one square up and left. Then we also need to take into account if an opponent piece is already there. ((Pawn-bitboard << 9)& Opponent-pieces) We cannot attack if there is no opponent piece.
| White pawn bitboard (Current) | Opponent-pieces (Current) |
|---|---|
|
|
| (Pawn bitboard << 9) | Opponent-pieces | ((Pawn Bitboard << 9) & Opponent-pieces) | ||
|---|---|---|---|---|
|
& |
|
= |
|
| (Pawn bitboard << 9) | not-H-file | Opponent pieces | Left diagonal attacks | |||
|---|---|---|---|---|---|---|
|
& |
|
& |
|
= |
|
Diagonal right is very similar to diagonal left. The only difference is that here we bitshift by 7 (Pawn bitboard << 7). We also have to be aware of pawns on the H file this time. A pawn on the H file has no diagonal to its right, we therefore have to make use of a not-A-file bitboard similar to not-H-file bitboard used above. We then end up with the following operation ((Pawn bitboard << 9) & not-A-file & Opponent pieces)
| (Pawn bitboard << 7) | not-A-file | Opponent pieces | Right diagonal attacks | |||
|---|---|---|---|---|---|---|
|
& |
|
& |
|
= |
|
TODO
Now we've calculated all possible pawn moves. We can now use the bitwise or operator to combine all moves into one bitboard: For black pawns the same rules apply, its just the opposite. If you want to simulate moving one piece, you instead have bitshift 8 to the right and so on.
| Single push | Double push | Left diagonal attacks | Right diagonal attacks | White pawn moves | ||||
|---|---|---|---|---|---|---|---|---|
|
| |
|
| |
|
| |
|
= |
|