0% found this document useful (0 votes)
53 views14 pages

Mini Project

The document describes implementing a Snake and Ladder game in Python. It outlines creating Player and MovingEntity classes to track game state. The objective is a fun, engaging Python implementation of the classic board game, focusing on object-oriented principles and providing experience with classes, methods and game logic.

Uploaded by

adityapatil8904
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)
53 views14 pages

Mini Project

The document describes implementing a Snake and Ladder game in Python. It outlines creating Player and MovingEntity classes to track game state. The objective is a fun, engaging Python implementation of the classic board game, focusing on object-oriented principles and providing experience with classes, methods and game logic.

Uploaded by

adityapatil8904
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/ 14

Mini Project on

“ Snake and Ladder Game ”

A Report Submitted for a mini project for Data Structures and


Algorithms lab in 4th Semester of Second Year Artificial Intelligence &
Data Science

Second Year(Artificial Intelligence & Data Science)

Academic Year 2023 - 24

Submitted by -
Sr.no Name of the student Roll No.
1

Zeal Education Society's


Zeal College of Engineering & Research
Narhe, Pune – 411041
Department of Artificial Intelligence & Data Science
Zeal Education Society's
Zeal College of Engineering & Research
Narhe, Pune – 411041
Department of Artificial Intelligence & Data Science

CERTIFICATE

Certified that the project entitled “Snake And Ladder Game” is a


bonafide work carried out by .
It is certified that all corrections/suggestions indicated for Internal
Assignment have been incorporated in the report.
The project report has been approved as it satisfies the academic
requirements in respect of Project work prescribed for the Bachelor of
Engineering Degree

Prof. Kalpana Sonval Prof. Dikshendra D. Sarpate


(Project Guide) (HOD)
ACKNOWLEDGEMENT

We take this opportunity to thank our project guide Prof. Kalpana Sonval
mam and Head of the department Prof. Dikshendra Sir for their valuable
guidance and for providing all the necessary facilities, which were
indispensable in the completion of this project report.
We are also thankful to all the staff members of Artificial Intelligence &
Data Science Department for their valuable time, support, comments,
suggestions and persuasion. We would also like to thank the institute for
providing the required facilities, Internet access and important books.
Index

Sr.No. Content Page No.

1 Abstract 5

2 Software Requirement 6
3 Introduction 7
4 Problem Statement 8
5 Objective & Outcome 8
6 Implementation 9
7 Outcome 14
8 Conclusion 14
Abstract

Snakes and Ladders is a classic board game played by two or more players,
typically on a 10x10 grid of numbered squares. The objective is to navigate
one's game piece from the start (bottom square) to the finish (top square),
helped or hindered by ladders and snakes, respectively .

Players take turns rolling a dice and moving their token forward by the number
that appears on the top of the dice.

If a player lands on a square with the base of a ladder, they must climb the
ladder to the square at the top of the ladder.
If a player lands on the square in which there is a mouth of a snake, they must
slide down to the square which is at the end of the snake.

The game terminates until one player reaches the final square.
The game has been used in teaching various subjects, such as mathematics,
social studies, and oral health, due to its educational effectiveness in
improving students' knowledge, attitude, and behavior.
The game can be implemented using a six-faced die and a square board with
a series of numbered squares arranged in a grid.
The players use different color tokens to represent themselves.
The game can be enhanced by adding elements such as cards containing
questions, science cards, bonus cards, surprise questions, and zonk cards.
SOFTWARE AND HARDWARE REQUIREMENT

Software Requirements :
1. C++ Compiler: The program is written in C++, so a C++ compiler is
necessary.
Recommended compilers include:
● GCC (GNU Compiler Collection)
● Clang
● Microsoft Visual C++
2. Operating System : The Progarm can run on various operating systems
Including:
● Windows
● Linux
● macOS
3. Text Editor Or IDE : A text editor or integrated development environment is
needed for editing and compiling the source code. Popular choices include:
● Visual Studio Code
● Code :: Blocks
● Ecilpse
● Microsoft Visual Studio

Hardware Requirements :

1. Processor: Any modern processor capable of running the chosen operating


system and compiler will suffice. A multi-core processor may provide better
performance for compiling and running the program.

2. Memory (RAM): At least 2 GB of RAM is recommended for smooth


compilation and execution of the program.

3. Storage: Minimal storage space is required for storing the source code,
executable files, and any necessary libraries. A few megabytes should be
sufficient.
4. Display: A standard monitor or display screen to interact with the program
through the user interface.

5. Input Devices: A keyboard and mouse (or equivalent input devices) are
required for user interaction with the program
Introduction

Snakes and Ladders is a classic board game that has been enjoyed by
children and adults alike for generations. Originating in ancient India as
"Moksha Patam," the game has evolved over time and spread globally,
captivating players with its simple yet engaging gameplay. The game is played
on a grid of numbered squares, with players aiming to navigate their pieces
from the start to the finish, encountering ladders that help them advance and
snakes that hinder their progress .

Background
Originally created in the 13th century by Marathi Saint Sant Dnyaneshwar, Snakes
and Ladders was designed as a tool to teach children about morality and the
consequences of their actions. The game was played on a board with squares
representing virtues and vices, where players advanced or retreated based on the
square they landed on. Over time, the game spread to the Western world, where it
was introduced as a recreational pastime, emphasizing the importance of virtues like
honesty and kindness, while highlighting the consequences of vices such as greed
and dishonesty.

Key Features
1. Educational Value: Snakes and Ladders is not just a game but also a valuable
educational tool used to teach moral lessons and social values to players of
all ages.
2. Symbolism: The game's design, with ladders representing virtues and snakes
representing vices, serves as a metaphor for life's ups and downs, teaching
players about the consequences of their actions and the importance of
making ethical choices.
3. Global Appeal: From its origins in India, Snakes and Ladders has
transcended borders and cultures, becoming a universally recognized and
enjoyed board game that continues to entertain and educate players
worldwide.
4. Gameplay: With its simple rules and reliance on chance through dice rolls,
Snakes and Ladders offers a fun and interactive experience that encourages
strategic thinking and decision-making.
5. Evolution: Over the years, the game has evolved and adapted to different
cultures, with variations like "Chutes and Ladders" in the United States,
maintaining the core mechanics while incorporating local themes and
nuances.
Problem statement:
Design a mini project to implement Snake and Ladders Game using
Python.

Project Outcome:

The project outcome will be a complete implementation of the Snakes and


Ladders game using Python, with a user-friendly interface and interactive
gameplay. The project will include the following features:
1. Player Class: A class that encapsulates a player's properties, such as
their ID, rank, and current position on the board.
2. Moving Entity Class: A class that can be extended by anything that can
move the player to another position, like a Snake or ladder, to keep
the code extensible and abstract common behavior like end position.
3. Game Logic: The game logic will include dice rolls, movement of
players, and the effects of landing on snakes and ladders.
4. Error Handling: The project will include error handling for invalid
moves, such as when a player tries to move beyond the board's limits.
5. User Interface: A user-friendly interface that allows players to easily
navigate the game and understand the game state.

Project Objective:

The objective of this project is to create a complete Snakes and Ladders


game using Python, with a focus on object-oriented programming
principles. The project aims to provide a fun and engaging experience for
players, while also demonstrating the use of classes, methods, and game
logic in Python. The project will also serve as a practical example of how to
implement a classic board game in Python, with potential applications in
education and entertainment.
Implementation Code :
class GamePlayer:

def __init__(self, _id):


self._id = _id

self.rank = -1

self.position = 1

def set_position(self, pos):


self.position = pos

def set_rank(self, rank):


self.rank = rank

def get_pos(self):
return self.position

def get_rank(self):
return self.rank

class MovingEntity:

def __init__(self, end_pos=None):

self.end_pos = end_pos

self.desc = None

def set_description(self, desc):


self.desc = desc

def get_end_pos(self):
if self.end_pos is None:
raise Exception("no_end_position_defined")
return self.end_pos

class Snake(MovingEntity):
"""Snake entity"""
def __init__(self, end_pos=None):
super(Snake, self).__init__(end_pos)
self.desc = "Bit by Snake"

class Ladder(MovingEntity):
"""Ladder entity"""

def __init__(self, end_pos=None):


super(Ladder, self).__init__(end_pos)
self.desc = "Climbed Ladder"

class Board:

def __init__(self, size):


self.size = size

self.board = {}

def get_size(self):
return self.size

def set_moving_entity(self, pos, moving_entity):

self.board[pos] = moving_entity

def get_next_pos(self, player_pos):

if player_pos > self.size:


return player_pos
if player_pos not in self.board:
return player_pos
print(f'{self.board[player_pos].desc} at {player_pos}')
return self.board[player_pos].get_end_pos()

def at_last_pos(self, pos):


if pos == self.size:
return True
return False
class Dice:
def __init__(self, sides):
# no of sides in the dice
self.sides = sides

def roll(self):
# return random number between 1 to sides
import random
ans = random.randrange(1, self.sides + 1)
return ans

class Game:

def __init__(self):

self.board = None

self.dice = None

self.players = []

self.turn = 0
self.winner = None

self.last_rank = 0

self.consecutive_six = 0

def initialize_game(self, board: Board, dice_sides, players):


"""
Initialize game using board, dice and players
"""
self.board = board
self.dice = Dice(dice_sides)
self.players = [GamePlayer(i) for i in range(players)]

def can_play(self):
if self.last_rank != len(self.players):
return True
return False
def get_next_player(self):
"""
Return curr_turn player but if it has already
won/completed game , return
next player which is still active
"""
while True:
# if rank is -1 , player is still active so return
if self.players[self.turn].get_rank() == -1:
return self.players[self.turn]
# check next player
self.turn = (self.turn + 1) % len(self.players)

def move_player(self, curr_player, next_pos):


# Move player to next_pos
curr_player.set_position(next_pos)
if self.board.at_last_pos(curr_player.get_pos()):
# if at last position set rank
curr_player.set_rank(self.last_rank + 1)
self.last_rank += 1

def can_move(self, curr_player, to_move_pos):


# check if player can move or not ie. between board bound
if to_move_pos <= self.board.get_size() and
curr_player.get_rank() == -1:
return True
return False

def change_turn(self, dice_result):

self.consecutive_six = 0 if dice_result != 6 else


self.consecutive_six + 1
if dice_result != 6 or self.consecutive_six == 3:
if self.consecutive_six == 3:
print("Changing turn due to 3 consecutive sixes")
self.turn = (self.turn + 1) % len(self.players)
else:
print(f"One more turn for player {self.turn+1} after
rolling 6")

def play(self):
while self.can_play():
curr_player = self.get_next_player()
player_input = input(
f"Player {self.turn+1}, Press enter to roll the
dice")
dice_result = self.dice.roll()
print(f'dice_result: {dice_result}')
_next_pos = self.board.get_next_pos(
curr_player.get_pos() + dice_result)
if self.can_move(curr_player, _next_pos):
self.move_player(curr_player, _next_pos)
self.change_turn(dice_result)
self.print_game_state()
self.print_game_result()

def print_game_state(self):

print('-------------game state-------------')
for ix, _p in enumerate(self.players):
print(f'Player: {ix+1} is at pos {_p.get_pos()}')
print('-------------game state-------------\n\n')

def print_game_result(self):

print('-------------final result-------------')
for _p in sorted(self.players, key=lambda x:
x.get_rank()):
print(f'Player: {_p._id+1} , Rank: {_p.get_rank()}')

def sample_run():

board = Board(10)
board.set_moving_entity(7, Snake(2))
board.set_moving_entity(4, Ladder(6))
game = Game()
game.initialize_game(board, 6, 2)
game.play()

sample_run()
Output :

Conclusion :

In conclusion, the implementation of the Snakes and Ladders game using


Python has been a rewarding and educational mini project. Through this
endeavor, we have successfully applied fundamental programming concepts
such as classes, methods, and game logic to recreate a timeless board game
in a digital format. This project has not only enhanced our understanding of
object-oriented programming but has also provided us with a practical
example of how to design and develop a simple yet engaging game
application.

You might also like