Design a chess game
Design a two-player chess game that validates moves and detects check, checkmate and stalemate.
Good first question for: Senior
Requirements to confirm first
- The standard 8 by 8 board and pieces; players alternate
- A move is legal only if the piece can make it and it does not leave the mover's own king in check
- Detect check, checkmate and stalemate after each move
- Keep the move history and support undo
- Ask: are castling, en passant and promotion in scope? Clocks?
Write your own class list on paper before you open the answer key. Ten minutes of your own design is the practice; reading the key is only the check.
Show the answer key (write yours first)
Classes and what each one owns
- Game
- The players, whose turn it is, the status and the move history
- Board
- Which piece is on which square; applies and reverts moves
- Piece
- Abstract base; King, Queen, Rook, Bishop, Knight and Pawn each know how they move, ignoring check
- Position
- A file and a rank, as a value
- Move
- From, to, the piece moved, the piece captured and any special flag
- Rules
- Filters piece moves down to legal ones by testing whether the king is safe
How they connect
- Board maps positions to pieces; a Piece does not store the board, it is given the board when asked for moves
- Game has a Board and a list of Moves; undo takes the last Move and the board reverts it
- Subclasses per piece are right here: each has different behaviour, not just different data
Design patterns that fit, and why
- Polymorphism
- Each piece type supplies its own move generation, so there is no switch on piece type anywhere.
- Command
- A Move that can be applied and reverted gives undo and the history without extra work.
- Factory
- Sets up the starting position, or a custom position for a test.
Code sketch in Java
The core types and one real method. It is a sketch to talk through, not a full program.
abstract class Piece {
final Color color;
// squares this piece could move to, ignoring whether its own king ends up in check
abstract List<Position> candidateMoves(Board board, Position from);
}
class Rules {
// isInCheck and legalMoves scan candidate moves; left out of the sketch
boolean isLegal(Board board, Move move) {
board.apply(move);
boolean safe = !isInCheck(board, move.piece().color);
board.revert(move);
return safe;
}
boolean isCheckmate(Board board, Color side) {
return isInCheck(board, side) && legalMoves(board, side).isEmpty();
}
}Edge cases to raise yourself
- A pinned piece: its normal move is illegal because it exposes the king
- Stalemate: no legal move and not in check is a draw, not a loss
- Castling through an attacked square, and en passant only on the very next move
- The choice of piece at promotion
Where answers usually go wrong
- A switch on piece type inside Board. This is the one question where a subclass per piece is expected.
- Letting each piece decide full legality. A piece cannot know about pins and checks; generate candidate moves, then filter by king safety in one place.
- No Move object, only a 'from' and a 'to'. Then undo, history and special moves have nowhere to live.
- Calling 'no legal moves' a checkmate without checking that the king is attacked.
Follow-up questions
- Add a clock per player
- Save and load a game
- Plug in a computer player: what interface does it need?