Design Snakes and Ladders
🎲 What is Snakes and Ladders? It is a race along a numbered board. Each player rolls a die and moves forward. Landing at the foot of a ladder carries you up; landing on a snake's head slides you down to its tail. The first player to reach the last cell wins.
Snakes and Ladders is a common warm-up because the game has almost no decisions in it: the dice decide everything. That is exactly what makes it a good design test. With no strategy to model, the interviewer watches how you handle the parts that are easy to get wrong: randomness you cannot test, turn order with more than two players, a rule that breaks the "next player" pattern, and the temptation to build a class for every noun on the box.
Requirements
"Design a Snakes and Ladders game. Several players take turns rolling a die and moving along a board with snakes and ladders."
Most interviewers stop there. The board, the dice and the turn rules all have house variants, so the questions below pick one version and turn it into rules we can code against.
Clarifying Questions
You: How big is the board, and where are the snakes and ladders?
Interviewer: The classic board has 100 cells, but take the size and the positions as input. Assume the layout is valid: no two snakes or ladders start on the same cell, and none ends where another one starts.
You: How many players, and how many dice?
Interviewer: Two to six players, one normal six-sided die. We may want other dice later.
You: What happens if a roll would take a player past the last cell?
Interviewer: They stay where they are. You have to land on the last cell exactly to win.
You: Does rolling a six do anything special?
Interviewer: Yes, a six gives the same player another turn. If the six wins the game, the game is simply over.
You: Does the game stop at the first winner, or do the others keep playing for second place?
Interviewer: Stop at the first winner for now.
You: Can two players stand on the same cell? Does landing on someone send them home?
Interviewer: Any number of players can share a cell. Nobody gets knocked off.
You: How do the rolls come in? Should the game roll the die itself?
Interviewer: In the real game, yes. But I want to test it, so make it possible to feed in the rolls.
The last answer shapes the whole design. A game that calls random.randint deep inside its move method cannot be tested: every run is different. We want the randomness to come from one replaceable place, and we want a way to say "player 2 rolled a 6" directly.
💡 Interview tip: Treat a die like a clock. Both are inputs from the outside world, and both make code untestable when they are hidden inside it. Ask early whether you may pass the roll in, and say why you are asking.
Final Requirements
- The board has cells
1tosize. Snakes and ladders are given as[start, end]pairs: a snake goes down, a ladder goes up. - Two to six players, numbered from 1. Everyone starts off the board at position
0. - Players move in order
1, 2, ..., n, then back to1. A move by the wrong player is rejected. - A move adds the roll (1 to 6) to the player's position. If that passes the last cell, the player stays put. Otherwise they move there, and if a snake or ladder starts there, on to its end.
- A roll of 6 gives the same player another turn, unless that move won.
- The first player to land exactly on the last cell wins, and the game is over: every later move is rejected.
- The rolls come from a dice object that can be swapped, and a move can also be made with a given roll.
Out of scope
- Drawing the board and its zigzag numbering.
- Generating a random board layout.
- Playing on after the first winner (we come back to this in Extensibility).
- Several games at once, networking and saving games.