-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSnakes and Ladders Game.c
95 lines (81 loc) · 2.89 KB
/
Snakes and Ladders Game.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function to roll the dice
int rollDice() {
return rand() % 6 + 1;
}
// Function to play the game
void playGame() {
int player1 = 0, player2 = 0; // Player positions
int currentPlayer = 1; // 1 for player 1, 2 for player 2
int dice;
while (player1 < 100 && player2 < 100) {
printf("Player %d, press Enter to roll the dice...\n", currentPlayer);
getchar();
dice = rollDice();
printf("You rolled a %d!\n", dice);
if (currentPlayer == 1) {
player1 += dice;
} else {
player2 += dice;
}
// Check for snakes and ladders
if (player1 == 4) player1 = 14;
if (player1 == 9) player1 = 31;
if (player1 == 17) player1 = 7;
if (player1 == 20) player1 = 38;
if (player1 == 28) player1 = 84;
if (player1 == 40) player1 = 59;
if (player1 == 51) player1 = 67;
if (player1 == 54) player1 = 34;
if (player1 == 62) player1 = 19;
if (player1 == 64) player1 = 60;
if (player1 == 71) player1 = 91;
if (player1 == 87) player1 = 24;
if (player1 == 93) player1 = 73;
if (player1 == 95) player1 = 75;
if (player1 == 99) player1 = 78;
if (player2 == 4) player2 = 14;
if (player2 == 9) player2 = 31;
if (player2 == 17) player2 = 7;
if (player2 == 20) player2 = 38;
if (player2 == 28) player2 = 84;
if (player2 == 40) player2 = 59;
if (player2 == 51) player2 = 67;
if (player2 == 54) player2 = 34;
if (player2 == 62) player2 = 19;
if (player2 == 64) player2 = 60;
if (player2 == 71) player2 = 91;
if (player2 == 87) player2 = 24;
if (player2 == 93) player2 = 73;
if (player2 == 95) player2 = 75;
if (player2 == 99) player2 = 78;
printf("Player 1 is at position %d, Player 2 is at position %d\n", player1, player2);
if (player1 >= 100) {
printf("Player 1 wins!\n");
break;
}
if (player2 >= 100) {
printf("Player 2 wins!\n");
break;
}
// Switch players
if (currentPlayer == 1) {
currentPlayer = 2;
} else {
currentPlayer = 1;
}
}
}
int main() {
srand(time(NULL)); // Seed the random number generator
printf("Welcome to Snakes and Ladders!\n");
printf("Rules:\n");
printf("- Each player rolls a dice in turns.\n");
printf("- If you land on a snake's head, you move down.\n");
printf("- If you land on a ladder's bottom, you move up.\n");
printf("- The first player to reach or exceed 100 wins.\n");
playGame();
return 0;
}