Design Snake & Ladder game

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-06-07

Problem Statement

Design and implement a Snake & Ladder game system that manages the game board, player movements, snakes, ladders, and game rules. Focus on object modeling, turn-based gameplay, and extensibility.


Functional Requirements (FR)


Game Setup


•Define board size (e.g., 100 cells).
•Place snakes and ladders at specific positions:
•Snakes: move the player down from head to tail.
•Ladders: move the player up from bottom to top.
•Support 2 or more players.


Gameplay


•Players take turns to roll a dice (1–6).
•Move the player based on dice outcome.
•Handle encounters with snakes and ladders:
•If landing on a snake’s head → move down to tail.
•If landing on a ladder’s bottom → climb to top.
•First player to reach the final cell wins the game.


Dice


•Randomized dice rolls between 1–6.
•Optionally support multiple dice.


Game State & Rules


•Track player positions.
•Handle invalid moves (e.g., overshooting final cell).
•Announce winner and game completion.


Optional Features


•Undo/Redo moves.
•Save and load game state.
•Multiple game boards with different snake/ladder configurations.


Non-Functional Requirements (NFR)


•Extensibility → Support additional rules or multiplayer modes.
•Performance → Minimal latency for dice roll and move computation.
•Scalability → Handle multiple concurrent games.
•Maintainability → Easy to add new board configurations or features.


Core Entities & Attributes


Player


•playerId, name, currentPosition


Board


•size → Total number of cells.
•cells[] → Array representing cells (can store snake/ladder info).


Snake / Ladder


•startPosition, endPosition


Dice


•sides → Number of dice sides (default 6)
•roll(): int → Returns random value between 1 and 6


GameManager


•players[] → List of players
•currentPlayerIndex → Tracks turn
•board → Reference to the board
•gameStatus → Ongoing / Completed
•winner → Player who won


Core Components


Board

•Stores snake and ladder positions.
•Provides methods to check if a cell has a snake or ladder.

Dice

•Generates random numbers to simulate dice rolls.

Player

•Holds current position and player metadata.

GameManager

•Handles turns, moves, dice rolls, snake/ladder checks, and game completion.


APIs / Operations


Game Management APIs


•startGame(players: List<Player>) → Initialize game state.
•rollDice(playerId) → Returns dice value for the current player.
•movePlayer(playerId, diceValue) → Updates player position and applies snakes/ladders.
•getGameStatus() → Returns ongoing/completed status, player positions, winner.
•getCurrentPlayer() → Returns player whose turn it is.


LLD Design Goals


•Class Diagram → GameManager, Board, Player, Dice, Snake, Ladder.


Design Patterns:


•Singleton (Optional) → Single instance of GameManager per game.
•Observer (Optional) → Notify players of moves or game events.
•Trade-offs →1. Using array vs. map for board representation.2. Predefined snakes/ladders vs. dynamic/random placement.


Solution

Comments