Design a Snake & Ladder Game

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2020-10-20

Problem Statement

Design and implement the Snake & Ladder game that allows multiple players to play on a board with snakes and ladders. Players roll dice to move, and the system updates positions according to game rules until a winner is declared.


Functional Requirements (FR)


Board Setup


•Represent the game board with numbered cells (e.g., 1 to 100).
•Place snakes (head → tail) and ladders (start → end) at predefined positions.


Player Management


•Support 2 or more players.
•Each player starts at cell 0 (outside the board).


Dice Roll


•Players roll a dice (1–6).
•Move player forward by dice outcome.


Snake & Ladder Rules


•If a player lands on the head of a snake, they move down to its tail.
•If a player lands at the base of a ladder, they move up to its top.


Winning Condition


•First player to exactly reach the last cell (e.g., 100) wins.
•If dice roll overshoots the last cell, the player stays in place.


Turn Management


•Players take turns in sequence.
•Game continues until a winner is declared.


Non-Functional Requirements (NFR)


•Extensibility: Should allow different board sizes and custom snake/ladder configurations.
•Fairness: Random dice rolls should be unbiased.
•Scalability: Should support multiple players smoothly.


Core Entities & Attributes


•Board → size, cells, snakes (map head→tail), ladders (map start→end).
•Cell → position, type (normal, snake, ladder).
•Dice → roll() → returns [1–6].
•Player → playerId, name, currentPosition.
•Game → players, board, dice, currentTurn, winner.


Example Use Case


1.Board size = 100, 2 players, 3 snakes, 2 ladders.
2.Player 1 rolls 4 → moves to cell 4.
3.Player 2 rolls 6 → moves to cell 6 → climbs ladder to cell 15.
4.Player 1 rolls 2 → moves to cell 6 → climbs ladder to 15.
5.Later, Player 2 lands on snake at 62 → goes down to 19.
6.Game continues until a player reaches 100 exactly.


Evaluation Criteria


•Well-defined class design (Game, Board, Player, Dice, Snake, Ladder).
•Handling of game rules (snakes, ladders, exact win condition).
•Proper turn management among players.
•Extensible to add features like multiple dice or special rules.


Solution

Comments