Skip to content

Commit cd961e3

Browse files
Add single player Snakes and Ladders game in C
1 parent e5dad3f commit cd961e3

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

games/snakes_and_ladders.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Snake and Ladders Game (Single Player)
3+
* Author: KarthikeyaPila
4+
*
5+
* A simple console-based implementation of Snake and Ladders.
6+
* Roll the dice and try to reach exactly 100.
7+
* Ladders move you up, snakes move you down.
8+
*
9+
* Compile: gcc snake_game.c -o snake_game
10+
* Run: ./snake_game
11+
*/
12+
13+
14+
#include <stdio.h> // for input output functions. -> printf, scanf
15+
#include <stdlib.h> // for memory allocated functions -> calloc
16+
#include <time.h> // used with srand()
17+
18+
int update_pos_if_ladder_snake(int player_position) {
19+
int ladder_snake_pos[16][2] = { // -> these are the ladder and snake positions.
20+
{4, 14}, {9, 31}, {20, 38}, {28, 84}, {40, 59}, // -> if the 0th index is greater than 1st index => represents a snake
21+
{51, 67}, {63, 81}, {71, 91}, {17, 7}, {54, 34}, // -> if the 0th index is lesser than the 1st index => represents a ladder
22+
{62, 19}, {64, 60}, {87, 24}, {93, 73}, {95, 75}, {99, 2}};
23+
24+
for (int j = 0; j < 16; j++) {
25+
if (player_position == ladder_snake_pos[j][0])
26+
{
27+
player_position = ladder_snake_pos[j][1];
28+
break; // Once matched, no need to check further
29+
}
30+
}
31+
32+
return player_position;
33+
}
34+
35+
int main(){
36+
int no_of_players = 1;
37+
38+
printf("Let's play Snake and Ladders! (Single Player Mode)\n");
39+
printf("Let's start the game!\n");
40+
41+
int *player_position_arr = calloc(no_of_players, sizeof(int));
42+
43+
printf("\nOkay, let's start the game.\n");
44+
45+
printf("You are currently at: %d\n", player_position_arr[0]);
46+
47+
srand(time(NULL)); // Seed random generator once here
48+
49+
while (player_position_arr[0] < 100){
50+
printf("\nRoll the dice by pressing Enter\n");
51+
while (getchar() != '\n'); // Wait for Enter key (ignore other characters)
52+
53+
int dice = (rand() % 6) + 1; // this gives a value between 1 and 6 both included.
54+
printf("Dice value: %d\n", dice);
55+
56+
if (player_position_arr[0] + dice > 100) {
57+
dice = 0;
58+
printf("Oops, you need the exact number to reach 100.\n");
59+
}
60+
61+
player_position_arr[0] += dice;
62+
int updated_pos = update_pos_if_ladder_snake(player_position_arr[0]);
63+
64+
if (updated_pos > player_position_arr[0]) { // this executes when the late r position is higher than the previous position => got up a ladder
65+
printf("Let's go! You climbed a ladder!\n");
66+
}
67+
else if (updated_pos < player_position_arr[0]) {
68+
printf("Oh no! You got bitten by a snake.\n"); // this executes when the later position is lower than the previous position => got bitten by a snake.
69+
}
70+
71+
player_position_arr[0] = updated_pos; // assigns the new player position after the dice roll.
72+
printf("You are now at: %d\n", updated_pos);
73+
}
74+
75+
//the loop ends when the player reaches 100.
76+
printf("\nCongrats, you won!!\n");
77+
78+
free(player_position_arr);
79+
80+
return 0;
81+
}

0 commit comments

Comments
 (0)