Object-Oriented Design Interview: Designing a Chess Game
6 views
"Design a chess game" is a classic object-oriented design (low-level design) question. Interviewers are not looking for a chess engine; they want clean classes, clear responsibilities and extensibility. Here is a structure for a 45-minute round.
1. Clarify scope
Ask: two human players on one device, or online play? Do we need move validation for every rule, including castling, en passant and promotion? Timers? Undo? Saving games? A good answer: two players, full move validation, check and checkmate detection, and move history, with timers as an extension.
2. Core classes
- Game: owns the board, the two players, whose turn it is, the move history and the game status (active, check, checkmate, stalemate, resigned).
- Board: an 8x8 grid of Square objects; can return the piece at a position and apply or revert a move.
- Square / Position: row and column, plus the piece on it, if any.
- Piece (abstract): color, has_moved, and a method candidate_moves(board, position).
- King, Queen, Rook, Bishop, Knight, Pawn: subclasses implementing their movement rules.
- Move: from, to, piece moved, piece captured, and flags for castling, en passant and promotion.
- Player: name and color; could later be a human or an AI subclass.
3. Validating a move
Validation happens in two layers:
1. Piece rules: the piece's candidate_moves checks geometry and blocked paths. Sliding pieces (rook, bishop, queen) share a helper that walks in a direction until blocked.
2. Game rules: the Game applies the move on the board temporarily and checks whether the mover's own king is in check. If it is, the move is illegal, and the board reverts it.
Keeping "does this piece move like that?" separate from "is this move legal right now?" is the key design point to say out loud.
4. Special moves
- Castling: requires that king and rook haven't moved, the squares between are empty, and the king doesn't pass through check.
- En passant: needs the last move from history, which is a good reason to store moves as objects.
- Promotion: the Move carries the chosen piece type.
5. Check, checkmate and stalemate
After each move, compute whether the opponent's king is attacked. If the opponent has no legal moves, it is checkmate when in check and stalemate otherwise.
6. Extensibility
- Undo: pop a Move from history and revert it, which is easy because Move records the captured piece.
- Timers: a Clock per player managed by the Game.
- Online play: the Game becomes a server-side object and moves arrive as commands, so the same validation code is reused.
- AI player: implement a Player that picks moves, without touching board logic.
7. Wrap up
Summarize responsibilities, mention which design patterns appear (polymorphism for pieces, the command pattern for moves) and how you would test them, such as a unit test per special move.
During live design rounds, Linkjob AI (https://linkjob.ai) offers real-time help in technical interviews, which can help you remember an edge case like castling through check when the conversation moves quickly.