0% found this document useful (0 votes)
4 views4 pages

13 - Snake and Ladder

The document describes a Python implementation of the Snakes and Ladders game, where players roll dice to move across a board with defined snakes and ladders. The game continues until a player reaches or exceeds square 100, with messages indicating movements and interactions with snakes or ladders. It includes functions for rolling the dice and managing player positions on the board.

Uploaded by

hetavimodi2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

13 - Snake and Ladder

The document describes a Python implementation of the Snakes and Ladders game, where players roll dice to move across a board with defined snakes and ladders. The game continues until a player reaches or exceeds square 100, with messages indicating movements and interactions with snakes or ladders. It includes functions for rolling the dice and managing player positions on the board.

Uploaded by

hetavimodi2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

ASSIGNMENT :13

Input:

import random

# Define the board with snake and ladder positions


# Positive values represent ladders, negative values represent snakes
board = [i for i in range(101)]
# Sample snakes and ladders
snakes = {16: 6, 47: 26, 49: 11, 56: 53, 62: 19, 64: 60, 87: 24, 93: 73, 95: 75, 98: 78}
ladders = {1: 38, 4: 14, 9: 31, 21: 42, 28: 84, 36: 44, 51: 67, 71: 91, 80: 100}

for key, value in snakes.items():


board[key] = value
for key, value in ladders.items():
board[key] = value

def roll_dice():
# Ask the user to provide a dice value between 1 and 6
while True:
try:
roll = int(input("Enter your dice roll (1-6): "))
if 1 <= roll <= 6:
return roll
else:
print("Please enter a valid dice roll between 1 and 6.")
except ValueError:
print("Invalid input, please enter an integer between 1 and 6.")

def play_game():
position = 1
while position < 100:
print(f"\nYou are currently on square {position}")
dice_roll = roll_dice()
position += dice_roll

# Check if the player landed on a snake or ladder


if position < 100:
if board[position] != position:
if board[position] > position:
print(f"Yay! You climbed a ladder to square {board[position]}")
else:
print(f"Oops! You got bitten by a snake and are now on square {board[position]}")
position = board[position]

if position >= 100:


print("Congratulations! You have reached or passed square 100. You win!")
break

if __name__ == "__main__":
print("Welcome to Snakes and Ladders!")
play_game()

Output:

Welcome to Snakes and Ladders!

Player 1, you are currently on square 1


Enter your dice roll (1-6): 2
Player 1 is now on square 3

Player 2, you are currently on square 1


Enter your dice roll (1-6): 3
Yay! Player 2 climbed a ladder to square 14
Player 2 is now on square 14

Player 1, you are currently on square 3


Enter your dice roll (1-6): 5
Player 1 is now on square 8

Player 2, you are currently on square 14


Enter your dice roll (1-6): 3
Player 2 is now on square 17

Player 1, you are currently on square 8


Enter your dice roll (1-6): 2
Player 1 is now on square 10

Player 2, you are currently on square 17


Enter your dice roll (1-6): 6
Player 2 is now on square 23

Player 1, you are currently on square 10


Enter your dice roll (1-6): 2
Player 1 is now on square 12

Player 2, you are currently on square 23


Enter your dice roll (1-6): 5
Yay! Player 2 climbed a ladder to square 84
Player 2 is now on square 84

Player 1, you are currently on square 12


Enter your dice roll (1-6): 2
Player 1 is now on square 14

Player 2, you are currently on square 84


Enter your dice roll (1-6): 1
Player 2 is now on square 85

Player 1, you are currently on square 14


Enter your dice roll (1-6): 6
Player 1 is now on square 20
Player 2, you are currently on square 85
Enter your dice roll (1-6): 3
Player 2 is now on square 88

Player 1, you are currently on square 20


Enter your dice roll (1-6): 4
Player 1 is now on square 24

Player 2, you are currently on square 88


Enter your dice roll (1-6): 4
Player 2 is now on square 92

Player 1, you are currently on square 24


Enter your dice roll (1-6): 2
Player 1 is now on square 26

Player 2, you are currently on square 92


Enter your dice roll (1-6): 6
Oops! Player 2 got bitten by a snake and is now on square 78
Player 2 is now on square 78

Player 1, you are currently on square 26


Enter your dice roll (1-6): 1
Player 1 is now on square 27

Player 2, you are currently on square 78


Enter your dice roll (1-6): 6
Player 2 is now on square 84

Player 1, you are currently on square 27


Enter your dice roll (1-6): 5
Player 1 is now on square 32

Player 2, you are currently on square 84


Enter your dice roll (1-6): 4
Player 2 is now on square 88

Player 1, you are currently on square 32


Enter your dice roll (1-6): 3
Player 1 is now on square 35

Player 2, you are currently on square 88


Enter your dice roll (1-6): 6
Player 2 is now on square 94

Player 1, you are currently on square 35


Enter your dice roll (1-6): 4
Player 1 is now on square 39

Player 2, you are currently on square 94


Enter your dice roll (1-6): 6
Player 2 is now on square 100

Congratulations! Player 2 has reached or passed square 100. Player 2 wins!

You might also like