Design Chess With Move Validation & Full Undo/Redo
In a 45-minute LLD interview, the interviewer assesses your ability to decouple piece movement geometry from board state and implement reversible action history. Scope the design to: an 8x8 board representation, piece movement validation via IMoveRule strategies with raycast obstruction checking, piece assembly via PieceFactory, and reversible MoveCommand executions encapsulating captured piece Mementos for full bidirectional undo() and redo(). Special rules like en passant, castling, and pawn promotion can be discussed as advanced rules.
Problem statement & clarifying requirements
Design an extensible 2-player Chess Engine supporting piece movement validation and full bidirectional Undo/Redo. Each piece type (Pawn, Rook, Knight, Bishop, Queen, King) follows distinct movement geometries and path obstruction checks, piece creation is centralized via a Factory Method, and moves are executed as reversible Commands encapsulating Memento snapshots so that undos seamlessly resurrect captured pieces back onto the board.
Each piece delegates its movement logic to a dedicated IMoveRule Strategy (RookRule, BishopRule, KnightRule, PawnRule, etc.) that inspects rank/file deltas and intermediate board obstruction.
The Command + Memento pattern: before mutating the board, MoveCommand snapshots the destination piece (if any). Calling undo() restores the moved piece to its origin and resurrects the captured piece at the destination.
In accordance with standard Command history semantics, executing any new move invalidates and clears the Redo stack, branching into a new timeline.
The ChessGame context tracks currentTurn (White / Black). A move is rejected immediately if the piece at the source square does not match the active player's color.
First, sketch the design yourself
Spend 5 minutes listing the classes, who owns whom, and which interface hides the part that changes.
- 1.
Separate piece identity and color from movement geometry: a
Piecehas aColorand atype_, but delegates spatial validation to an interchangeableIMoveRule. - 2.
Notice that Knights jump over pieces, while Rooks, Bishops, and Queens require an obstacle-free trajectory: encapsulate path walking in
Board::isPathClear(). - 3.
A move is not just source and destination coordinates: an undo must restore the entire previous tile state, making Memento snapshotting inside Command the cleanest architecture.
How to think through this problem (5-step walkthrough)
In a 45-minute LLD interview, the interviewer assesses your ability to decouple piece movement geometry from board state and implement reversible action history. Scope the design to: an 8x8 board representation, piece movement validation via IMoveRule strategies with raycast obstruction checking, piece assembly via PieceFactory, and reversible MoveCommand executions encapsulating captured piece Mementos for full bidirectional undo() and redo(). Special rules like en passant, castling, and pawn promotion can be discussed as advanced rules.
Not every noun in the prompt deserves its own class. Separate dumb data structs from stateful managers, and turn trivial nouns into fields or enums.
ChessGameCentral game context orchestrating player turns (White/Black), holding the 8x8 Board, validating active player ownership, and managing undoStack_ / redoStack_ command history.
BoardOwns the 8x8 tile grid (std::shared_ptr<Piece> grid_[8][8]) and performs raycast line-of-sight obstacle checks (isPathClear(from, to)) for sliding pieces.
PieceRepresents an individual chess piece on the board, binding piece color (Color), piece type (PieceType), and a reference to its specific IMoveRule strategy.
IMoveRuleStrategy interface declaring canMove(board, from, to, color) for piece-specific trajectories (RookRule, BishopRule, KnightRule, QueenRule, KingRule, PawnRule).
MoveCommandCommand object encapsulating move coordinates, board mutation, and an internal Memento snapshot (capturedPiece_) enabling perfect bidirectional execute() and undo().
PieceFactoryFactory Method centralizing piece instantiation and coupling pieces to their appropriate IMoveRule strategy singletons.
SquareColor / MoveHistoryStringTrap noun β do not create a SquareColor class for board tiles; square shading is a parity calculation (row + col) % 2 == 0. Similarly, move history should be stored as executable MoveCommand objects on an undo stack, not raw text strings that would require parsing to reverse.
A naive implementation puts move validation inside a massive switch(piece.type) inside Board::move(). Adding new pieces (like fairy chess pieces) or special rules forces modifying core board logic. Even worse, attempting undo by just moving the piece back from to to from permanently destroys the captured enemy piece, making proper chess undo impossible without replaying the entire match history from move 1.
A monolithic switch-statement couples the board to every piece geometry; adding new piece types (e.g. Princess, Empress) violates the Open-Closed Principle.
Ad-hoc coordinate swapping loses captured pieces and makes multi-level undo/redo fragile, stateful, and error-prone.
Scattering piece instantiation across board setup duplicates rule bindings and complicates testing with custom pieces.
- β
Command-Memento reversibility invariant: For any executed
MoveCommand, callingundo()restores the active piece tofromand resurrectscapturedPiece_atto, restoring the board to its exact pre-move tile configuration. - β
Redo stack invalidation: Executing any new move immediately clears the
redoStack_, preventing divergent history timelines after an undo. - β
Raycast line-of-sight clearance: For sliding pieces (Rook, Bishop, Queen), every intermediate square strictly between
fromandtoalong the ray must be empty (nullptr); Knights are exempt from path clearance.
Entities & design patterns used
Unlock Full UML Diagram & C++23 Solution
The complete UML diagram, production-ready C++23 code, follow-up extensions, and design pitfall analysis are part of Brute2Best Pro. All 7 foundation chapters are 100% free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.