# Snakes and Ladders using Python

Snakes and ladders is a board game for two or more players and is played worldwide. It originated in ancient India as “Moksha Patam”, and was brought to the UK in the 1890s. It is a race game where each player progresses through the 100 numbered cells of the board by rolling a dice. When a player lands at the bottom of the ladder, they can automatically move their token to the top of the ladder. However, when a player lands in the mouth of a snake, they slide down to the tail of the snake.

The first player to reach the last cell of the board (cell 100), they win. However they have to land exactly on this cell. If they reach a number higher than 100, then their token bounce back. For instance, if a player is on cell 98 and rolls a 6, they move as follows: 98 >> 99 >> 100 >> 99 >> 98 >> 97 >> 96.

Another rule is that when a player rolls a 6, they can have another turn.

We have started to implement the game using Python code. However our code is incomplete. For instance, we our game is only for one player. We dd not implement all the ladders and snakes of the board. And we did not check if the player is allowed another go (when rolling a 6).

After checking and testing the code provided below, you will need to complete 3 tasks to finalise this code:

Add some Python code (from line 55) to make sure all ladders and snakes are catered for.
Add some Python code to allow the player to have another go when they roll a 6.
Add some Python code to allow add a second player to the game.

#### Python code so far…

[pms-restrict display_to=”not_logged_in” message=” “]

#### Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
[/pms-restrict] [pms-restrict subscription_plans=”14217″ display_to=”not_subscribed” message=” “]

#### Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
[/pms-restrict] [pms-restrict subscription_plans=”14217″ message=” “]

#### Solution...

You are viewing this solution as part of your full membership subscription!

main.py

#### Python Code

```#Snakes and Ladders using Python - www.101computing.net/snakes-and-ladders-using-python/
import turtle
import random

screen = turtle.Screen()

def movePlayer(player, position):
yPos = (position-1) // 10
xPos = (position-1) % 10
y = -170 + yPos * 37
if yPos%2 == 0:
x = -170 + xPos * 38
else:
x = 170 - xPos * 38
if player==player1:
player.goto(x,y)
else:
player.goto(x+3,y-3)

# Setup the Snakes & Ladders board
screen.setup(400, 400)

# Initialise first player...
player1 = turtle.Turtle()
player1.shape("circle")
player1.speed(5)
player1.color("#810081")
player1.pensize(15)
player1.penup()

# Initialise first player...
player2 = turtle.Turtle()
player2.shape("circle")
player2.speed(5)
player2.color("#FFA500")
player2.pensize(15)
player2.penup()

# Position the first player to the bottom left corner of the board (Position 1)
player1Position = 1
movePlayer(player1, player1Position)
player2Position = 1
movePlayer(player2, player2Position)

gameOver=False
while not gameOver:

print(">>> Player 1:")
input("Press enter to throw the dice...")
#Throw the dice...
playAgain = True
while playAgain:
dice = random.randint(1,6)
print("You've rolled a " + str(dice))

#Calculate the new position of the player based on the dice value
player1Position = player1Position + dice
movePlayer(player1, player1Position)

#Check if the player lands on a ladder (to climb up) or on a snake (slide down)
if player1Position == 5:
player1Position = 35
elif player1Position == 8:
player1Position = 13
elif player1Position == 11:
player1Position = 52
elif player1Position == 21:
player1Position = 41
elif player1Position == 36:
print("Sliding down the snake!")
player1Position = 22
elif player1Position == 41:
print("Sliding down the snake!")
player1Position = 63
elif player1Position == 59:
player1Position = 83
elif player1Position == 72:
player1Position = 91
elif player1Position == 84:
print("Sliding down the snake!")
player1Position = 66
elif player1Position == 85:
player1Position = 96
elif player1Position == 93:
print("Sliding down the snake!")
player1Position = 86

if player1Position>100:
#Bounce back
player1Position = 100 - (player1Position-100)

# Move player on the board to the new position
print("Player 1 position: " + str(player1Position))
movePlayer(player1, player1Position)

playAgain = False
# Have we got a winner?
if player1Position == 100:
print("---- Player 1 wins!!! ----")
gameOver = True
break
else:
if dice == 6:
print("You rolled a 6. Your turn again...")
playAgain=True

print(">>> Player 2:")
input("Press enter to throw the dice...")
#Throw the dice...
playAgain = True
while playAgain:
dice = random.randint(1,6)
print("You've rolled a " + str(dice))

#Calculate the new position of the player based on the dice value
player2Position = player2Position + dice
movePlayer(player2, player2Position)

#Check if the player lands on a ladder (to climb up) or on a snake (slide down)
if player2Position == 5:
player2Positon = 35
elif player2Position == 8:
player2Position = 13
elif player2Position == 11:
player2Position = 52
elif player2Position == 21:
player2Position = 41
elif player2Position == 36:
print("Sliding down the snake!")
player2Position = 22
elif player2Position == 41:
print("Sliding down the snake!")
player2Position = 63
elif player2Position == 59:
player2Position = 83
elif player2Position == 72:
player2Position = 91
elif player2Position == 84:
print("Sliding down the snake!")
player2Position = 66
elif player2Position == 85:
player2Position = 96
elif player2Position == 93:
print("Sliding down the snake!")
player2Position = 86

if player2Position>100:
#Bounce back
player2Position = 100 - (player2Position-100)

# Move player on the board to the new position
print("Player 1 position: " + str(player2Position))
movePlayer(player2, player2Position)

playAgain = False
# Have we got a winner?
if player2Position == 100:
print("---- Player 2 wins!!! ----")
gameOver = True
break
else:
if dice == 6:
print("You rolled a 6. Your turn again...")
playAgain=True
```
[/pms-restrict]

Did you like this challenge?

Click on a star to rate it!

Average rating 3.9 / 5. Vote count: 43

No votes so far! Be the first to rate this post.

As you found this challenge interesting...