0
0
LLDsystem_design~7 mins

Move validation and check detection in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
When move validation and check detection logic are tightly coupled with the main game loop or piece movement code, the system becomes hard to maintain and extend. This coupling leads to duplicated code, bugs when rules change, and difficulty in testing individual parts of the game logic.
Solution
Separate the move validation and check detection into dedicated components or modules. These components independently verify if a move is legal and if the king is in check after a move. This separation allows clear responsibilities, easier testing, and flexible rule updates without affecting the core game flow.
Architecture
Game Loop
Move Validation

This diagram shows the game loop sending move requests to the move validation module, which checks legality using the board state. If valid, the check detection module verifies if the king is under threat, using the king's position and board state.

Trade-offs
✓ Pros
Improves code maintainability by separating concerns.
Enables isolated testing of move rules and check detection.
Facilitates adding new rules or variants without changing core logic.
Reduces bugs caused by duplicated or intertwined logic.
✗ Cons
Introduces additional complexity in communication between modules.
May add slight performance overhead due to modular calls.
Requires careful design to keep modules synchronized with game state.
Use when building chess engines or board games with complex move rules and check conditions, especially if the codebase is expected to grow or require frequent updates.
Avoid if building a very simple or one-off prototype where modularity overhead outweighs benefits, or if performance is critical and profiling shows modular calls are bottlenecks.
Real World Examples
Lichess
Separates move validation and check detection to allow fast, accurate rule enforcement and easy addition of chess variants.
Chess.com
Uses modular validation to support complex rules and real-time move legality checks during online play.
Stockfish
Implements distinct validation and check detection modules to optimize engine evaluation and maintain correctness.
Code Example
The before code mixes move validation and check detection inside the Game class, making it hard to maintain. The after code moves validation and check detection into separate classes, improving modularity and testability.
LLD
### Before: move validation and check detection mixed in Game class
class Game:
    def move(self, from_pos, to_pos):
        # Validate move
        if not self.is_valid_move(from_pos, to_pos):
            return False
        # Make move
        self.board[to_pos] = self.board[from_pos]
        self.board[from_pos] = None
        # Check if king is in check
        if self.is_king_in_check(self.current_player):
            # Undo move
            self.board[from_pos] = self.board[to_pos]
            self.board[to_pos] = None
            return False
        return True

    def is_valid_move(self, from_pos, to_pos):
        # Complex logic here
        pass

    def is_king_in_check(self, player):
        # Complex logic here
        pass

### After: separated MoveValidator and CheckDetector
class MoveValidator:
    def __init__(self, board):
        self.board = board

    def validate(self, from_pos, to_pos):
        # Complex validation logic
        pass

class CheckDetector:
    def __init__(self, board):
        self.board = board

    def is_in_check(self, player):
        # Complex check detection logic
        pass

class Game:
    def __init__(self):
        self.board = ...
        self.validator = MoveValidator(self.board)
        self.check_detector = CheckDetector(self.board)

    def move(self, from_pos, to_pos):
        if not self.validator.validate(from_pos, to_pos):
            return False
        # Make move
        self.board[to_pos] = self.board[from_pos]
        self.board[from_pos] = None
        if self.check_detector.is_in_check(self.current_player):
            # Undo move
            self.board[from_pos] = self.board[to_pos]
            self.board[to_pos] = None
            return False
        return True
OutputSuccess
Alternatives
Monolithic move handling
All move validation and check detection logic is embedded directly in the game loop or piece classes.
Use when: When building very simple or educational chess programs where modularity is not a priority.
Event-driven validation
Move validation and check detection are triggered by events and listeners rather than direct calls.
Use when: When building highly extensible or plugin-based chess platforms requiring dynamic rule changes.
Summary
Separating move validation and check detection prevents tangled code and bugs.
This modular approach improves maintainability and allows easier rule updates.
It is widely used in professional chess engines and online platforms.