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.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:33

See it run

One garden with 18 stones. A die, a board and a game call each other, and the lines light up.

Operations

One turn, one ladder

Roll, count, look up, write down, pass on.

Step 1 of 6

Before

screencallergameGameAsha0Lee0nextAshastreak0dieScriptedDieshows?gardenBoardsize183, 810, 1611, 172, 7

Both players stand beside the board, at 0. Asha is next.

Step 1: Before

def take_turn(self, player):    if self.winner is not None:        raise GameOver(self.winner)    if player != self.current:        raise NotYourTurn(player)    roll = self._die.roll()    special = roll == self._rules.again_on    self._streak = self._streak + 1 if special else 0    wasted = special and self._streak >= self._rules.streak    start = self._at[player]    stepped = start if wasted else self._rules.finish(start, roll, size)    end = self._board.landing(stepped) if stepped != start else start    won = end == size    again = special and not wasted and not won    self._at[player] = end    if won:        self.winner = player    elif not again:        self._pass_on()    self._log.append(Turn(...))
Cost
O(1)
Guarantee
One Turn record
List of snake objects
a walk each turn

The board is asked once, where the counting ends.

In the real world

The same snake and ladders doing real jobs. Pick one and step through it.

The button is hit twice. Asha rolled a six, so the turn number is needed.

Step 1 of 3

Two requests

tap 1Requestturn7tap 2Requestturn7tableTableturn7lockfree

Both say: this is turn 7. A check of the name alone would not help, because a six keeps Asha at the die.

Step 1: Two requests

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • The client sends the roll

    critical

    Somebody will always roll what they need.

    Wrong
    game.move(player, request.roll)
    Right
    game.take_turn(session.player)     # the server rolls
  • A die nobody can replace

    major

    The test for three sixes runs until it happens to see three sixes.

    Wrong
    roll = random.randint(1, 6)
    Right
    roll = self._die.roll()            # handed in
  • Forgetting the streak

    major

    The counter of one player is carried to the next.

    Wrong
    self._next += 1
    Right
    self._streak = 0self._next = (self._next + 1) % len(self._order)
  • Two dictionaries

    major

    A square with a snake and a ladder is now possible, and the order of two ifs decides.

    Wrong
    if at in snakes: at = snakes[at]if at in ladders: at = ladders[at]   # after a snake, too
    Right
    at = jumps.get(at, at)
  • Checking the jump before counting

    minor

    A player that stayed is moved again by the stone they stand on.

    Wrong
    end = board.landing(start)
    Right
    end = board.landing(stepped) if stepped != start else start
  • A random number with a bias

    minor

    256 is not a multiple of 6: faces 1 to 4 come up more often.

    Wrong
    roll = 1 + random_byte % 6
    Right
    roll = 1 + secrets.randbelow(6)

Interview tips

  • Open with

    • One dictionary of jumps.
    • A die handed in.
    • One method: take_turn.
  • Reject aloud

    • Snake and Ladder classes.
    • A Cell for each square.
    • A Player with a board.
  • Prove it

    • Exact finish.
    • Three sixes.
    • Leave while next.
  • Never

    • Roll on the client.
    • Sleep in a test.
    • A chain without a rule.