Design chess
hard 90 minModel chess with classes that each know one thing: pieces know their steps, rules know about the king, the game knows whose turn it is.
Video lesson · Chapter 1 of 6
Make a prediction
See it run
Pieces on a real board, and the objects behind it: calls travel, fields change, the lines light up.
Operations
A pawn moves
e2 to e4: ask, check, apply, judge, tell.
Step 1 of 7
The position
| a | b | c | d | e | f | g | h | |
|---|---|---|---|---|---|---|---|---|
| 8 | ♜ | ♞ | ♝ | ♛ | ♚ | ♝ | ♞ | ♜ |
| 7 | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ |
| 6 | ||||||||
| 5 | ||||||||
| 4 | ||||||||
| 3 | ||||||||
| 2 | ♙ | ♙ | ♙ | ♙ | ♙ | ♙ | ♙ | ♙ |
| 1 | ♖ | ♘ | ♗ | ♕ | ♔ | ♗ | ♘ | ♖ |
White is to move. All 32 pieces stand at home.
Step 1: The position
def play(self, color, move): if self.status is not ACTIVE or color is not self.position.turn: raise IllegalMove("not your turn") if move not in self._rules.legal_moves(self.position): raise IllegalMove(move) self._redo.clear() return self._do(move)def _do(self, move): self._undo.append(self.position.apply(move)) self._seen[self._key()] += 1 self.status = self._judge() for listener in self._listeners: listener(move, self.status) return self.status- Cost
- One list of legal moves
- Guarantee
- One Undo record
Every check happens before the first change.
In the real world
The same chess doing real jobs. Pick one and step through it.
One player, two taps, one slow network.
Step 1 of 3
Two requests
The tap is sent twice. Both say: I have seen 10 half moves.
Step 1: Two requests
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Check detection that calls itself
criticalcan_moveasks about the king's safety, which asks every enemy piececan_move, which asks about the other king. The stack overflows.Wrongdef in_check(self, king): return any(p.can_move(king) for p in enemies) # asks for safety againRightdef hit_by(self, color, target): return any(target in p.attacks(self, s) for s, p in self.of(color))Change first, check later
criticalAn illegal move leaves the board changed, and the next player sees it.
Wrongundo = pos.apply(move)if in_check(mover): raise IllegalMove() # and nobody revertsRightif move not in rules.legal_moves(pos): raise IllegalMove(move)pos.apply(move)Trusting the screen
criticalThe screen marks legal squares. A changed client sends any square it likes.
Wrongboard.put(request.dst, board.take(request.src))Rightgame.play(session.color, Move(src, dst))A capture is where the piece lands
majorWrong for en passant, in both directions.
Wrongcaptured = board.take(move.dst)Rightat = Square(move.dst.col, move.src.row) if passing else move.dstcaptured = board.take(at)A flag `has_moved` that undo forgets
majorAfter an undo the king looks as if it had moved. Castling is lost.
Wrongpiece.has_moved = TrueRightundo = Undo(..., castling=self.castling) # and revert puts it backCounting repetitions by move
minorThe same squares with other castling rights are another position.
Wronghistory.count(move) >= 3Rightseen[(men, turn, castling, usable_en_passant)] >= 3
Interview tips
Open with
- Pieces know steps.
- Rules know kings.
- The game knows turns.
Reject aloud
- A Cell class.
- has_moved on a piece.
- A CheckDetector of its own.
Prove it
- 20, 400, 8902.
- Pin, castle, en passant.
- Undo gives the same key.
Never
- Recursion in check.
- Check as a status.
- Trust the client.