AlgoViz

Design chess

hard 90 min

Model 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

0:00 / 4:35

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

abcdefgh
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

tap 1Requestseen10tap 2Requestseen10roomGameRoomply10lockfree

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

    critical

    can_move asks about the king's safety, which asks every enemy piece can_move, which asks about the other king. The stack overflows.

    Wrong
    def in_check(self, king):    return any(p.can_move(king) for p in enemies)   # asks for safety again
    Right
    def hit_by(self, color, target):    return any(target in p.attacks(self, s) for s, p in self.of(color))
  • Change first, check later

    critical

    An illegal move leaves the board changed, and the next player sees it.

    Wrong
    undo = pos.apply(move)if in_check(mover):    raise IllegalMove()      # and nobody reverts
    Right
    if move not in rules.legal_moves(pos):    raise IllegalMove(move)pos.apply(move)
  • Trusting the screen

    critical

    The screen marks legal squares. A changed client sends any square it likes.

    Wrong
    board.put(request.dst, board.take(request.src))
    Right
    game.play(session.color, Move(src, dst))
  • A capture is where the piece lands

    major

    Wrong for en passant, in both directions.

    Wrong
    captured = board.take(move.dst)
    Right
    at = Square(move.dst.col, move.src.row) if passing else move.dstcaptured = board.take(at)
  • A flag `has_moved` that undo forgets

    major

    After an undo the king looks as if it had moved. Castling is lost.

    Wrong
    piece.has_moved = True
    Right
    undo = Undo(..., castling=self.castling)   # and revert puts it back
  • Counting repetitions by move

    minor

    The same squares with other castling rights are another position.

    Wrong
    history.count(move) >= 3
    Right
    seen[(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.