0% found this document useful (0 votes)
30 views18 pages

Snake and Ladder Game Using Algoritms: Roject Eport

Uploaded by

rajanthakurxo
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)
30 views18 pages

Snake and Ladder Game Using Algoritms: Roject Eport

Uploaded by

rajanthakurxo
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/ 18

Snake and Ladder Game using Algoritms

PROJECT REPORT

Name :
Priyanshu Thakur (221030166)
Avantik Thakur (221030161)
Rajan Thakur (221030175)

Subject : Design and Analysis of Algorithms Lab

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.

Key objectives of the project include:

1. - A game board with a specified size and layout.


2. - Players who take turns to roll a dice and move their tokens
on the board.
3. - Snakes and ladders placed at specific positions on the board,
affecting player movement.
4. - Game logic to handle player turns, dice rolling, and
interactions with snakes and ladders.

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

Data Structure Used :

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:

Graph creation Algorithm :

- 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.

Dice Rolling Algorithm :


- Description: Simulates the rolling of a six-sided dice, producing a random outcome between 1 and 6.
- Implementation: Uses the rand() function from the C standard library to generate random numbers. -
Purpose: Provides randomness to the game, determining the number of positions a player can move on
their turn.

Player Movement Algorithm :


- Description: Handles the movement of players on the game board, taking into account the outcome of
dice rolls and the presence of snakes and ladders.
- Implementation: Updates the player's position based on the sum of their current position and the dice
roll outcome. Checks for the presence of snakes and ladders at the new position and adjusts the
player's position accordingly.
- Purpose: Ensures that players move correctly on the board, climbing ladders and descending snakes as
necessary

Page

6
Project Code
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>

#define BOARD_SIZE 100


#define MAX_PLAYERS 2

typedef struct
{ int start;
int end;
} Jump;

typedef struct Node


{ int vertex;
struct Node *next;
} Node;

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;
}

Graph *createGraph(int vertices)


{
Graph *graph = (Graph *)malloc(sizeof(Graph)); graph->adjLists =
(Node **)malloc(vertices * sizeof(Node *)); graph->visited = (int

Page

7
*)malloc(vertices * sizeof(int));

for (int i = 0; i < vertices; i++)


{
graph->adjLists[i] = NULL; graph>visited[i]
= 0;
}
return graph;
}

void addEdge(Graph *graph, int src, int dest)


{
Node *newNode = createNode(dest); newNode-
>next = graph->adjLists[src]; graph->adjLists[src] = newNode;
}

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 displayChart(int curp1, int curp2)


{ for (int i = 10; i > 0; i-
-)
{ for (int j = 1; j <= 10;
j++)
{
int pos = (i % 2 == 0) ? (i - 1) * 10 + j : i * 10 - j + 1; if
(curp1 == pos && curp2 == pos)
{
printf("BOTH\t");
}
else if (curp1 == pos)
{
printf("P1\t");
}
else if (curp2 == pos)
{
printf("P2\t");
} else
{
printf("%d\t", pos);
} }
printf("\n");
}
printf("\n");
}

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;
}
}

// Check for ladders for (int i =


0; i < numLadders; i++)
{
if (ladders[i].start == newPos)
{
newPos = ladders[i].end;
break;
}
}
}

*playerPos = newPos;
}

int main()
{
srand(time(0)); // Seed for random number generation

Jump snakes[] = {{99, 1}, {65, 40}, {25, 9}}; Jump


ladders[] = {{13, 42}, {60, 83}, {70, 93}}; int
numSnakes = sizeof(snakes) / sizeof(snakes[0]); int
numLadders = sizeof(ladders) / sizeof(ladders[0]);
Graph *graph = createGraph(BOARD_SIZE);
initializeBoard(graph, snakes, ladders, numSnakes,
numLadders);

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);

if (positions[0] >= BOARD_SIZE)


{
printf("Player 1 wins!\n");
exit(0);
}

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);

if (positions[1] >= BOARD_SIZE)


{
printf("Player 2 wins!\n");
exit(0);

Page

11
}

displayChart(positions[0], positions[1]); printf("Player 2 rolled a %d and is


now at position %d\n", dice2, positions[1]); currentPlayer = 0; break;
case '3': exit(0); default:
printf("Invalid choice. Try again.\n");
}
}

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 |

Choose your option


[1] Player 1 plays
[2] Player 2 plays
[3] Exit
1

Rolling the dice for Player 1...


91 92 93 94 95 96 97 98 99 100
90 89 88 87 86 85 84 83 82 81
71 72 73 74 75 76 77 78 79 80
70 69 68 67 66 65 64 63 62 61
51 52 53 54 55 56 57 58 59 60
50 49 48 47 46 45 44 43 42 41
31 32 33 34 35 36 37 38 39 40
30 29 28 27 26 25 24 23 22 21
11 12 13 14 15 16 17 18 19 20
10 9 8 7 6 5 4 P1 2 1

Player 1 rolled a 3 and is now at position 3


Snakes: | 25 to 9 | 65 to 40 | 99 to 1 |
Ladders: | 13 to 42 | 60 to 83 | 70 to 93 |

Choose your option


[1] Player 1 plays
[2] Player 2 plays
[3] Exit
1
It's Player 2's turn!
Snakes: | 25 to 9 | 65 to 40 | 99 to 1 |
Ladders: | 13 to 42 | 60 to 83 | 70 to 93 |

Choose your option


[1] Player 1 plays

Page

15
[2] Player 2 plays
[3] Exit
2

Rolling the dice for Player 2...


91 92 93 94 95 96 97 98 99 100
90 89 88 87 86 85 84 83 82 81
71 72 73 74 75 76 77 78 79 80
70 69 68 67 66 65 64 63 62 61
51 52 53 54 55 56 57 58 59 60
50 49 48 47 46 45 44 43 42 41
31 32 33 34 35 36 37 38 39 40
30 29 28 27 26 25 24 23 22 21
11 12 13 14 15 16 17 18 19 20
10 9 8 7 6 5 4 P1 P2 1

Player 2 rolled a 2 and is now at position 2


Snakes: | 25 to 9 | 65 to 40 | 99 to 1 |
Ladders: | 13 to 42 | 60 to 83 | 70 to 93 |

Choose your option


[1] Player 1 plays
[2] Player 2 plays
[3] Exit

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.

- Graph Creation: O(V), where V is the number of vertices (positions on


the board).
- Board Initialization: O(BOARD_SIZE (numSnakes + numLadders)),
where BOARD_SIZE is the size of the game board and
numSnakes/numLadders are the number of snakes/ladders.
- Player Movement: O(1) - Constant time complexity for updating player
positions.
- Overall: O(V + BOARD_SIZE (numSnakes + numLadders)), where V is
the number of vertices.

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

You might also like