Design snake and ladder
Design a snake and ladder game for several players on a 100-cell board.
Good first question for: Fresher
Requirements to confirm first
- Players take turns rolling a dice and move forward by the number rolled
- Landing on the bottom of a ladder moves the player up; landing on the head of a snake moves the player down
- The first player to reach the last cell wins; confirm that a roll which overshoots does not move the player
- Board size, snakes, ladders and the number of dice come from configuration
- Ask: does a six give another turn? Do the others keep playing after the first winner?
Write your own class list on paper before you open the answer key. Ten minutes of your own design is the practice; reading the key is only the check.
Show the answer key (write yours first)
Classes and what each one owns
- Game
- Holds the players in turn order and runs turns until someone wins
- Board
- Knows its size and where each jump starts and ends
- Jump
- One start cell and one end cell: a snake goes down, a ladder goes up
- Dice
- Returns a roll; the source of randomness is passed in
- Player
- A name and a current position
How they connect
- Game has a Board, a Dice and a queue of Players
- Board keeps jumps in a map from start cell to end cell, so the lookup after each move is constant time
- Snake and Ladder need no separate classes: both are a Jump, and the direction follows from the two numbers
Design patterns that fit, and why
- Strategy
- Dice behind an interface: one dice, two dice, or a fixed sequence for tests.
- Builder
- Board setup validates the layout once (no jump from the last cell, no two jumps from one cell, no loop), so the game loop can trust it.
Code sketch in Java
The core types and one real method. It is a sketch to talk through, not a full program.
interface Dice { int roll(); }
class Board {
private final int size;
private final Map<Integer, Integer> jumps; // start cell -> end cell
int next(int from, int roll) {
int to = from + roll;
if (to > size) return from; // overshoot: stay
return jumps.getOrDefault(to, to);
}
}
class Game {
private final Deque<Player> turns;
// take the first player, move, check for the last cell, put the player at the back
}Edge cases to raise yourself
- A ladder that ends on the head of a snake: chained jumps, and whether the rules allow them
- A snake and a ladder that start on the same cell: reject it at setup
- Overshooting the last cell
- Tests need a fixed sequence of rolls, not real randomness
Where answers usually go wrong
- Separate Snake and Ladder classes with the same two fields and no different behaviour. One Jump type is simpler, and the interviewer will ask why you need two.
- Calling the random generator directly inside Game. Then no test can replay a game; pass the dice in.
- Validating snakes and ladders during play instead of once at setup.
Follow-up questions
- Add a rule: three sixes in a row cancel the turn
- Let the game continue to rank second and third place
- Make it playable over a network: what becomes shared state?