-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
185 lines (165 loc) · 3.57 KB
/
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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//C program for creating & simulating a Snake & Ladder Game
// Coded by: Krishna Bhandari. /\('-')/\
//Constraints and Rules
/*
1.The game will begin with any dice value.
2.If a 6(six) is appeared then a chance is awarded to that player.
3.Only the location of current player is shown on the board,
the location of other player is mentioned below the board.
4.Snakes:- 99 to 1, 65 to 40, 25 to 9.
5.Ladder:- 70 to 93, 60 to 83, 13 to 42.
*/
#include<stdio.h>
#include<stdlib.h>
int rd()
{
int rem;
A:rem=rand()%7;
if(rem==0)
goto A;
else
return rem;
}
void displaychart(int curp,char player[4])
{ int i,j,t,c,sft=0,diceres,pos1,pos2;
if(curp==100)
{
printf("*****Congratulations*****\n\n\nPlayer %s wins\n",player);
scanf("%*s");
exit(0);
}
for(i=10;i>0;i--)
{
t=i-1;
if((sft%2)==0)
{
c=0;
for(j=10;j>=1;j--)
{
diceres=(i*j)+(t*c++);
if(curp==diceres)
printf("%s\t",player);
else
printf("%d\t",diceres);
}
sft++;
}
else
{
c=9;
for(j=1;j<=10;j++)
{
diceres=(i*j)+(t*c--);
if(curp==diceres)
printf("%s\t",player);
else
printf("%d\t",diceres);
}
sft++;
}
printf("\n\n");
}
printf("--------------------------------------------------------------------------\n");
}
void main()
{
int i,dice,cur_pos1=0,cur_pos2=0;
char ch;
while(1)
{ printf(" ** SNAKE AND LADDER GAME** \n Coded By Krishna bhandari\n");
printf("Snakes:- 25 to 9,\t 65 to 40,\t 99 to 1.\nLadder:- 13 to 42,\t 60 to 83,\t 70 to 93.\n");
printf("Choose your option\n");
printf("1. Player 1 plays\n");
printf("2. Player 2 plays\n");
printf("3. Exit\n");
scanf("%s",&ch);
switch(ch)
{
case '1':dice=rd();
system("cls");
printf("\t\t\t\tDice = %d\n\n",dice);
if(dice==6)
printf("Dice=6: You have earned a chance to play one more time.\n");
cur_pos1=dice+cur_pos1;
if(cur_pos1<101){
if(cur_pos1==99)
{
displaychart(1,"$P1$");//snake
}
if(cur_pos1==65)
{
displaychart(40,"$P1$");//snake
}
if(cur_pos1==25)
{
displaychart(9,"$P1$");//snake
}
if(cur_pos1==70)
{
displaychart(93,"$P1$");//ladder
}
if(cur_pos1==60)
{
displaychart(83,"$P1$");//ladder
}
if(cur_pos1==13)
{
displaychart(42,"$P1$");//ladder
}
else{
displaychart(cur_pos1,"$P1$");
}
}
else{
cur_pos1=cur_pos1-dice;
printf("Range exceeded of Player 1.\n");
displaychart(cur_pos1,"$P1$");
}
printf("Player 2 position is %d\n",cur_pos2);
break;
case '2':dice=rd();
system("cls");
printf("\t\t\t\tDice = %d\n\n",dice);
cur_pos2=dice+cur_pos2;
if(cur_pos2<101){
if(cur_pos2==99) //snake
{
displaychart(1,"$P2$");
}
if(cur_pos2==65) //snake
{
displaychart(40,"$P2$");
}
if(cur_pos2==25) //snake
{
displaychart(9,"$P2$");
}
if(cur_pos2==70) //ladder
{
displaychart(93,"$P2$");
}
if(cur_pos2==60) //ladder
{
displaychart(83,"$P2$");
}
if(cur_pos2==13) //ladder
{
displaychart(42,"$P2$");
}
else{
displaychart(cur_pos2,"$P2$");
}
}
else{
cur_pos2=cur_pos2-dice;
printf("Range exceeded of Player 2.\n");
displaychart(cur_pos2,"$P2$");
}
printf("Player 1 position is %d\n",cur_pos1);
break;
case '3':exit(0);
break;
default:printf("Incorrect choice.Try Again\n");
}
}
}