Design a Chess Game
You'll learn to
- -Scope a very large prompt out loud, naming what is in and out before designing
- -Give each piece its own movement rule by polymorphism, and put rules every piece shares in one place
- -Detect check by reusing the pieces' own move rules, and make moves undoable with Command
Chess is the most demanding of the classic LLD prompts, because it has real domain complexity. Six kinds of piece move in genuinely different ways, and there is check, checkmate and a handful of special moves. A shallow design cannot hand-wave that away. It rewards the candidate who scopes deliberately and does not try to model every rule of chess in 45 minutes.
Step 1: Scope the Problem Out Loud
Say what is in and out before you design. Standard movement and capture, plus check detection, are in. Castling, en passant and pawn promotion are special cases you name as things you would add with more time, rather than ignoring silently or cramming in. Scoping is itself a strong signal, and it echoes the very first chapter of this course.
Step 2: A Piece Hierarchy, With Movement as Polymorphism
Every piece answers the same question, "is this move legal for you?", but each answers it differently. A bishop moves diagonally and cannot jump over pieces. A knight moves in an L shape and can jump. That is polymorphism: the board asks the question once and never needs to know which kind of piece it asked.
class Color(Enum):
WHITE = "white"
BLACK = "black"
class Piece(ABC):
def __init__(self, color: Color):
self.color = color
@abstractmethod
def is_legal_move(self, board: "Board", start: "Position", end: "Position") -> bool: ...
class Bishop(Piece):
def is_legal_move(self, board, start, end) -> bool:
row_diff = abs(end.row - start.row)
col_diff = abs(end.col - start.col)
if row_diff != col_diff: # must move diagonally
return False
return board.is_path_clear(start, end)
class Knight(Piece):
def is_legal_move(self, board, start, end) -> bool:
row_diff, col_diff = abs(end.row - start.row), abs(end.col - start.col)
return (row_diff, col_diff) in [(1, 2), (2, 1)] # knights jump, no path check neededAdding a chess variant with a custom piece now means one new class and no other edits. The board never says "if it is a bishop do this, if it is a knight do that".
Step 3: The Board and Move Execution
The board owns the grid, checks that a path is clear, and applies a move. Notice where one rule lives. "You cannot capture your own piece" applies to every piece equally, so move_piece checks it once, instead of six piece classes each repeating it. Each is_legal_move only has to answer "does this piece move this way?"
class Board:
def __init__(self):
self._grid: dict[Position, Piece] = self._initial_setup()
def is_path_clear(self, start: "Position", end: "Position") -> bool:
for pos in positions_between(start, end): # every square strictly between start and end
if pos in self._grid:
return False
return True
def move_piece(self, start: "Position", end: "Position") -> "Piece | None":
piece = self._grid[start]
target = self._grid.get(end)
if target is not None and target.color == piece.color:
raise IllegalMoveError() # can't capture your own piece - checked once here,
if not piece.is_legal_move(self, start, end): # not duplicated across all six piece classes
raise IllegalMoveError()
captured = target
self._grid[end] = piece
del self._grid[start]
return capturedStep 4: Turn Order as State, Moves as Commands
Two earlier patterns fit cleanly. Whose turn it is, and whether the game is ongoing, in check or over, is a State machine, with states such as white's turn, black's turn and checkmate, each allowing different actions. Each executed move is a Command object that remembers the piece, the squares and what was captured. Pushing those onto a history stack gives you undo for free, in the same shape as the Command chapter.
class MoveCommand(Command):
def __init__(self, board: Board, start: "Position", end: "Position"):
self._board = board
self._start = start
self._end = end
self._captured: Piece | None = None
def execute(self) -> None:
self._captured = self._board.move_piece(self._start, self._end)
def undo(self) -> None:
self._board._grid[self._start] = self._board._grid.pop(self._end)
if self._captured:
self._board._grid[self._end] = self._capturedStep 5: Check Detection, Scoped Honestly
Is a king in check? For every enemy piece, ask whether a move to the king's square is legal for it. That reuses the is_legal_move already on every piece, with no separate check logic per piece type. Checkmate sits on top: the king is in check and no legal move by the player resolves it. That is a real but bounded amount of extra work, worth describing rather than coding live if time is short.
The demo below sets up a small board and tries a few moves, including an undo and a check. One honest limit: it has no pawns, which fits our scope, because a pawn captures differently from how it moves.
On a problem this large, saying "given the time, I will implement standard movement and check detection fully, and describe castling, en passant and promotion" is a stronger signal than silently running out of time on an unscoped attempt.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Forge Level 44: Chess Game asks you to implement movement legality and occupancy rules for a scoped-down set of pieces, graded by running your actual code against real test cases.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!