AlgoViz

Design tic-tac-toe

easy 70 min

Model a game everybody knows: marks on a grid, turns, a full line wins.

Watch lesson4:32

Where it shows up

The same pattern appears across everyday software.

  • React: the tic-tac-toe tutorial

    The official tutorial builds this game and adds time travel. It stores a list of past boards and never changes one: undo by pictures, right for a user interface where the pictures are small.

  • boardgame.io

    A framework for turn-based games. Its tutorial is tic-tac-toe: a move function returns `INVALID_MOVE` for a taken cell, and a separate `endIf` decides about victory or draw. Moves and verdict have different owners.

  • boardgame.io: undo and redo

    The documentation describes undo and redo within a turn, and that they can be switched off for a game. Who may take back what is a rule, not a feature of the list.

  • OpenSpiel: tic_tac_toe

    DeepMind's collection of games has `UndoAction` in its C++ state and the constant 5478 for the number of positions, the number of this page.

  • PettingZoo: tictactoe

    The learning environment gives each player a mask of legal moves, and an illegal move ends the game with a penalty. The referee does not trust the players.

  • python-chess: push and pop

    The board keeps a stack of moves: `push` makes one, `pop` takes it back. Programs that look ahead do this all the time, which is why undo must be cheap.

  • Python: functools.lru_cache

    The memory behind our computer player. One line turns the walk over 549,946 nodes into 5,478 values, because a position can be a key.

  • MENACE

    The matchbox machine of 1961 played against its maker and improved. It is a table of positions with weights, made of beads: a computer player without a computer.