Design a Chess Game
0%
Editorial Solution
Requirements
Define Core Objects
Analyze Relationships
Establish Hierarchy
Piece Hierarchy
Design Patterns
Command Pattern: Move History and Undo
Define Class Members (write code)
Enums and Value Objects
Adhere to SOLID Guidelines
Consider Scalability and Flexibility
Thread Safety for Online Play: The Core Concurrency Challenge
Create/Explain your diagram(s)
Future improvements
Table of Contents
Requirements
Define Core Objects
Analyze Relationships
Establish Hierarchy
Piece Hierarchy
Design Patterns
Command Pattern: Move History and Undo
Define Class Members (write code)
Enums and Value Objects
Adhere to SOLID Guidelines
Consider Scalability and Flexibility
Thread Safety for Online Play: The Core Concurrency Challenge
Create/Explain your diagram(s)
Future improvements
Chess is one of the richest OOD problems because it combines strict rule enforcement, complex state management, and a natural class hierarchy. Before designing any classes, let's nail down exactly what the system must do.
Interview Dialogue
Functional Requirements
Start by scanning the requirements for nouns. Each one is a candidate class. "Board" with "64 squares" gives us Board and Cell. "Pieces" like "King, Queen, Rook, Bishop, Knight, Pawn" each have "distinct movement rules," pointing to an abstract Piece with six subclasses. "Move" carrying "from/to positions, captured piece, special move flags" becomes the Move object. "Players" with a "color" give us Player. "Check, checkmate, stalemate" map to a GameStatus enum. Not every noun survives. "Turn" is a state tracked by Game rather than a standalone class, but the nouns give you a strong starting lineup to refine.
Let's discover the objects by asking: what do we need at each step of a chess game?
Two players sit down. The game starts. We need a Board with 64 squares and 32 pieces. Already we face a design question: should the board be a Piece[][] array, or should we wrap each square in its own object? A raw array works, but every piece of code that touches the board would need to do null checks (if (board[r][c] != null)), and if you later want to mark squares as attacked or highlighted, you have nowhere to put that data. So we introduce Cell: a simple wrapper around a square that knows whether it is empty and provides clean get/set/clear operations.
The trickiest relationship question in chess is: does a Cell own its Piece?
Cell → Piece is association, not composition. This trips up many candidates. Think about what happens when a piece moves from e2 to e4. If the Cell owned the Piece (composition), removing it from the old cell would destroy it. But the piece is still alive; it just lives in a different cell now. And when a piece is captured, it leaves the board entirely but continues to exist in the capturing player's captured list. A Cell is just a parking spot. It temporarily holds a Piece, but the Piece has its own identity and lifecycle.
Game → Board and Board → Cell are composition. The Board cannot exist without its Game. When the game ends, the board is discarded. Similarly, a Cell has no meaning outside its Board, the board creates all 64 cells in its constructor. These relationships represent genuine ownership.
The chess piece hierarchy is one of the cleanest examples of polymorphism in OOD.
Piece Hierarchy
The abstract Piece class defines the common contract:
Think about what happens when a player makes a bad move and wants to take it back. Without a formal undo mechanism, you would need to snapshot the entire board before every move, 64 cells, 32 pieces, positions, hasMoved flags, the captured list, whose turn it is. That is expensive and fragile. You also have the sliding-piece problem: Rook, Bishop, and Queen all move the same way (keep going in a direction until something blocks you), and writing that loop three times is a maintenance headache. Finally, if you ever want AI opponents, the move-selection algorithm should not be tangled into the Game class. These three pain points map directly to three patterns.
Command Pattern: Move History and Undo
The undo problem is what leads us here. Storing board snapshots would work, but every snapshot copies 64 cells, all piece positions, and every game flag, memory that grows linearly with game length for a 300-move game. Instead, notice that each move is a small, reversible change: one piece shifts, at most one piece is captured, and a few flags flip. If we record just that delta, we can reverse any move without keeping a full copy of anything. That is exactly what the Command pattern gives us. The Move class captures everything needed to execute and undo a single transition, and the moveHistory list acts as the command stack:
Now we translate the design into Java. A few decisions to call out before we start: Position is immutable (a position on the board never changes, pieces move between positions). Color is an enum with an opposite() helper because turn-switching and enemy-detection come up constantly. And GameStatus is an enum rather than a boolean because there are six distinct end states, not just "game over" vs "still playing."
Enums and Value Objects
Single Responsibility Principle
- Each
Piecesubclass knows only its own movement rules. Boardmanages the grid and piece placement.Gameorchestrates turns, validation, and status detection.Moverecords the data needed to execute and undo a transition.
No class handles concerns outside its domain. The King does not know about game status. The Board does not validate turns.
Thread Safety for Online Play: The Core Concurrency Challenge
In a local game, both players share a single thread and take turns through method calls. But in an online setting, each player connects from a separate thread (or network handler). Now consider what happens without synchronization: White calls makeMove() and is halfway through executing, the piece has been moved on the board, but the turn has not switched yet. At that exact moment, Black's thread also calls makeMove(). It sees currentTurn == WHITE (not yet switched), so it rejects Black's move, even though White's move was about to complete. Or worse: both threads modify the board simultaneously and leave it in a corrupted state where two pieces occupy the same cell.
The critical invariant is simple: only one move can be processed at a time per game, and it must be the correct player's turn. synchronized on makeMove() enforces this:
Below is the class diagram for the chess game system.
Key points to highlight in an interview:
AI Opponent with Minimax
Implement a minimax algorithm with alpha-beta pruning and iterative deepening. A position evaluation function scores material balance (Queen=9, Rook=5, Bishop/Knight=3, Pawn=1), piece activity (number of legal moves), king safety (pawn shield, exposed squares), and pawn structure (doubled, isolated, passed pawns). The MoveStrategy interface allows swapping difficulty levels without touching the game engine.
Chess Variants
Try this problem yourself
Reading a solution is not the same as producing one under interview pressure. Design it on the whiteboard and get scored feedback.
0%
Editorial Solution
Requirements
Define Core Objects
Analyze Relationships
Establish Hierarchy
Piece Hierarchy
Design Patterns
Command Pattern: Move History and Undo
Define Class Members (write code)
Enums and Value Objects
Adhere to SOLID Guidelines
Consider Scalability and Flexibility
Thread Safety for Online Play: The Core Concurrency Challenge
Create/Explain your diagram(s)
Future improvements
Table of Contents
Requirements
Define Core Objects
Analyze Relationships
Establish Hierarchy
Piece Hierarchy
Design Patterns
Command Pattern: Move History and Undo
Define Class Members (write code)
Enums and Value Objects
Adhere to SOLID Guidelines
Consider Scalability and Flexibility
Thread Safety for Online Play: The Core Concurrency Challenge
Create/Explain your diagram(s)
Future improvements