Design a Chess Game

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-05-14

Problem Statement

Design the Low-Level Design (LLD) of an online/offline Chess game between two players.


The system should simulate a standard 8×8 chessboard, enforce chess rules, and manage turns, moves, and game status.


You must focus on object-oriented modeling — identifying key classes, their relationships, and responsibilities — not on UI or network gameplay (that would be HLD).


🧠 Functional Requirements

Game Setup


•Two players participate — one plays White, one plays Black.
•Standard 8×8 board with 32 pieces (16 per player).
•Each player’s pieces are placed in their initial positions.


Moves


•Players take alternate turns.
•Each piece type (Pawn, Rook, Knight, Bishop, Queen, King) moves according to its rules.
•The system should validate if a move is legal before executing it.


Check / Checkmate / Stalemate


•Detect when a King is in check or checkmate.
•Detect stalemate (no legal move but not in check).
•End the game appropriately when checkmate or draw occurs.

Game Flow


•Alternate turns between White and Black.
•Maintain the state of the board after each move.
•Allow moves to be undone (optional feature).

Special Rules (Optional Enhancements)


Castling


Pawn Promotion


En Passant


Draw by repetition or 50-move rule

Game Result


•Declare result as White Wins, Black Wins, or Draw.


🚫 Non-Functional Requirements


•Ensure all moves are validated before changing board state.
•Design should support future extensions like:
•Timed games (with a chess clock)
•Online multiplayer
•Move history and replay


🧱 Suggested Classes


ClassResponsibility


ChessGame


Manages overall game flow, players, board, turns, and game status.


Board


Represents the 8×8 chessboard with squares and pieces.


Square


Represents a single cell on the board (row, column, piece reference).


Piece


Abstract base class for all chess pieces with color and position.


Pawn, Rook, Knight, Bishop, Queen, King


Concrete classes implementing specific movement rules.


Player


Represents a player (id, name, color).


Move


Represents a move (from-square, to-square, piece, captured piece if any).


GameStatus (Enum)


Enum values like IN_PROGRESS, WHITE_WIN, BLACK_WIN, DRAW.


MoveValidator


Responsible for validating moves according to piece rules and game state.


🔄 Example Flow


1.Game Start
1 2ChessGame.startGame() 3→ Initialize board and players 4→ White moves first
1.Player Move
1 2Player(White): Move pawn from e2 → e4 3→ Validate move (legal pawn move) 4→ Update board
1.Next Move
1 2Player(Black): Move pawn from e7 → e5


Check / Checkmate

•If a move results in the opponent’s King being attacked → mark as “Check”.
•If no valid moves exist for the opponent while in check → “Checkmate”.


4. Game End

1 2Result: White wins by checkmate.


🧪 Example Input / Output


Input:

1 2Move 1: e2 → e4 3Move 2: e7 → e5 4Move 3: Qd1 → h5 5Move 4: Nb8 → c6 6Move 5: Bf1 → c4 7Move 6: Ng8 → f6 8Move 7: Qh5 → f7


Output:

1 2Checkmate! White wins.


⚙️ Constraints


•Board size fixed at 8×8.
•Each player can make only one move per turn.
•All moves must follow official chess rules.
•No simultaneous or parallel turns.


🎯 Goals


Your design should:


•Demonstrate strong object-oriented principles (inheritance, polymorphism, abstraction).
•Support extensibility (easy to add new rules/features).
•Keep game logic modular, separating validation, move execution, and state management.


Solution

Comments