Problem F
Snakes and Ladders
Write a program that plays the legendary board game, Snakes and Ladders.
The game is played on a board with exactly $100$ squares, numbered from $1$ to $100$.
The rules of the game are as follows:
-
In each turn, a player rolls a die and moves forward by the same number of squares as indicated by the die.
-
After having moved, if the player is located at the bottom of a ladder, the player moves up the ladder. If a player is instead located at the head of a snake, the player moves down to the tail of the snake.
-
If a player rolls $6$, then that player gets to roll the die again. It doesn’t matter if the player just moved up a ladder or down a snake, they are still entitled to another roll.
-
The player who is first to reach the last square wins.
-
If a player is close to the last square and overshoots by rolling a number higher than required, then the player still moves to square $100$ and wins the game.
The starter code includes a main() function and a function named roll_die() that shall be used to simulate the rolling of a single $6$-sided die.
Input
First the program should prompt the user for $n$, the number of snakes and ladders on the board.
The second part of the input contains $n$ lines, where each line $i$ contains two integers $s_i$ and $e_i$, separated by a space, indicating which squares on the board are connected by snakes or ladders, where $s_i$ designates the starting square and $e_i$ the end square of a particular connection.
If $s_i > e_i$ then the pair is considered to be a snake, but if $e_i > s_i$ then it will be a ladder.
Lastly the program should prompt the user for two names, $p_1$ and $p_2$, the names of Player $1$ and Player $2$. The program then proceeds to play the game following the rules indicated above. Player $1$ starts the game.
In the tests $n$ will be restricted to $1 \le n \le 49$. It is guaranteed that no snake/ladder will overlap one another. The names $p_1$ and $p_2$ will be of length $1 \le |p_1|, |p_2| \le 10$.
Output
For each turn of the game the program should output the rolls and locations of the players as such:
-
“{$p$} rolled {$r$} and is now at {$d$}.”
where $p$ is the name of the given player, $r$ is the number that came up on the die, and $d$ is the number of the destination square where the player ended up after moving according to the die. Additionally it should display one of these messages when players land on a start of a snake or a ladder:
-
“Darn! A snake brought {$p$} down to square {$d'$}.”
-
“Splendid! {$p$} climbed a ladder up to square {$d'$}.”
where $d'$ is the square the snake or ladder took the player to. When the game is over, the program should output
-
“{$p$} won the game.”
Sample Input 1 | Sample Output 1 |
---|---|
9 21 75 30 60 7 18 67 30 85 94 89 60 50 75 11 26 52 16 Joseph Donald |
Joseph rolled 5 and is now at square 5. Donald rolled 5 and is now at square 5. Joseph rolled 6 and is now at square 11. Splendid! Joseph climbed a ladder up to square 26. Joseph rolled 3 and is now at square 29. Donald rolled 5 and is now at square 10. Joseph rolled 5 and is now at square 34. Donald rolled 6 and is now at square 16. Donald rolled 2 and is now at square 18. Joseph rolled 3 and is now at square 37. Donald rolled 4 and is now at square 22. Joseph rolled 6 and is now at square 43. Joseph rolled 3 and is now at square 46. Donald rolled 3 and is now at square 25. Joseph rolled 4 and is now at square 50. Splendid! Joseph climbed a ladder up to square 75. Donald rolled 6 and is now at square 31. Donald rolled 2 and is now at square 33. Joseph rolled 6 and is now at square 81. Joseph rolled 3 and is now at square 84. Donald rolled 1 and is now at square 34. Joseph rolled 4 and is now at square 88. Donald rolled 6 and is now at square 40. Donald rolled 4 and is now at square 44. Joseph rolled 1 and is now at square 89. Darn! A snake brought Joseph down to square 60. Donald rolled 5 and is now at square 49. Joseph rolled 6 and is now at square 66. Joseph rolled 6 and is now at square 72. Joseph rolled 3 and is now at square 75. Donald rolled 4 and is now at square 53. Joseph rolled 4 and is now at square 79. Donald rolled 6 and is now at square 59. Donald rolled 3 and is now at square 62. Joseph rolled 6 and is now at square 85. Splendid! Joseph climbed a ladder up to square 94. Joseph rolled 1 and is now at square 95. Donald rolled 3 and is now at square 65. Joseph rolled 5 and is now at square 100. Joseph won the game. |