Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
149 views16 pages

Snake and Ladder Game in C

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

Snake and Ladder Game in C

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

SNAKE AND LADER

GAME In C

Done by:

237Z1A6665:[Link]

237Z1A6666:[Link] Prasad

237Z1A6667:[Link]

237Z1A6668:[Link]

237Z1A6670:[Link]

CSM-B
● 1
Introduction
Snake and Ladder game is a traditional game that
involves two or more players and the winner is the
guy who reaches the final square on the game
board at the earliest .

Components of game :

1.A square board with a series of numbered


squares arranged in m x m grid.

2. A dice to be rolled usually uses a six-faced die.

3. Each player uses different color tokens in order


to represent them.

● 2
Rules of the game:
1. Players take turns rolling the die and move their token forward by the
number that appears on the top of the die. For example, if a player rolls a
3, they move three squares forward.

2. If a player lands on a square with the base of a ladder, then they must
climb the ladder to the square which is at the top of the ladder.

3. If a player lands on the square in which there is a mouth of a snake then


the player must slide down to the square which is at the snake’s tail.

4. Players take the turn clockwise and the game terminates until one player
reaches the final square. If a player rolls a number that counts past the
final square, then the player must wait until their next turn to try again.

5. The first player to reach the final square is declared the winner.

● 3
Implementation
1. Defining a function to roll a six-faced die we will use rand() here
in order to generate the random integer value in each roll of
die.
2. Defining a function to decide the move of the player based on
the number that appeared on the top of the die. newPosition is
the sum of the current position and the number on top of the
die.
3. newSquare, the square where the player lands is basically the
sum of newPosition and the board[new position] .
4. If newSquare is greater than the final value of the square board
then try again else return newSquare.
5. Initialize the players in the main function further check for the
moves and return the winner.

● 4
C program for sanke and ladder game :
// C Program to implement Snake and Ladder Game

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

// Function to roll a six-sided die

int rollDie()

return rand() % 6 + 1;

// global variables to store postions of player1 and player2

int player1 = 0, player2 = 0;

// Function to print the board

void printBoard()

// logic to print a snake-ladder Game board

// programmer can implement their own logic for the board,

// this is one way to print a snake ladder board.

int board[101];

● 5
for (int i = 1; i <= 100; i++)

board[i] = i;

int alt = 0; // to switch between the alternate nature of the board

int iterLR = 101; // iterator to print from left to right

int iterRL = 80; // iterator to print from right to left

int val = 100;

while (val--)

if (alt == 0)

iterLR--;

if (iterLR == player1)

printf("#P1 ");

else if (iterLR == player2)

printf("#P2 ");

else

● 6
printf("%d ", board[iterLR]);

if (iterLR % 10 == 1)

printf("\n\n");

alt = 1;

iterLR -= 10;

else

iterRL++;

if (iterRL == player1) {

printf("#P1 ");

else if (iterRL == player2)

printf("#P2 ");

else

printf("%d ", board[iterRL]);

if (iterRL % 10 == 0) {

● 7
printf("\n\n");

alt = 0;

iterRL -= 30;

if (iterRL == 10)

break;

printf("\n");

// Function to move the player

int movePlayer(int currentPlayer, int roll)

int newPosition = currentPlayer + roll;

● 8
// Define the positions of snakes and ladders on the board//

int snakesAndLadders[101];

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

snakesAndLadders[i] = 0;

// here positive weights represent a ladder

// and negative weights represent a snake.

snakesAndLadders[6] = 40;

snakesAndLadders[23] = -10;

snakesAndLadders[45] = -7;

snakesAndLadders[61] = -18;

snakesAndLadders[65] = -8;

snakesAndLadders[77] = 5;

● 9
snakesAndLadders[98] = -10;

int newSquare = newPosition + snakesAndLadders[newPosition];

if (newSquare > 100)

return currentPlayer;

// Player cannot move beyond

// square 100

return newSquare;

int main()

srand(time(0)); // Initialize random seed

int currentPlayer = 1;

int won = 0;

● 10
printf("Snake and Ladder Game\n");

while (!won)

printf("\nPlayer %d, press Enter to roll the die...",currentPlayer);

getchar(); // Wait for the player to press Enter

int roll = rollDie();

printf("You rolled a %d.\n", roll);

if (currentPlayer == 1) {

player1 = movePlayer(player1, roll);

printf("Player 1 is now at square %d.\n\n",

player1);

printBoard();

if (player1 == 100) {

printf("Player 1 wins!\n");

● 11
won = 1;

else {

player2 = movePlayer(player2, roll);

printf("Player 2 is now at square %d.\n\n",

player2);

printBoard();

if (player2 == 100)

printf("Player 2 wins!\n");

won = 1;

// Switch to the other player

● 12
currentPlayer = (currentPlayer == 1) ? 2 : 1;

return 0;

Output 1:

Output 2:

Output 3:

● 13
Output4:

● 14
Conclusion
This project aims to Create an engaging program in that tests's user knowledge across
various topics . It will provide an interactive platform for user's to participate and enjoy the
experience.

Reference:
C programming Language Book

● 15

You might also like