Snakes and Ladders Game Engine

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-01-23

Problem Statement

Design and implement a Snakes and Ladders game engine that supports:


•Custom configuration of the game board and rules
•Turn-based multiplayer gameplay
•Special board features and overrides for testing


🔶 Functional Requirements:

1.Game Configuration (via JSON/YAML or input):
•Number of players N
•Board size BS x BS (default: 100 cells for a 10x10)
•Number of dice D (1 or more)
•Snakes: list of (head, tail) — where tail < head
•Ladders: list of (bottom, top) — where top > bottom
2.Gameplay Rules:
•Players roll dice in round-robin fashion.
•A player moves forward by the sum of all dice.
•Landing on:
•Snake head: player slides to tail.
•Ladder base: player climbs to top.
•Same cell as another player: other player resets to start (position 1).
•First player to reach or cross the last cell wins.
3.Manual Override:
•Allow input for:
•Initial player positions.
•Manual die rolls per turn.
•Useful for unit tests and edge case simulation.
4.Logging:
•Print/log every event:
•Die roll(s)
•Position changes
•Snakes/ladders triggered
•Player collisions
•Special tiles (if implemented)


🔷 Optional Extensions (At least one required):

•Random Board Generation:
•Based on S and L, generate random but valid snake and ladder positions.
•Special Tiles:
•Crocodile: move back 5 steps.
•Mine: player loses next 2 turns.
•Multi-die Support:
•Rolling multiple dice in a single turn.
•Unit Testing Framework:
•Cover edge cases: overlapping snakes/ladders, back-to-back effects, multiple players on same tile, winning condition, etc.


🔶 Design Expectations:

•Class design should follow OOP principles and be extensible:
•Classes: Game, Board, Player, Dice, Snake, Ladder, SpecialTile, GameDriver
•Use interfaces/abstract classes for extensibility (BoardObject, Effect)
•Support easy addition of new game mechanics or tiles
•Clear separation of concerns:
•Board state vs gameplay logic vs I/O
•Logic should be decoupled from input method (manual or random)


🔷 Constraints & Assumptions:

•Snake head and tail can't overlap with ladders or other snakes.
•Player moves cannot exceed board's last cell (unless allowed).
•Assume only one winner per game.


✅ Sample Use Case (from problem):

•Input:
•2 Players: Gaurav and Sagar
•Board: 10x10 (100 cells)
•Snakes and ladders loaded from input file
•Output:
•Turn-by-turn game log with all events
•Final winner announcement


Solution

Comments