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

Stable Diffusion WebUI 1.10.1 Full Installation Guide | AUTOMATIC1111 | Windows 11

Stable Diffusion WebUI 1.10.1 Full Installation Guide | AUTOMATIC1111 | Windows 11  Welcome to this step-by-step Stable Diffusion WebUI 1.10.1 installation guide! In this tutorial, we will walk you through the complete setup process on Windows 11 , including downloading and installing Git , setting up Python 3.10.6 , cloning the AUTOMATIC1111 repository , and configuring .gitignore for a clean and efficient installation. By following this guide, you’ll be able to generate AI-generated images using Stable Diffusion with ease. Whether you're new to AI image generation or an experienced user, this guide ensures that your setup is optimized for performance and stability. πŸ”— Required Downloads: Before we begin, make sure to download the following tools: ✅ Git for Windows – Download Here ✅ Stable Diffusion WebUI (AUTOMATIC1111) – Download Here ✅ Python 3.10.6 – Download Here πŸ› ️ Step-by-Step Installation Process 1️⃣ Install Git for Windows Git is required to clone the ...

Unreal Engine Product Showcase: Mesmerizing Video Sequence Render

  4k Image:

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