-
Notifications
You must be signed in to change notification settings - Fork 0
/
saapsidi.c
168 lines (142 loc) · 4.5 KB
/
saapsidi.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#define NUM_SNAKES 14
#define NUM_LADDERS 9
#define DEFAULT_FINAL_SQUARE 100
void displayBoard(int numPlayers, char player[][20], int position[])
{
printf("\nCurrent Board:\n");
for (int i = 0; i < numPlayers; i++)
{
printf("%s: %d\t", player[i], position[i]);
}
printf("\n\n");
}
void playGame(int numPlayers, int finalSquare)
{
char player[numPlayers][20];
int position[numPlayers];
int consecutiveSixCount[numPlayers];
// Seed the random number generator with the current time
srand((unsigned int)time(NULL));
for (int i = 0; i < numPlayers; i++)
{
printf("Enter player %d's name: ", i + 1);
scanf("%s", player[i]);
position[i] = 0;
consecutiveSixCount[i] = 0;
}
// Define snakes and ladders
int snakes[NUM_SNAKES][2] = {
{23,7},
{28,6},
{36,8},
{44,5},
{46,16},
{49,11},
{54,22},
{60,32},
{68,22},
{82,42},
{91,18},
{94,62},
{97,8},
{99,2}
};
int ladders[NUM_LADDERS][2] = {
{3, 22},
{7, 21},
{14,43},
{19, 42},
{25, 65},
{38, 89},
{60, 81},
{42,62},
{69,98}
};
int turn = 0;
while (1)
{
printf("%s is rolling now .... \n", player[(turn % numPlayers)]);
int current = rand() % 6 + 1;
printf("Rolled a %d \n", current);
sleep(1);
// Move the player
if(!(position[turn % numPlayers] + current >100)){
position[turn % numPlayers] += current;
}
// Check for snakes and ladders
for (int i = 0; i < NUM_SNAKES; i++)
{
if (position[turn % numPlayers] == snakes[i][0])
{
printf("Oh no! Encountered a snake! Sliding down to %d.\n", snakes[i][1]);
position[turn % numPlayers] = snakes[i][1]; // Corrected line
}
}
for (int i = 0; i < NUM_LADDERS; i++)
{
if (position[turn % numPlayers] == ladders[i][0])
{
printf("Yay! Found a ladder! Climbing up to %d.\n", ladders[i][1]);
position[turn % numPlayers] = ladders[i][1];
}
}
// Check if the player reaches or passes the final square
if (position[turn % numPlayers] >= finalSquare)
{
printf("Congratulations! %s reached the final square!, he is the winner!\n", player[turn % numPlayers]);
displayBoard(numPlayers, player, position);
return;
}
printf("%s's current position is now %d \n", player[turn % numPlayers], position[turn % numPlayers]);
// Check for collisions with other players
for (int i = 0; i < numPlayers; i++)
{
if (i != turn % numPlayers && position[i] == position[turn % numPlayers])
{
printf("Oh no! Collision with %s! %s eliminates %s, sending them back to square 1.\n", player[i], player[turn % numPlayers], player[i]);
position[i] = 1; // The latest player eliminates the previous one
printf("\n\n %s gets another chance",player[turn % numPlayers]);
if(current == 6){
consecutiveSixCount[turn % numPlayers]++;
}
continue;
}
}
// Check for consecutive sixes
if (current == 6)
{
consecutiveSixCount[turn % numPlayers]++;
if (consecutiveSixCount[turn % numPlayers] == 3)
{
printf("Oh no! %s rolled three consecutive 6s and gets a penalty of -6!\n", player[turn % numPlayers]);
position[turn % numPlayers] -= 12;
consecutiveSixCount[turn % numPlayers] = 0;
}
else
{
printf("Wow! %s rolled a 6 and gets another chance.\n", player[turn % numPlayers]);
continue; // Bonus chance for rolling a 6
}
}
else
{
consecutiveSixCount[turn % numPlayers] = 0;
}
displayBoard(numPlayers, player, position);
sleep(1.5);
turn += 1;
// system("clear");
}
}
int main()
{
int numPlayers;
printf("Enter the number of players: ");
scanf("%d", &numPlayers);
playGame(numPlayers, DEFAULT_FINAL_SQUARE);
return 0;
}