|
| 1 | ++++ |
| 2 | +title = "Chess Engine Pawns" |
| 3 | +date = 2026-01-10 |
| 4 | +updated= 2026-05-31 |
| 5 | ++++ |
| 6 | + |
| 7 | +Pawn move generation using bitboards. For pawn pushes (moving pawn forward), we |
| 8 | +can implement this by shifting the bitboard up (for white pawns), and then |
| 9 | +taking the intersection of the empty board (to ensure no piece is blocking the |
| 10 | +push). Note that pawns in their starting position can move up two squares. |
| 11 | + |
| 12 | +```c |
| 13 | +U64 wSinglePushTargets(U64 wpawns, U64 empty) { |
| 14 | + return nortOne(wpawns) & empty; |
| 15 | +} |
| 16 | + |
| 17 | +U64 wDblPushTargets(U64 wpawns, U64 empty) { |
| 18 | + const U64 rank4 = C64(0x00000000FF000000); |
| 19 | + U64 singlePushs = wSinglePushTargets(wpawns, empty); |
| 20 | + // if a pawn is not in the fourth rank after double pushing, |
| 21 | + // then it is not in its starting position and therefore |
| 22 | + // not allowed to move two squares |
| 23 | + return nortOne(singlePushs) & empty & rank4; |
| 24 | +} |
| 25 | + |
| 26 | +U64 bSinglePushTargets(U64 bpawns, U64 empty) { |
| 27 | + return soutOne(bpawns) & empty; |
| 28 | +} |
| 29 | + |
| 30 | +U64 bDoublePushTargets(U64 bpawns, U64 empty) { |
| 31 | + const U64 rank5 = C64(0x000000FF00000000); |
| 32 | + U64 singlePushs = bSinglePushTargets(bpawns, empty); |
| 33 | + return soutOne(singlePushs) & empty & rank5; |
| 34 | +} |
| 35 | +``` |
| 36 | +
|
| 37 | +We use separate implementations for optimization. To check beforehand if a pawn |
| 38 | +is allowed to push, shift the empty board in the reverse direction, and if the |
| 39 | +pawn is there, that means it can push |
| 40 | +
|
| 41 | +```c |
| 42 | +U64 wPawnsAble2Push(U64 wpawns, U64 empty) { |
| 43 | + return soutOne(empty) & wpawns; |
| 44 | +} |
| 45 | +
|
| 46 | +U64 wPawnsAble2DblPush(U64 wpawns, U64 empty) { |
| 47 | + const U64 rank4 = C64(0x00000000FF000000); |
| 48 | + U64 emptyRank3 = soutOne(empty & rank4) & empty; |
| 49 | + return wPawnsAble2Push(wpawns, emptyRank3); |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +For attacks, since pawns attack diagonally we can shift the bitboards of the |
| 54 | +pawns diagonally one square |
| 55 | + |
| 56 | +```c |
| 57 | +U64 wPawnEastAttacks(U64 wpawns) {return noEaOne(wpawns);} |
| 58 | +U64 wPawnWestAttacks(U64 wpawns) {return noWeOne(wpawns);} |
| 59 | + |
| 60 | +U64 bPawnEastAttacks(U64 bpawns) {return soEaOne(bpawns);} |
| 61 | +U64 bPawnWestAttacks(U64 bpawns) {return soWeOne(bpawns);} |
| 62 | + |
| 63 | +U64 wPawnAnyAttacks(U64 wpawns) { |
| 64 | + return wPawnEastAttacks(wpawns) | wPawnWestAttacks(wpawns); |
| 65 | +} |
| 66 | + |
| 67 | +U64 wPawnDblAttacks(U64 wpawns) { |
| 68 | + return wPawnEastAttacks(wpawns) & wPawnWestAttacks(wpawns); |
| 69 | +} |
| 70 | + |
| 71 | +U64 wPawnSingleAttacks(U64 wpawns) { |
| 72 | + return wPawnEastAttacks(wpawns) ^ wPawnWestAttacks(wpawns); |
| 73 | +} |
| 74 | +``` |
| 75 | +
|
| 76 | +For efficiency, we precomputed attacks from a given position into an array, and |
| 77 | +then by intersecting that with the bitboard of black pieces we can determine if |
| 78 | +a capture is possible, like the following |
| 79 | +
|
| 80 | +```c |
| 81 | +whitePawnAttacks = arrPawnAttacks[white][sqOfWhitePawn]; |
| 82 | +if (whitePawnAttacks & pieceBB[black]) -> pseudo legal captures possible |
| 83 | +``` |
| 84 | + |
| 85 | +En passant is kind of difficult to implement. Note that a pawn can only be captured |
| 86 | +via en passant if if double-moved, and if there are enemy pawns next to it. |
0 commit comments