Implement a function called SnakesLadders
to play a two-player game of Snakes and Ladders. The function should keep track of player turns and player position on the board (of the 100 spaces). SnakesLadders should call a method play(die1, die2)
independently of the state of the game or the player turn. These variables are the die thrown in a turn and are both integers between 1 and 6. The player moves the sum of die1 and die 2.
Rules
- There are two players and both start off the board on square 0.
- Player 1 starts and alternates with player 2.
- You follow the numbers up the board in order 1=>100
- If the value of both die are the same then that player will have another go.
- Climb up ladders. The ladders on the game board allow you to move upwards and get ahead faster. If you land exactly on a square that shows an image of the bottom of a ladder, then you may move the player all the way up to the square at the top of the ladder. (even if you roll a double).
- Slide down snakes. Snakes move you back on the board because you have to slide down them. If you land exactly at the top of a snake, slide move the player all the way to the square at the bottom of the snake or chute. (even if you roll a double).
- Land exactly on the last square to win. The first person to reach the highest square on the board wins. But there's a twist! If you roll too high, your player "bounces" off the last square and moves back. You can only win by rolling the exact number needed to land on the last square. For example, if you are on square 98 and roll a five, move your game piece to 100 (two moves), then "bounce" back to 99, 98, 97 (three, four then five moves.)
Return Player n Wins!
. Where n is winning player that has landed on square 100 without any remaining moves left.
Return Game over!
if a player has won and another player tries to play.
Otherwise return Player n is on square x.
Where n is the current player and x is the square they are currently on.
Good luck!
This challenge comes from adrian.eyre on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (5)
Scala
Instead of saving the state internally, the
play
function receives a state object and returns a new state object to keep it pure.which language is this
It was fun
Vanilla JS
It was fun doing it. Will appreciate any feedback.
gist.github.com/FCO/9d330adcb072a5...