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
Post a Comment