0% found this document useful (0 votes)
14 views6 pages

Dsa Miniproject

project

Uploaded by

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

Dsa Miniproject

project

Uploaded by

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

Program

import turtle

import random

# Setup screen

screen = turtle.Screen()
screen.setup(width=700, height=700)

screen.tracer(0)

# Instruction for the user

print("Press SPACEBAR to roll the dice. Player 1 starts!")

# Constants
cell_size = 30

start_x = -230

start_y = -170

# Draw board

board = turtle.Turtle()

board.hideturtle()
board.penup()

# Draw grid and numbers

number = 1

for row in range(10):

for col in range(10):

actual_col = col if row % 2 == 0 else 9 - col

x = start_x + actual_col * cell_size


y = start_y + row * cell_size
board.goto(x + 5, y + 5)

board.write(str(number), font=("Arial", 10, "normal"))

board.goto(x, y)

board.pendown()
for _ in range(4):

board.forward(cell_size)

board.left(90)

board.penup()

number += 1

# Coordinate helper
def get_coordinates(pos):
row = (pos - 1) // 10

col = (pos - 1) % 10

if row % 2 == 1:

col = 9 - col

x = start_x + col * cell_size + cell_size // 2

y = start_y + row * cell_size + cell_size // 2

return x, y

# Snakes and ladders

snakes = {99: 21, 95: 75, 62: 19, 46: 5}

ladders = {3: 22, 6: 25, 20: 38, 36: 57, 63: 81, 68: 85}

# Draw lines for snakes and ladders

def draw_line(from_pos, to_pos, color):


t = turtle.Turtle()

t.hideturtle()

t.penup()
t.pensize(4)

t.color(color)

t.goto(get_coordinates(from_pos))

t.pendown()
t.goto(get_coordinates(to_pos))

for start, end in ladders.items():

draw_line(start, end, "green")

for head, tail in snakes.items():

draw_line(head, tail, "red")

# Players
player1 = turtle.Turtle()

player1.shape("turtle")

player1.color("blue")

player1.penup()

player1.goto(get_coordinates(1))

player1_position = 1

player2 = turtle.Turtle()

player2.shape("turtle")

player2.color("red")

player2.penup()

player2.goto(get_coordinates(1))

player2_position = 1

# Current player

current_player = 1
# Dice roll function

def roll():

global player1_position, player2_position, current_player

if player1_position >= 100 or player2_position >= 100:


print("Game over!")

return

dice = random.randint(1, 6)

print(f"Player {current_player} rolled: {dice}")

if current_player == 1:

player1_position += dice
if player1_position > 100:
player1_position -= dice

print(f"Player 1's roll was too high! Stay at {player1_position}")

else:

if player1_position in snakes:

print("Player 1 was bitten by a snake!")

player1_position = snakes[player1_position]

elif player1_position in ladders:


print("Player 1 climbed a ladder!")

player1_position = ladders[player1_position]

player1.goto(get_coordinates(player1_position))

else:

player2_position += dice

if player2_position > 100:

player2_position -= dice
print(f"Player 2's roll was too high! Stay at {player2_position}")

else:

if player2_position in snakes:
print("Player 2 was bitten by a snake!")

player2_position = snakes[player2_position]

elif player2_position in ladders:

print("Player 2 climbed a ladder!")


player2_position = ladders[player2_position]

player2.goto(get_coordinates(player2_position))

# Check win

if player1_position == 100:

print(" Player 1 wins the game!")

elif player2_position == 100:

print(" Player 2 wins the game!")

# Switch turns

current_player = 2 if current_player == 1 else 1

screen.update()

# Bind space to roll

screen.listen()

screen.onkey(roll, "space")

screen.update()
turtle.done()
Output:-
Press SPACEBAR to roll the dice. Player 1 starts!

Player 1 rolled: 3

Player 2 rolled: 1

Player 1 rolled: 6

Player 2 rolled: 1
Player 2 climbed a ladder!

You might also like