[CHAPTER 02 // crates/core · engine, SPEC 001 (R2–R4)] Implemented in engine code

Pin Masks, Check Evasion & En-Passant Legality

Exact legal move generation using king-ray pin masks, check evasion filters, and post-removal en-passant king-exposure checks.

Why does BalouChess do this?

Generating pseudo-legal moves and testing every single one by playing it on the board wastes precious cycles on clearly illegal moves when the king is safe, yet cutting corners risks catastrophic illegal moves in subtle tactical edge cases.

BalouChess computes check masks and ray-aligned pin masks around the side-to-move king. A pin mask lets the generator accept moves of unpinned pieces without a further test; moves of pinned pieces are checked one by one for king safety. Special king-exposure cases — file and diagonal pins as well as horizontal en-passant captures where two pawns vanish from the fifth rank — are verified explicitly.

[X-RAY CARD 01 // BITBOARD & PIN INSPECTOR]

Live 64-Bit Ray & En-Passant Exposure Inspector

Active 64-bit mask (7 bits) 0x1010101010101000
ILLEGAL MOVE (e5d6): Capturing on d6 vacates e5 and exposes Ke1 vertically along the e-file to Re8.

FEN: k3r3/8/8/3pP3/8/8/8/4K3 w - d6 0 1

Click or use arrow keys + Space/Enter on any board square to toggle bits in Custom mode.

Data Layout, Invariants & Common Pitfalls

Memory & State Layout

For a given Position, king danger analysis (KingSafety) computes checker bitboards, between-ray masks for single-check block/capture evasions, orthogonal/diagonal pin rays for friendly blockers, and dedicated post-removal occupancy masks for king moves, standard castling paths (rook-file Chess960 castling is specified in engine, SPEC 001/R15 but not built yet), and en-passant captures.

Formal Invariants

  • Exact legality contract (engine, SPEC 001/R2): legal_moves returns all and only FIDE-legal moves — zero missing legal evasions and zero illegal moves under single check, double check, or absolute pins.
  • Arbitrary 16-bit probe safety (engine, SPEC 001/R16): Position::is_legal(mv) accepts any raw 16-bit value (such as a collided transposition-table move) and returns true if and only if mv is legal in the current position.

Typical Pitfalls & Edge Cases

  • En-passant vertical ray pins and horizontal double-vacancy pins (engine, SPEC 001/R4): in regression position W-0001 (k3r3/8/8/3pP3/8/8/8/4K3 w - d6 0 1), e5xd6 vacates the e-file and exposes Ke1 vertically to Re8; in k7/8/8/r4pPK/8/8/8/8 w - f6 0 1, g5xf6 removes both f5 and g5 from the fifth rank simultaneously, uncovering a horizontal rook attack (Ra5–Kh5) that single-blocker pin masks miss unless occupancy is tested with both pawns removed.
  • Castling through attacked squares vs rook clearance: in the standard-chess rules implemented today (engine, SPEC 001/R3; the extension to arbitrary Chess960 rook files is specified in engine, SPEC 001/R15 but not built yet), the king may never start in, pass through, or land on an attacked square, whereas the rook may cross an attacked square when the path is vacant.