AlgoViz

Design snake and ladders

easy 75 min

A small board game that shows judgement: one map for snakes and ladders, a die that is handed in, rules as values, and one object that owns what changes.

Watch lesson4:33

Where it shows up

The same pattern appears across everyday software.

  • boardgame.io: randomness belongs to the engine

    Its documentation asks moves to be pure and gives them a `random` object with functions such as `D6`, because calling an outside generator would make the game logic impure.

  • boardgame.io: turn events

    `endTurn` can be called without an argument or with the next player. Passing the turn is one named operation, like our `_pass_on`.

  • Unity: a seeded sequence

    `Random.InitState` starts the generator from a seed. The manual explains that the sequence is preset and only appears random.

  • Python: two kinds of random

    The `random` module documents seeds and reproducible sequences, and points to `secrets` for security purposes.

  • Java: SecureRandom

    The class provides a cryptographically strong generator. It can stand in for the source of our `RandomDie`.

  • Browsers: getRandomValues

    The Web Crypto method fills an array with cryptographically strong values. Our TypeScript die uses it and refuses the uneven rest.

  • Chutes and Ladders as a Markov chain

    The published board has 100 squares with 19 chutes and ladders. An article in The Mathematical Gazette treats the game exactly as a chain, as we do.

  • PostgreSQL: the unique key as referee

    `INSERT ... ON CONFLICT` depends on a unique index. A key on table and turn number decides which of two servers stored the roll.