← Back to problems

6. Snake and Ladder Engine

EASY
DESIGNGameLLDState Machine

Drive a deterministic Snake & Ladder game from an explicit sequence of dice rolls.

Simulate Snake & Ladder on a board of cells 1..N. Players start off-board at position 0 and take turns in registration order. Dice values are given explicitly, so the game is deterministic. Commands: - INIT <N> : first line; the winning cell. - LADDER <from> <to> : a ladder from a lower cell to a higher cell (from < to). - SNAKE <from> <to> : a snake from a higher cell to a lower cell (from > to). - PLAYER <name> : register a player; registration order is turn order. - ROLL <value> : the next player (in turn order) moves forward by <value>: - If the move would go past cell N, the player does NOT move; print "<name> STAY <pos>" and the turn passes. - Otherwise move to the landing cell, then follow any snake or ladder at that cell, chaining repeatedly if the new cell also has one. - If the final cell is exactly N, print "<name> WINS" and the game ends. - Otherwise print "<name> -> <finalPos>" and the turn passes. After the game has ended, every further ROLL prints "GAMEOVER".
Log in to submit a solution

Comments

Log into join the discussion.