Snake & Ladder
Board-game rules as an engine: turn rotation, overshoot, snakes, ladders, and a win condition — with the dice injected so every game is testable.
Requirements
- Board cells 1–100; players start at 0 (off-board) and need to land on exactly 100 to win.
- Overshoot rule: if position + roll > 100 the player does not move (but the turn still passes).
- Jumps trigger only on the exact landing cell and never chain (guaranteed by setup: no jump lands on another jump's trigger).
- Turn order is strict round-robin P1 → P2 → … → Pk → P1; a win freezes the game.
- The dice value is an input, not generated inside the class.
API to implement
Implement the class SnakeLadder. The I/O driver is already written in the starter code — it parses the command script below and calls your methods. You only implement the class.
| Command | Returns | Behavior |
|---|---|---|
| add_snake <head> <tail> | — | Add a snake: landing exactly on head slides down to tail (tail < head). Called only before the first roll; no cell hosts two jumps. |
| add_ladder <bottom> <top> | — | Add a ladder: landing exactly on bottom climbs to top (top > bottom). Same setup guarantees. |
| roll <value> | string | The current player rolls value (1–6). Overshooting 100 → stay put. Land on a snake head / ladder bottom → jump once (jumps never chain). Reaching exactly 100 wins: return P<i> WINS and end the game (later rolls return GAME_OVER). Otherwise return P<i> <newPosition> and pass the turn. |
Input format
The first line contains Q, the number of operations. The second line contains the constructor argument: numPlayers — players P1…Pk, all starting off-board at position 0; P1 moves first. Each of the next Q lines is one command as listed above. String arguments contain no spaces.
Output format
For each command with a non-void return, print its result on its own line, in order. The driver does this for you — your methods just return values.
This is a design problem: the hidden tests exercise edge cases and interaction sequences, not performance tricks. Model the state cleanly and the tests will pass.
Sample cases
7
2
add_ladder 4 98
add_snake 99 2
roll 4
roll 6
roll 2
roll 5
roll 6P1 98
P2 6
P1 WINS
GAME_OVER
GAME_OVER3
2
add_ladder 3 100
roll 3
roll 1P1 WINS
GAME_OVER