Snakes & Ladders
Description:
This Kata is like the game of Snakes & Ladders
There is an array representing the squares on the game board.
The starting
square is at array element 0. The final
square is the last array element.
At each "turn" you move forward a number of places (according to the next dice throw).
The value at the square you end up on determines what happens next:
0
Stay where you are (until next turn)+n
This is a "ladder". Go forward n places-n
This is a "snake". Go back n places
Each snake or ladder will always end on a square with a 0, so you will only go up or down one at a time.
There are no ladders on the starting
square, and there are no snakes on the final
square.
Rules
- You are given a number of dice throws. The game continues until either:
- You have no throws left, OR
- You end up exactly on the final square
At each turn, make your move, then go up the "ladders" and down the "snakes" as appropriate.
If the dice roll overshoots the final square then you cannot move. Roll the dice again.
Task
Return the index of the array element that you ended up on at the end of the game.
Example
Start
Dice: [2, 1, 5, 1, 5, 4]
Board: [0, 0, 3, 0, 0, 0, 0, -2, 0, 0, 0]
Roll a 2
. Move forward 2 squares, then go up the ladder (+3)
Dice: [2, 1, 5, 1, 5, 4] Board: [0, 0, 3, 0, 0, 0, 0, -2, 0, 0, 0] Board: [0, 0, 3, 0, 0, 0, 0, -2, 0, 0, 0]
Roll a 1
. Move forward 1 square
Dice: [2, 1, 5, 1, 5, 4] Board: [0, 0, 3, 0, 0, 0, 0, -2, 0, 0, 0]
Roll a 5
. Can't move
Dice: [2, 1, 5, 1, 5, 4] Board: [0, 0, 3, 0, 0, 0, 0, -2, 0, 0, 0]
Roll a 1
. Move forward 1 square, then go down the snake (-2)
Dice: [2, 1, 5, 1, 5, 4] Board: [0, 0, 3, 0, 0, 0, 0, -2, 0, 0, 0] Board: [0, 0, 3, 0, 0, 0, 0, -2, 0, 0, 0]
Roll a 5
. Move forward 5 squares
Dice: [2, 1, 5, 1, 5, 4] Board: [0, 0, 3, 0, 0, 0, 0, -2, 0, 0, 0]
You are on the final square so the game ends. Return 10
:-)
Similar Kata:
Stats:
Created | Nov 8, 2016 |
Published | Nov 15, 2016 |
Warriors Trained | 2324 |
Total Skips | 87 |
Total Code Submissions | 5518 |
Total Times Completed | 1054 |
Java Completions | 287 |
JavaScript Completions | 348 |
C# Completions | 128 |
Go Completions | 78 |
Python Completions | 262 |
Scala Completions | 8 |
Total Stars | 56 |
% of votes with a positive feedback rating | 94% of 204 |
Total "Very Satisfied" Votes | 180 |
Total "Somewhat Satisfied" Votes | 24 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 11 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 6 kyu |
Lowest Assessed Rank | 7 kyu |