Low-Level Design Tutorial 0/42 lessons ~6 min read Lesson 38

    Snake and Ladder Game

    Snake and Ladder is a compact LLD problem testing game board modeling, player movement, jump rules, win detection, and turn management — common as a lighter interview or warm-up.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Snake and Ladder is a compact LLD problem testing game board modeling, player movement, jump rules, win detection, and turn management — common as a lighter interview or warm-up.

    Classes: GameBoard, Player, Dice, SnakeAndLadderGame (or GameController). Board holds size and jump map; players take turns rolling dice until one reaches exactly the last cell.

    Extensions: multiple dice, two-player vs n-player, bi-directional snakes.

    Understanding the topic

    Key concepts

    • Board: size N, map from cell to destination (snake or ladder).
    • Player: name, current position.
    • Dice: random 1–6 (injectable for tests).
    • Win: exact landing on size required.
    • Turn order rotation among players.
    • Immutable board config after game start.

    Step-by-step explanation

    1. Initialize board with snakes and ladders.
    2. Register players at position 1.
    3. Active player rolls dice.
    4. Advance position; apply jump if mapped.
    5. Check win; else next player.
    6. Reject move overshooting final cell (optional rule).

    Informative example

    Full game with board, players, dice, and turn loop:

    java
    public interface Dice {
    int roll();
    }
    public final class RandomDice implements Dice {
    private final Random random = new Random();
    @Override public int roll() { return random.nextInt(6) + 1; }
    }
    public final class Player {
    private final String name;
    private int position;
    public Player(String name) {
    this.name = name;
    this.position = 1;
    }
    public String name() { return name; }
    public int position() { return position; }
    void moveTo(int cell) { position = cell; }
    }
    public final class GameBoard {
    private final int size;
    private final Map<Integer, Integer> jumps;
    public GameBoard(int size, Map<Integer, Integer> jumps) {
    this.size = size;
    this.jumps = Map.copyOf(jumps);
    }
    public int size() { return size; }
    public int resolve(int from, int steps) {
    int target = from + steps;
    if (target > size) return from;
    return jumps.getOrDefault(target, target);
    }
    }
    public final class SnakeAndLadderGame {
    private final GameBoard board;
    private final List<Player> players;
    private final Dice dice;
    private int turnIndex;
    public SnakeAndLadderGame(GameBoard board, List<Player> players, Dice dice) {
    this.board = board;
    this.players = List.copyOf(players);
    this.dice = dice;
    }
    public Optional<Player> playTurn() {
    Player current = players.get(turnIndex);
    int roll = dice.roll();
    int next = board.resolve(current.position(), roll);
    current.moveTo(next);
    turnIndex = (turnIndex + 1) % players.size();
    if (next == board.size()) return Optional.of(current);
    return Optional.empty();
    }
    }

    FixedDice implementation enables deterministic tests; Builder configures snakes/ladders on board.

    Real-world use

    Real-world applications

    • Lightweight OOD warm-up interview.
    • Teaching state and turn management.
    • Practice simple class collaboration.

    Best practices

    • Inject Dice for testability.
    • Immutable jump map on board.
    • Exact win rule configurable.
    • Separate board config from game session.
    • Log each turn for replay/debug.

    Common mistakes

    • Allowing move past final cell without bounce-back rule clarity.
    • Snake and ladder on same cell undefined behavior.
    • Mutating shared board mid-game.
    • No turn order — simultaneous moves.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    5 questions
    1BeginnerQuestionCore snake and ladder classes?+

    Answer

    GameBoard, Player, Dice, Game controller.
    2BeginnerQuestionApply snake after move?+

    Answer

    resolve() adds steps then checks jumps map for destination.
    3IntermediateQuestionExact win rule?+

    Answer

    If from + roll > size, stay put — common house rule.
    4IntermediateQuestionTwo snakes same cell?+

    Answer

    Define precedence or disallow at board build validation.
    5AdvancedQuestionAdd undo last move.+

    Answer

    Command history stack with MoveCommand storing prior positions.

    Summary

    Board holds size and jump mappings. Players rotate turns rolling dice. resolve applies movement and snakes/ladders. Win when landing exactly on final cell. Inject Dice for deterministic tests.

    Ready to mark this lesson complete?Track your journey across the entire course.