Skip to content
Low Level Design Mastery Logo
LowLevelDesign Mastery

Design a Chess Game

Design a robust board game with complex rules and state validation.

Design a chess game system that allows two players to play against each other, adhering to standard chess rules. The system should manage the game board, validate moves according to chess rules, handle turn management, detect game states (check, checkmate, stalemate), and maintain move history.

In this problem, you aren’t just building a board; you’re building a rule engine that ensures every move is legal and every piece follows its unique movement pattern.


Design a chess game system that allows two players to play against each other, adhering to standard chess rules. The system should manage the game board, validate moves according to chess rules, handle turn management, detect game states (check, checkmate, stalemate), and maintain move history.

Functional Requirements:

  • Board Setup: Initialize an 8x8 chessboard with all pieces in their correct starting positions.
  • Piece Movement: Support all six piece types (Pawn, Rook, Knight, Bishop, Queen, King) with their specific rules.
  • Turn Management: Players must alternate turns, with the white player starting first.
  • Move Validation: Ensure moves are legal, the path is clear (except for Knights), and follow turn order.
  • King Safety: Prevent moves that would leave the player’s own king in check.
  • Game Status: Detect and handle special states: Check, Checkmate, and Stalemate.
  • History Tracking: Maintain a history of all moves made, including source/destination and any captured pieces.
  • Move Restrictions: Prevent players from making moves when it’s not their turn.

Non-Functional Requirements:

  • Object-Oriented Design: Clear separation of concerns and well-defined responsibilities for each class.
  • Extensibility: Support adding special moves like Castling or En Passant as future enhancements.
  • Performance: Move validation must be efficient enough for real-time play.
  • Robustness: Handle edge cases gracefully, such as invalid move attempts or game state queries.
  • Maintainability: Use appropriate design patterns (Factory for creation, Strategy for movement) to make the code manageable.

The system is centered around the Game controller which coordinates between the Board and the Players.

Diagram
classDiagram
    class Game {
        -Board board
        -Player[] players
        -List~Move~ history
        +makeMove(player, from, to)
        +isGameOver()
    }
    
    class Board {
        -Cell[][] grid
        +getPiece(x, y)
        +movePiece(from, to)
    }
    
    class Piece {
        <<abstract>>
        -Color color
        +isValidMove(board, from, to)*
    }

    class Knight {
        +isValidMove()
    }

    class Pawn {
        +isValidMove()
    }
    
    Game --> Board
    Board --> Piece
    Piece <|-- Knight
    Piece <|-- Pawn

Diagram

Every piece has a unique way of moving. Hardcoding this in a central Validator class leads to a massive, unmaintainable if-else block.

Solution: Use Polymorphism. Define an abstract Piece class with an isValidMove() method. Each concrete class (Queen, Knight, etc.) implements its own logic.

Checkmate occurs if the King is in check and no legal move by the player can remove that check.

Solution: The Game class can iterate through all pieces of the current player, calculate every possible move, and simulate them. If none result in the King being safe, it’s Checkmate.

A player cannot make a move that puts or leaves their own King in check.

Solution: Use a Sandbox/Simulation approach. Before finalizing a move, “tentatively” apply it on a copy of the board, check if the King is under attack, and then either commit or roll back.


By solving this problem, you’ll master:

  • Inheritance & Polymorphism - Structuring complex hierarchies.
  • Rule Engines - Building logic to validate state transitions.
  • Grid-based Modeling - Managing spatial data structures.
  • State Machines - Handling game flow and turn logic.

Ready to see the full implementation? Open the interactive playground to access:

  • 🎯 Step-by-step guidance through the 8-step LLD approach
  • 📊 Interactive UML builder to visualize your design
  • 💻 Complete Code Solutions in Python, Java, C++, TypeScript, JavaScript, C#
  • 🤖 AI-powered review of your design and code

After mastering Chess, try these similar problems: