Snake and Ladder Game Using Algoritms: Roject Eport
Snake and Ladder Game Using Algoritms: Roject Eport
PROJECT REPORT
Name :
Priyanshu Thakur (221030166)
Avantik Thakur (221030161)
Rajan Thakur (221030175)
Page
Table of Contents
• Introduction
• Objective
• Prerequisite Libraries
• Data Structure Used
• Algorithm Used
• Project Code
• Code Analysis: Snake and Ladder game in C
• Output
• Time Complexity of the Program
• Real-Time Application
Page
INTRODUCTION
The Snakes and Ladders game, also known as Chutes and Ladders, is a classic board game
enjoyed by people of all ages. It offers a simple yet entertaining gameplay experience that
combines luck with strategy. This project aims to implement the Snakes and Ladders game
algorithmically using the C programming language, with a particular focus on the design and
analysis of algorithms.
Page
OBJECTIVE
The objective of this project is to design and implement a digital version of the Snakes
and Ladders game using the C programming language. This simulation will support two
players, allow them to take turns rolling a die, and move their pieces across the board.
Special attention will be given to accurately representing the effects of landing on snakes
and ladders, as well as providing an interactive and engaging user interface.
Page
Prerequisite Libraries :
• iostream : for input and output operations
• string : for handling string operations
• process : for process control functions
In this hotel management project, the data structure used is arrays to store informa on such as room
numbers, room types, customer names, availability status, charges, etc. Arrays are suitable for this project
as they allow for easy access and manipula on of data elements in a structured manner.
Algorithm Used :
The implementation of the Snakes and Ladders game involves several algorithms to ensure smooth
gameplay and adherence to game rules. These algorithms include:
- Description: The game board is represented as a graph, where each position on the board is a vertex,
and connections between positions are represented as edges.
- Implementation: An adjacency list representation is used to efficiently store the graph structure. -
Purpose: Facilitates navigation on the game board and enables efficient checking for the presence of
snakes and ladders at specific positions.
Page
5
Board Initialization Algorithm :
- Description: Sets up the initial state of the game board, including the positions of snakes and ladders. -
Implementation: Loops through the board positions and adds connections between vertices based on
the presence of snakes and ladders.
- Purpose: Prepares the game board for gameplay, ensuring that player movement is correctly affected
by the presence of snakes and ladders.
Page
6
Project Code
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
typedef struct
{ int start;
int end;
} Jump;
typedef struct
{
Node **adjLists; int
*visited;
} Graph;
Node *createNode(int v)
{
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->vertex = v; newNode->next = NULL;
return newNode;
}
Page
7
*)malloc(vertices * sizeof(int));
void initializeBoard(Graph *graph, Jump *snakes, Jump *ladders, int numSnakes, int numLadders)
{
for (int i = 0; i < BOARD_SIZE; i++)
{
for (int dice = 1; dice <= 6; dice++)
{ int dest = i + dice;
if (dest < BOARD_SIZE)
{
for (int j = 0; j < numSnakes; j++)
{ if
(snakes[j].start == dest)
{
dest = snakes[j].end;
break;
}
}
for (int k = 0; k < numLadders; k++)
{ if
(ladders[k].start == dest)
{
dest = ladders[k].end;
break;
}
}
addEdge(graph, i, dest);
Page
8
}
}
}
}
int rollDice()
{ return (rand() % 6) + 1;
}
void movePlayer(Graph *graph, int *playerPos, int dice, Jump *snakes, Jump *ladders, int
numSnakes, int numLadders)
{
int newPos = *playerPos + dice;
Page
9
// Check for boundary overflow
if (newPos >= BOARD_SIZE)
{
newPos = *playerPos; // Player remains in the same position if the move exceeds the board size }
else
{
// Check for snakes for (int i =
0; i < numSnakes; i++)
{
if (snakes[i].start == newPos)
{
newPos = snakes[i].end;
break;
}
}
*playerPos = newPos;
}
int main()
{
srand(time(0)); // Seed for random number generation
Page
10
int positions[MAX_PLAYERS] = {0}; // Players' positions
int currentPlayer = 0; char ch;
while (1)
{ printf("Snakes: | 25 to 9 | 65 to 40 | 99 to 1 |\nLadders: | 13 to 42 | 60 to 83 | 70 to 93
|\n\n");
printf("Choose your option\n"); printf("[1] Player 1 plays\n"); printf("[2] Player 2 plays\n");
printf("[3] Exit\n"); scanf(" %c", &ch);
switch (ch)
{ case '1':
if (currentPlayer != 0)
{
printf("It's Player 2's turn!\n");
break;
}
int dice1 = rollDice(); printf("\n\nRolling the dice for Player 1...\n");
movePlayer(graph, &positions[0], dice1, snakes, ladders, numSnakes, numLadders);
displayChart(positions[0], positions[1]);
printf("Player 1 rolled a %d and is now at position %d\n", dice1, positions[0]);
currentPlayer = 1; break; case '2':
if (currentPlayer != 1)
{
printf("It's Player 1's turn!\n");
break;
}
int dice2 = rollDice(); printf("\n\nRolling the dice for Player 2...\n");
movePlayer(graph, &positions[1], dice2, snakes, ladders, numSnakes, numLadders);
Page
11
}
return 0;
}
Page
12
Code Analysis: Snake and Ladder Game
Data Structures :
The implement on of the Snakes and Ladders game relies on the following data structures:
Graph
- Represent on: Adjacency list
- Purpose: Represents the game board and connect Ons between pose Ons, facilitating efficient traversal and
navigation.
Jump
- Structure: {start, end}
-Purpose: Represents the start and end positions of snakes and ladders on the game board, allowing for easy
identification and handling of these game elements.
Prototypes:
Implementation Details
The Snakes and Ladders game is implemented in the C programming language. The key
implementation details include:
Graph Creation
- The game board is represented as a graph using an adjacency list data structure.
- Each vertex represents a position on the board, and edges represent connections between positions. -
Graph creation initializes the adjacency list and adds connections between vertices based on the
presence of snakes and ladders.
Board Initialization
Page
13
- The initial state of the game board is set up, including the positions of snakes and ladders.
- Board initialization loops through the board positions and adds connections between vertices based on
the positions of snakes and ladders.
Player Movement
- Players take turns to roll a dice and move their tokens on the game board.
- Player movement algorithm updates the player's position based on the outcome of the dice roll and
adjusts the position if the player lands on a snake or ladder.
Game Logic
- The game logic handles player turns, dice rolling, and interactions with snakes and ladders. - Players
alternate turns, and the game continues until one of the players reaches the final position on the board.
Randomness
- Randomness is introduced into the game through the dice rolling algorithm.
- The rand() function from the C standard library is used to generate random numbers representing the
outcome of dice rolls.
Page
14
OUTPUT
Snakes: | 25 to 9 | 65 to 40 | 99 to 1 |
Ladders: | 13 to 42 | 60 to 83 | 70 to 93 |
Page
15
[2] Player 2 plays
[3] Exit
2
Page
16
Time Complexity of Program :
This project report provides a comprehensive overview of the Snakes and Ladders game
implementation, focusing on algorithm design and analysis aspects. Through detailed
explanations and analysis, it demonstrates the effectiveness of the chosen algorithms and
their importance in developing efficient software solutions.
Space Complexity
- Graph: O(V + E), where V is the number of vertices and E is the number
of edges.
- Board Initialization: O(BOARD_SIZE) - Space required to initialize the
game board.
- Overall: O(V + E + BOARD_SIZE) - Total space complexity of the
implementation.
Overall, the time complexity of the program can be influenced by the size of the data and the efficiency of
the algorithms used.
Page
17
Bibliography:
1. Faculty Guidance:
• Ms. Nitika (Assistant Professor)
2. Book Guidance
C Programming Documentation: https://en.cppreference.com/w/c
Algorithm : Design and Analysis textbooks
3. Website Guidance:
• www.google.com
• https://powerdrill.ai/
Page
18