livepython.net

260. Python Program to Play Snakes and Ladders with Given Dice Rolls

HardPhase 3 · Challenge

This is a longer program, but it uses only tools you know: a dictionary, a list, a for loop and split(). Play the first few rolls on paper first.

Overview

Board games are a good first step to game programming. The computer must follow every rule exactly, one move at a time, just like a careful player.

Problem Statement

Anya and Bruno play snakes and ladders on a small board with squares 1 to 30. Anya is Player 1 and Bruno is Player 2. A ladder takes you up. A snake takes you down.

  • Ladders: 3 to 22, 5 to 8, 11 to 26, 20 to 29.
  • Snakes: 17 to 4, 19 to 7, 21 to 9, 27 to 1.

The rules:

  • Both players start at square 0, before the board.
  • Player 1 uses the first roll, Player 2 the second roll, then Player 1 again, and so on. A 6 gives no extra turn.
  • Add the roll to your square. If you land on the bottom of a ladder or the head of a snake, you move to its other end.
  • If the roll would take you past 30, you stay where you are.
  • The first player to reach exactly 30 wins. Stop there, and ignore any rolls that are left.

Read the rolls. Print one line for each roll, in one of these formats:

  • Player <p> rolls <roll>: <from> to <to> for a normal move.
  • Player <p> rolls <roll>: <from> to <to>, ladder to <top> when the move ends on a ladder.
  • Player <p> rolls <roll>: <from> to <to>, snake to <tail> when the move ends on a snake.
  • Player <p> rolls <roll>: stays at <square> when the roll goes past 30.

Then print Player <p> wins, or No winner if the rolls run out first.

Examples

Example 1. Input:

3 5 6 3 5 1 2

gives:

Player 1 rolls 3: 0 to 3, ladder to 22
Player 2 rolls 5: 0 to 5, ladder to 8
Player 1 rolls 6: 22 to 28
Player 2 rolls 3: 8 to 11, ladder to 26
Player 1 rolls 5: stays at 28
Player 2 rolls 1: 26 to 27, snake to 1
Player 1 rolls 2: 28 to 30
Player 1 wins

Why: 28 + 5 = 33 is past 30, so Player 1 stays at 28. Later, 28 + 2 = 30 exactly, so Player 1 wins.

Example 2. Input:

6 1 6 4 5

gives:

Player 1 rolls 6: 0 to 6
Player 2 rolls 1: 0 to 1
Player 1 rolls 6: 6 to 12
Player 2 rolls 4: 1 to 5, ladder to 8
Player 1 rolls 5: 12 to 17, snake to 4
No winner

Why: Player 1 lands on the snake at 17 and slides to 4. Nobody reaches 30 before the rolls run out.

Input

One line of dice rolls, with one space between them. There is at least 1 roll, and each roll is a whole number from 1 to 6. Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Output

One line per roll that is played, then Player <p> wins or No winner. The tests try 8 different inputs, also a win by Player 2, rolls left over after a win, a single roll, and the snake at 27.

Try it first. The solution opens when you solve this question, or after 3 tries with Submit.

Finish this chapter firstChapter 13 · Dictionaries
solution.py
# write your code here

EXPECTED OUTPUT

1Player 1 rolls 3: 0 to 3, ladder to 22
2Player 2 rolls 5: 0 to 5, ladder to 8
3Player 1 rolls 6: 22 to 28
4Player 2 rolls 3: 8 to 11, ladder to 26
5Player 1 rolls 5: stays at 28
6Player 2 rolls 1: 26 to 27, snake to 1
7Player 1 rolls 2: 28 to 30
8Player 1 wins

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.