0
0
LLDsystem_design~7 mins

Special moves (castling, en passant) in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
Without special handling, chess game logic cannot correctly enforce or recognize unique moves like castling and en passant. This leads to incorrect game states, illegal moves being allowed, and a poor player experience.
Solution
Implement explicit rules and state tracking for special moves within the chess game logic. For castling, verify conditions such as unmoved king and rook, empty spaces, and no check along the path. For en passant, track the opponent's last pawn move and allow capture only immediately after. This ensures the game enforces these rare but critical moves correctly.
Architecture
┌───────────────┐       ┌───────────────┐       ┌───────────────┐
│ Player Move   │──────▶│ Move Validator│──────▶│ Game State    │
│ Input         │       │ (includes     │       │ (tracks board,│
│               │       │ special moves)│       │ move history) │
└───────────────┘       └───────────────┘       └───────────────┘
                                │                       ▲
                                │                       │
                                └───────────────────────┘

This diagram shows the flow from player input through move validation including special moves, updating the game state with board and move history.

Trade-offs
✓ Pros
Ensures correct enforcement of chess rules for special moves, preserving game integrity.
Improves player experience by allowing all legal moves.
Enables easier debugging and extension by isolating special move logic.
✗ Cons
Adds complexity to move validation logic.
Requires additional state tracking, increasing memory usage slightly.
May introduce subtle bugs if special move conditions are not exhaustively checked.
When building a complete chess game engine that must support all official chess rules including special moves.
For simplified chess variants or educational tools where special moves are intentionally omitted to reduce complexity.
Real World Examples
Lichess
Implements castling and en passant rules in their open-source chess engine to ensure official chess rules compliance.
Chess.com
Handles special moves in their game logic to provide accurate gameplay and fair competition.
Code Example
The before code only checks basic moves without special rules. The after code adds methods to detect and validate castling and en passant moves, tracking necessary state like king and rook movement and move history.
LLD
### Before: naive move validation without special moves
class ChessGame:
    def is_valid_move(self, start, end):
        # Only basic move validation
        return self.is_valid_basic_move(start, end)

### After: enhanced validation including castling and en passant
class ChessGame:
    def __init__(self):
        self.board = ...
        self.move_history = []
        self.king_moved = {'white': False, 'black': False}
        self.rook_moved = {'white': {'a': False, 'h': False}, 'black': {'a': False, 'h': False}}

    def is_valid_move(self, start, end):
        if self.is_castling_move(start, end):
            return self.can_castle(start, end)
        if self.is_en_passant_move(start, end):
            return self.can_en_passant(start, end)
        return self.is_valid_basic_move(start, end)

    def can_castle(self, start, end):
        # Check king and rook unmoved, path clear, not in check
        pass

    def can_en_passant(self, start, end):
        # Check last move was opponent pawn double step adjacent
        pass

    def is_castling_move(self, start, end):
        # Detect castling move pattern
        pass

    def is_en_passant_move(self, start, end):
        # Detect en passant move pattern
        pass
OutputSuccess
Alternatives
Simplified move validation
Ignores special moves and only validates basic piece movements.
Use when: When building a minimal chess demo or teaching basic piece movement without full rule enforcement.
Summary
Special moves like castling and en passant require explicit rules and state tracking in chess game logic.
Proper validation ensures legal moves and maintains game integrity.
Implementing these rules adds complexity but is essential for a complete chess engine.