Dsa Mini Project
Dsa Mini Project
Implementation Overview
The Snake and Ladder game is implemented in Python using the Tkinter library for GUI
development. The game board is designed as a 10×10 grid using a canvas, with each cell
numbered from 1 to 100 in a zig-zag manner. Snakes and ladders are represented using
dictionaries that map start and end positions. Visually, ladders are drawn as green straight
lines, while snakes are shown as red wavy curves for a more realistic effect. Player
movements are controlled via a dice roll, and tokens are dynamically updated on the board.
The game supports two players taking alternate turns until one reaches position 100 to win.
Interactive elements such as pop-up messages for dice rolls, snake bites, and ladder climbs
enhance user engagement.
Code :
import tkinter as tk
from tkinter import messagebox
import random
# Snakes and Ladders positions
snakes = {16: 6, 48: 30, 64: 60, 79: 19, 93: 68, 95: 24, 97: 76, 98: 78}
ladders = {1: 38, 4: 14, 9: 31, 21: 42, 28: 84, 36: 44, 51: 67, 71: 91, 80: 100}
class Player:
def __init__(self, name, color):
self.name = name
self.position = 0
self.color = color
class SnakeLadderGame:
def __init__(self, root):
self.root = root
self.root.title("Snake and Ladders Game")
self.root.geometry("600x700")
self.canvas = tk.Canvas(root, width=500, height=500, bg='white')
self.canvas.pack(pady=20)
self.status_label = tk.Label(root, text="", font=("Arial", 14))
self.status_label.pack()
self.roll_button = tk.Button(root, text="Roll Dice", font=("Arial", 14),
command=self.play_turn)
self.roll_button.pack(pady=10)
self.players = [Player("Player 1", "red"), Player("Player 2", "blue")]
self.current_turn = 0
self.player_tokens = {}
self.draw_board()
self.draw_snakes_and_ladders()
self.init_tokens()
self.update_status()
def draw_board(self):
size = 50
for row in range(10):
for col in range(10):
x1 = col * size
y1 = row * size
x2 = x1 + size
y2 = y1 + size
number = 100 - (row * 10 + col) if row % 2 == 0 else 100 - (row * 10 + (9 - col))
self.canvas.create_rectangle(x1, y1, x2, y2, fill="lightyellow", outline="black")
self.canvas.create_text(x1 + 25, y1 + 25, text=str(number), font=("Arial", 10))
def get_coords(self, position):
if position < 1 or position > 100:
return -100, -100
size = 50
pos = position - 1
row = 9 - (pos // 10)
col = pos % 10 if ((pos // 10) % 2 == 0) else 9 - (pos % 10)
x = col * size + 25
y = row * size + 25
return x, y
def draw_snakes_and_ladders(self):
# Draw ladders (green arrows)
for start, end in ladders.items():
x1, y1 = self.get_coords(start)
x2, y2 = self.get_coords(end)
self.canvas.create_line(x1, y1, x2, y2, fill="green", width=4, arrow=tk.LAST)
self.canvas.create_text((x1 + x2) // 2, (y1 + y2) // 2, text="🪜", font=("Arial", 16))
Snapshots :