Design snake and ladders
easy 75 minA 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
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
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
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
criticalSomebody will always roll what they need.
Wronggame.move(player, request.roll)Rightgame.take_turn(session.player) # the server rollsA die nobody can replace
majorThe test for three sixes runs until it happens to see three sixes.
Wrongroll = random.randint(1, 6)Rightroll = self._die.roll() # handed inForgetting the streak
majorThe counter of one player is carried to the next.
Wrongself._next += 1Rightself._streak = 0self._next = (self._next + 1) % len(self._order)Two dictionaries
majorA square with a snake and a ladder is now possible, and the order of two ifs decides.
Wrongif at in snakes: at = snakes[at]if at in ladders: at = ladders[at] # after a snake, tooRightat = jumps.get(at, at)Checking the jump before counting
minorA player that stayed is moved again by the stone they stand on.
Wrongend = board.landing(start)Rightend = board.landing(stepped) if stepped != start else startA random number with a bias
minor256 is not a multiple of 6: faces 1 to 4 come up more often.
Wrongroll = 1 + random_byte % 6Rightroll = 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.