Open In App

Snake and Ladder Game in C

Last Updated : 25 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

snake and ladder in c

Components of the 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.

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.

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.

C Program for Snake and Ladder Game

C




// 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];
    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
                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) {
                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;
    // 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;
    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;
  
    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");
                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
        currentPlayer = (currentPlayer == 1) ? 2 : 1;
    }
  
    return 0;
}


Output 1:

snake and ladder game output 1

Output 2:

snake and ladder game output 2

Output 3:

snake and ladder game output 3

Output 4

snake and ladder game output 4

Ultimately the game continues and the instant any of the players reaches the last square that player is declared the winner.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads