Chess Game

Classic Board Game

Design a two-player chess game: the board, the six piece types and their moves, legal-move checking, and check, checkmate and stalemate detection.

Overview

Chess is played on an 8 by 8 board by two players, White and Black, who move in turn. Each of the six piece types moves in its own way. A move is legal only if the piece can make it, nothing blocks it, the target does not hold a friendly piece, and the mover's own king is not left under attack. The game ends in checkmate when the player to move is in check and has no legal move, or in stalemate, a draw, when they are not in check and have no legal move.

The problem is a favorite for senior interviews because it has a clean shape with real depth. The piece rules are a textbook case for polymorphism. Legal-move checking forces you to try a move, look at the board, and take the move back, which is the Command pattern with a real reason to exist. Check and checkmate detection is a small algorithm on top of everything else, and it only works if the pieces below it are right.

There are three things interviewers watch for. Whether each piece owns only its geometry and leaves blocking, turn order and check to the board and the game. Whether you can undo a move exactly, including a capture and a promotion. And whether you know the difference between a pseudo-legal move (the piece can go there) and a legal move (and the king is safe afterwards).

Scoping the problem

The full rules of chess are long, so agree on a subset before you design anything. These are the questions to ask, why each one matters, and the answer interviewers usually give.

  • All six pieces with their real moves? Yes. This is the core; anything less is not chess.
  • Check, checkmate and stalemate? Yes. They need attack detection and a try-and-undo loop.
  • Castling and en passant? Design for them, and code them if time allows. Each needs extra state: has the king moved, and what was the last move.
  • Pawn promotion? Yes, promote to a queen. It needs a choice of piece and an exact undo.
  • Draw by repetition or the fifty-move rule? Out of scope. They need position hashing and a counter.
  • Undo and move history? Yes. They fall out of the Move object if you design it that way.
  • A clock, players, or a user interface? Out of scope. None of them is the design's core.

With the scope agreed, the classes almost name themselves.

Functional requirements

  • An 8 by 8 board with the standard starting position: White on ranks 1 and 2, Black on ranks 7 and 8
  • All six piece types move by the real rules: king one square, queen and rook and bishop slide until blocked, knight jumps in an L, pawn moves forward and captures diagonally
  • A move is refused if the piece cannot reach the square, a piece is in the way, the target holds a friendly piece, it is not that color's turn, or the mover's own king would be left in check
  • A pawn that reaches the last rank is promoted (to a queen in this implementation)
  • After every move the game reports ongoing, check, checkmate or stalemate, and refuses moves once the game is over
  • Every move is recorded with what it captured so it can be undone exactly

Non-functional requirements

  • Adding a piece type (a chess variant) means adding one subclass and one map entry; Board and Game do not change
  • Legality checking uses apply and undo on one board, never a copy of the board per candidate move
  • The rules of the game live in Game and Board; no piece knows about turns, check or the history
  • Coordinates are one type everywhere: (row, col) with a helper that reads algebraic names such as e4

Interview questions

  • Why one class per piece instead of a switch on the piece type?
  • How does undo work, and why does legal-move generation depend on it?
  • Where should move validation live: in the piece, the board or the game?
  • How would you add castling and en passant?
  • Is generating every legal move on every turn too slow?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio