Skip to main content

Snakes and Ladders game using C programming

Snakes and Ladders game using C programming:

Program:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>


#define BOARD_SIZE 100

#define NUM_SNAKES 5

#define NUM_LADDERS 5


// Function to roll a dice

int rollDice() {

    return rand() % 6 + 1;

}


// Function to generate random snakes and ladders

void generateSnakesAndLadders(int snakes[], int ladders[]) {

    int i;

    for (i = 0; i < NUM_SNAKES; i++) {

        snakes[i] = rand() % (BOARD_SIZE - 1) + 1;

    }

    for (i = 0; i < NUM_LADDERS; i++) {

        ladders[i] = rand() % (BOARD_SIZE - 1) + 1;

    }

}


// Function to check if a position is a snake head or ladder bottom

int isSnakeOrLadder(int position, int snakes[], int ladders[]) {

    int i;

    for (i = 0; i < NUM_SNAKES; i++) {

        if (snakes[i] == position) {

            return -1; // Snake

        }

    }

    for (i = 0; i < NUM_LADDERS; i++) {

        if (ladders[i] == position) {

            return 1; // Ladder

        }

    }

    return 0; // Neither snake nor ladder

}


int main() {

    int snakes[NUM_SNAKES];

    int ladders[NUM_LADDERS];

    int playerPosition = 0;

    char playAgain;


    srand(time(NULL)); // Seed the random number generator


    do {

        generateSnakesAndLadders(snakes, ladders);


        printf("Welcome to Snakes and Ladders!\n");

        printf("Roll the dice to move forward.\n");

        printf("Reach position %d to win!\n\n", BOARD_SIZE);


        while (playerPosition < BOARD_SIZE) {

            printf("Your current position is %d.\n", playerPosition);

            printf("Press Enter to roll the dice...");

            getchar(); // Wait for Enter key press

            int diceRoll = rollDice();

            printf("You rolled a %d!\n", diceRoll);

            playerPosition += diceRoll;


            // Check if player landed on a snake or ladder

            int snakeOrLadder = isSnakeOrLadder(playerPosition, snakes, ladders);

            if (snakeOrLadder == -1) {

                printf("Oops! You landed on a snake. Going down!\n");

                playerPosition -= diceRoll;

            } else if (snakeOrLadder == 1) {

                printf("Hooray! You climbed a ladder. Going up!\n");

            }


            // Ensure player doesn't overshoot the board

            if (playerPosition > BOARD_SIZE) {

                playerPosition = BOARD_SIZE - (playerPosition - BOARD_SIZE);

            }

        }


        printf("\nCongratulations! You reached position %d and won the game!\n", BOARD_SIZE);


        printf("Do you want to play again? (y/n): ");

        scanf(" %c", &playAgain);

        playAgain = tolower(playAgain);

        playerPosition = 0; // Reset player position for the next game


    } while (playAgain == 'y');


    return 0;

}

Output:

Welcome to Snakes and Ladders!
Roll the dice to move forward.
Reach position 100 to win!

Your current position is 0.
Press Enter to roll the dice...
You rolled a 5!

Your current position is 5.
Press Enter to roll the dice...
You rolled a 6!

Your current position is 11.
Press Enter to roll the dice...
You rolled a 3!

Your current position is 14.
Press Enter to roll the dice...
You rolled a 1!

Your current position is 15.
Press Enter to roll the dice...
You rolled a 6!

Your current position is 21.
Press Enter to roll the dice...
You rolled a 2!

Your current position is 23.
Press Enter to roll the dice...
You rolled a 6!

Your current position is 29.
Press Enter to roll the dice...
You rolled a 2!

Your current position is 31.
Press Enter to roll the dice...
You rolled a 6!

Your current position is 37.
Press Enter to roll the dice...
You rolled a 4!

Your current position is 41.
Press Enter to roll the dice...
You rolled a 6!

Your current position is 47.
Press Enter to roll the dice...
You rolled a 3!

Your current position is 50.
Press Enter to roll the dice...
You rolled a 1!

Your current position is 51.
Press Enter to roll the dice...
You rolled a 2!

Your current position is 53.
Press Enter to roll the dice...
You rolled a 6!

Your current position is 59.
Press Enter to roll the dice...
You rolled a 3!

Your current position is 62.
Press Enter to roll the dice...
You rolled a 6!

Your current position is 68.
Press Enter to roll the dice...
You rolled a 1!

Your current position is 69.
Press Enter to roll the dice...
You rolled a 3!

Your current position is 72.
Press Enter to roll the dice...
You rolled a 4!

Your current position is 76.
Press Enter to roll the dice...
You rolled a 5!

Your current position is 81.
Press Enter to roll the dice...
You rolled a 6!

Your current position is 87.
Press Enter to roll the dice...
You rolled a 5!

Your current position is 92.
Press Enter to roll the dice...
You rolled a 4!

Your current position is 96.
Press Enter to roll the dice...
You rolled a 2!

Your current position is 98.
Press Enter to roll the dice...
You rolled a 2!

Your current position is 100.

Congratulations! You reached position 100 and won the game!

Do you want to play again? (y/n): y
Welcome to Snakes and Ladders!
Roll the dice to move forward.
Reach position 100 to win!

Your current position is 0.
Press Enter to roll the dice...
You rolled a 4!

Your current position is 4.
Press Enter to roll the dice...
You rolled a 6!

Your current position is 10.
Press Enter to roll the dice...
You rolled a 5!

Your current position is 15.
Press Enter to roll the dice...
You rolled a 2!

Your current position is 17.
Press Enter to roll the dice...
You rolled a 5!

Your current position is 22.
Press Enter to roll the dice...
You rolled a 4!

Your current position is 26.
Press Enter to roll the dice...
You rolled a 5!

Your current position is 31.
Press Enter to roll the dice...
You rolled a 2!

Your current position is 33.
Press Enter to roll the dice...
You rolled a 1!

Your current position is 34.
Press Enter to roll the dice...
You rolled a 6!

Your current position is 40.
Press Enter to roll the dice...
You rolled a 3!

Your current position is 43.
Press Enter to roll the dice...
You rolled a 6!

Your current position is 49.
Press Enter to roll the dice...
You rolled a 3!

Your current position is 52.
Press Enter to roll the dice...
You rolled a 2!

Your current position is 54.
Press Enter to roll the dice...
You rolled a 5!

Your current position is 59.
Press Enter to roll the dice...
You rolled a 5!

Your current position is 64.
Press Enter to roll the dice...
You rolled a 1!

Your current position is 65.
Press Enter to roll the dice...
You rolled a 4!

Your current position is 69.
Press Enter to roll the dice...
You rolled a 6!

Your current position is 75.
Press Enter to roll the dice...
You rolled a 6!

Your current position is 81.
Press Enter to roll the dice...
You rolled a 3!

Your current position is 84.
Press Enter to roll the dice...
You rolled a 1!

Your current position is 85.
Press Enter to roll the dice...
You rolled a 5!

Your current position is 90.
Press Enter to roll the dice...
You rolled a 5!

Your current position is 95.
Press Enter to roll the dice...
You rolled a 2!

Your current position is 97.
Press Enter to roll the dice...
You rolled a 3!

Your current position is 100.

Congratulations! You reached position 100 and won the game!

Do you want to play again? (y/n): n

Comments

Popular posts from this blog

Install TensorFlow on Windows 11: Step-by-Step Guide for CPU & GPU

 --- Installing **TensorFlow on Windows 11** requires setting up system dependencies, configuring Python, and ensuring compatibility with CPU or GPU acceleration. This step-by-step guide provides everything needed to install **TensorFlow 2.10 or lower** on **Windows Native**, including software prerequisites, Microsoft Visual C++ Redistributable installation, Miniconda setup, GPU driver configuration, and verification steps.   ### **System Requirements:**   Before installing TensorFlow, ensure your system meets these requirements:   - **Operating System:** Windows 7 or higher (64-bit)   - **Python Version:** 3.9–3.12   - **pip Version:** 19.0 or higher for Linux and Windows, 20.3 or higher for macOS   - **Microsoft Visual C++ Redistributable:** Required for Windows Native   - **Long Paths Enabled:** Ensure long paths are enabled in Windows settings   For **GPU support**, install:   - **NVIDIA ...

Unreal Engine Product Showcase: Mesmerizing Video Sequence Render

  4k Image:

Cloudflare Is Down Worldwide: What Happened, What It Means & What You Can Do

🌍 Cloudflare Is Down Worldwide: What Happened & Why the Internet Broke Today On 18 November 2025 , the internet experienced one of the largest global outages in years as Cloudflare , the backbone of countless websites and online services, went down unexpectedly. The outage caused millions of websites to become slow, unreachable, or return 5xx internal server errors , leading to widespread disruption across businesses, apps, and essential online platforms. This blog post explains what happened, why the internet broke for many users, and what Cloudflare has officially said so far. What Exactly Happened? Around 4:30 PM IST , reports began flooding social media and outage trackers like Downdetector. Users across India, Europe, the US, and Southeast Asia noticed that: Websites were not loading Requests were timing out Services dependent on Cloudflare CDN or DNS stopped responding APIs hosted through Cloudflare were failing Even some security and protection layers were ...