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.

Watch lesson4:35

Where it shows up

The same pattern appears across everyday software.

  • Stockfish: do_move and undo_move

    Its `Position` applies a move and stores what is needed to restore the position in a `StateInfo` object. Our `Undo` is the same idea.

  • python-chess: a board with a move stack

    `Board` represents a position and a move stack, with `push` and `pop`. Legal and pseudo-legal moves are two separate dynamic lists.

  • python-chess: claims and automatic draws

    `can_claim_threefold_repetition` and `is_fivefold_repetition` are separate methods, because the rules differ.

  • scalachess: immutable positions

    The chess library behind lichess describes itself as functional, immutable and free of side effects. Each move gives a new position.

  • chess.js: moves from the rules

    The JavaScript library answers `moves()` with a list of legal moves and keeps the screen out of it.

  • shakmaty: Zobrist and perft

    The Rust library lists legal move generation, Zobrist hashing and perft speed among its features.

  • UCI: screen and engine apart

    Engines read `position` and `go` as text and answer with a move. Any screen can use any engine: Strategy across processes.

  • Published perft results

    Programmers compare their move generators with the same published counts. It is a shared test suite without shared code.