King safety and Attack Detection
The Attack Map
The AttackMap module answers the question: “Is square X under attack by color Y?”
It is used for:
- Castling: Ensuring the King does not pass through check.
- King Moves: Ensuring the King does not step into check.
- Check Detection: Checking if the current player’s King is under attack.
Legality Filtering
The Legality module transforms pseudo-legal moves into fully legal moves.
Algorithm (The “Make-Unmake” Test)
For each pseudo-legal move:
- Apply: Temporarily apply the move to the board.
- Scan: Run
isSquareAttackedon the player’s King’s square. - Validate:
- If the King is attacked AFTER the move, it is ILLEGAL (pinned piece or King walking into check).
- If the King is safe, the move is LEGAL.
- Revert: Undo the move to restore the original board state.
4-Player Complexity
In 4-player games, a King is in check if ANY of the other three colors can capture it. isSquareAttacked must be called for all three opponent colors to confirm total King safety.