Design Chess (Low-Level Design)
Low-level design of Chess: an 8x8 Board of Cells holding an abstract Piece subclassed per type, each with polymorphic canMove; a Game controller for turns, check/checkmate/stalemate, and special moves.
🟠Low-Level Design · Case Study · Senior
"Design Chess" is the heavyweight of board-game LLD. It has a rich domain — six piece types, each with its own movement rules — so it's the perfect test of polymorphism and clean OOD. Do it well and it's a showcase; hardcode a pile of if/else and it falls apart. Let's model it properly.
Step 1 — Requirements
- Functional: two players alternate moves; each piece moves by its own rules; capture opponents; detect check, checkmate, stalemate; support special moves (castling, en passant, promotion).
- Non-functional: extensible piece rules, clean separation between board state and game flow, easy to unit-test each piece.
Step 2 — The classes
- Board — an 8×8 grid of Cells; a Cell holds an optional Piece.
- Piece (abstract) → King, Queen, Rook, Bishop, Knight, Pawn — each implements
canMove(board, from, to). - Player — white or black; Move — from-cell, to-cell, piece, captured piece.
- Game — the controller: whose turn, apply/validate moves, check status.
- Enum
GameStatus{ACTIVE, CHECK, CHECKMATE, STALEMATE}andColor{WHITE, BLACK}.
Step 3 — Movement rules = polymorphism
🟠The heart of the design: each piece validates its own moves. No giant switch on piece type — that's the whole point. The Game asks the piece "can you legally go from here to there on this board?" and the piece answers.
abstract class Piece {
Color color;
abstract boolean canMove(Board b, Cell from, Cell to); // each subclass decides
}
class Bishop extends Piece {
boolean canMove(Board b, Cell from, Cell to) {
int dx = Math.abs(to.x - from.x), dy = Math.abs(to.y - from.y);
return dx == dy && pathIsClear(b, from, to) // diagonal, unobstructed
&& (to.piece == null || to.piece.color != color); // empty or capture
}
}
class Knight extends Piece {
boolean canMove(Board b, Cell from, Cell to) {
int dx = Math.abs(to.x-from.x), dy = Math.abs(to.y-from.y);
return (dx==1 && dy==2) || (dx==2 && dy==1); // L-shape, can jump
}
}
Adding a fairy-chess piece = a new subclass. Each piece is trivially unit-testable in isolation. (You could even make movement a pluggable Strategy if you wanted to swap rule sets.)
Step 4 — Check, checkmate & the tricky bits
🔴 The senior depth is in the game rules, not the pieces:
- Check: after a move, is the mover's own king attacked by any opponent piece? A move that leaves your own king in check is illegal — validate by simulating the move and testing.
- Checkmate: the king is in check and no legal move (by any piece) escapes it → game over.
- Stalemate: not in check but no legal move → draw.
- Special moves: castling (king+rook, neither moved, path clear, not through check), en passant, and pawn promotion — model these explicitly rather than bolting on flags.
Step 5 — Game flow
🟠The Game holds the board, both players, a move history (useful for undo, castling/en-passant eligibility, and threefold repetition), and the current turn. Each turn: validate the move (piece rule + doesn't leave own king in check), apply it, record it, then recompute status. Keeping validation in the pieces and orchestration in the Game is the clean separation interviewers reward.
The interview-ready walkthrough
"I model a Board of Cells holding an abstract Piece, subclassed per type — each piece implements its own canMove, so movement is polymorphic, not a switch. A Game controller manages turns, validates each Move (legal for the piece and doesn't leave your own king in check), and tracks a GameStatus — check/checkmate/stalemate. I keep a move history for undo and for castling/en-passant/repetition rules, and model special moves explicitly. Validation lives in the pieces, orchestration in the Game." That demonstrates real OOD on a rich domain."
What to read next
- Design Tic-Tac-Toe — the same ideas on a simpler board
- Design Snake & Ladder — next up, a dice-driven board game
- OOD Basics · SOLID Principles — the design toolkit
- ← The complete System Design guide (hub)