-
Notifications
You must be signed in to change notification settings - Fork 0
/
project1.cpp
293 lines (268 loc) · 8.5 KB
/
project1.cpp
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<string.h>
using namespace std;
class Snakes_Ladders
{
public:
int n,p1,p2,Score;
char li;
string name1,name2;
void DrawLine(int n,char li)
{
cout<<"\t\t\t";
for(int i=0;i<n;i++)
{
cout<<li;
}
}
void Board()
{
cout<<"\n";
DrawLine(70,'-');
cout<<"\n\n\t\t\t\t\t SNAKES POSITIONS ON THE BOARD\n\n";
DrawLine(70,'-');
cout <<"\n\t\t\tFrom 98 to 28";
cout <<"\n\t\t\tFrom 95 to 24";
cout <<"\n\t\t\tFrom 92 to 51";
cout <<"\n\t\t\tFrom 83 to 19";
cout <<"\n\t\t\tFrom 73 to 1";
cout <<"\n\t\t\tFrom 69 to 33";
cout <<"\n\t\t\tFrom 64 to 36";
cout <<"\n\t\t\tFrom 59 to 17";
cout <<"\n\t\t\tFrom 55 to 7";
cout <<"\n\t\t\tFrom 52 to 11";
cout <<"\n\t\t\tFrom 48 to 9";
cout <<"\n\t\t\tFrom 46 to 5";
cout <<"\n\t\t\tFrom 44 to 22\n";
DrawLine(70,'-');
cout<<"\n\n\t\t\t\t\t LADDERS POSITIONS ON THE BOARD\n\n";
DrawLine(70,'-');
cout<<"\n\t\t\tFrom 8 to 26";
cout<<"\n\t\t\tFrom 21 to 82";
cout<<"\n\t\t\tFrom 43 to 77";
cout<<"\n\t\t\tFrom 50 to 91";
cout<<"\n\t\t\tFrom 62 to 96";
cout<<"\n\t\t\tFrom 66 to 87";
cout<<"\n\t\t\tFrom 80 to 100\n";
DrawLine(70,'-');
cout<<endl;
}
void GameScore(string name1,string name2,int p1, int p2)
{
cout<<"\n\n";
DrawLine(70,'.');
cout<<"\n\n\t\t\t\t\t\t GAME STATUS\n\n";
DrawLine(70,'.');
cout << "\n\n\t\t\t" << name1 << " is at position " << p1 << endl;
cout << "\n\t\t\t" << name2 << " is at position " << p2 << endl;
DrawLine(70,'_');
cout<<endl;
}
void PlayDice(int &Score)
{
srand(time(0));
int Dice;
Dice = (rand()%6) + 1;
cout << "\n\t\t\tDice Rolled:\t\t" << Dice;
if(Dice==1)
{
cout <<"\n\t\t\t"<<" ----- " << endl;
cout <<"\t\t\t"<< "| |" << endl;
cout <<"\t\t\t"<< "| O |" << endl;
cout <<"\t\t\t"<< "| |" << endl;
cout <<"\t\t\t"<< " -----" << endl;
}
else if(Dice==2)
{
cout <<"\n\t\t\t"<< " -----" << endl;
cout <<"\t\t\t"<< "| O|" << endl;
cout <<"\t\t\t"<< "| |" << endl;
cout <<"\t\t\t"<< "|O |" << endl;
cout <<"\t\t\t"<< " -----" << endl;
}
else if(Dice==3)
{
cout <<"\n\t\t\t"<< " -----" << endl;
cout <<"\t\t\t"<< "| O|" << endl;
cout <<"\t\t\t"<< "| O |" << endl;
cout <<"\t\t\t"<< "|O |" << endl;
cout <<"\t\t\t"<< " -----" << endl;
}
else if(Dice==4)
{
cout <<"\n\t\t\t"<< " -----" << endl;
cout <<"\t\t\t"<< "|O O|" << endl;
cout <<"\t\t\t"<< "| |" << endl;
cout <<"\t\t\t"<< "|O O|" << endl;
cout <<"\t\t\t"<< " -----" << endl;
}
else if(Dice==5)
{
cout <<"\n\t\t\t"<< " -----" << endl;
cout <<"\t\t\t"<< "|O O|" << endl;
cout <<"\t\t\t"<< "| O |" << endl;
cout <<"\t\t\t"<< "|O O|" << endl;
cout <<"\t\t\t"<< " -----" << endl;
}
else if(Dice==6)
{
cout <<"\n\t\t\t"<< " -----" << endl;
cout <<"\t\t\t"<< "|O O|" << endl;
cout <<"\t\t\t"<< "|O O|" << endl;
cout <<"\t\t\t"<< "|O O|" << endl;
cout <<"\t\t\t"<< " -----" << endl;
}
Score = Score + Dice;
cout << "\n\t\t\tUpdated Position:\t" << Score;
switch (Score)
{
case 98:
Score = 28;
break;
case 95:
Score = 24;
break;
case 92:
Score = 51;
break;
case 83:
Score = 19;
break;
case 73:
Score = 1;
break;
case 69:
Score = 33;
break;
case 64:
Score = 36;
break;
case 59:
Score = 17;
break;
case 55:
Score = 7;
break;
case 52:
Score = 11;
break;
case 48:
Score = 9;
break;
case 46:
Score = 5;
break;
case 44:
Score = 22;
break;
case 8:
Score = 26;
break;
case 21:
Score = 82;
break;
case 43:
Score = 77;
break;
case 50:
Score = 91;
break;
case 54:
Score = 93;
break;
case 62:
Score = 96;
break;
case 66:
Score = 87;
break;
case 80:
Score = 100;
}
}
};
int main()
{
Snakes_Ladders obj;
int Player_1=0,Player_2=0,LastPosition;
string Player_1_Name,Player_2_Name;
int randomize();
obj.DrawLine(70,'=');
cout<<"\n\n\n\n\t\t\t\t\t\t SNAKE LADDER GAME\n\n\t\t\t\t\t By Chahel Gupta [C049, B2]\n\n\n\n";
obj.DrawLine(70,'=');
cout<<"\n\n\t\t\tEnter The Name of Player 1 : ";
getline(cin,Player_1_Name);
cout<<"\n\t\t\tEnter The Name of Player 2 : ";
getline(cin,Player_2_Name);
obj.Board();
while(Player_1<=100 && Player_2<=100)
{
obj.GameScore(Player_1_Name,Player_2_Name,Player_1,Player_2);
cout<<"\n\n\t\t\t***"<<Player_1_Name<<"'s Turn!***\n\t\t\tPress Any Key To Play";
getch();
LastPosition=Player_1;
obj.PlayDice(Player_1);
if(Player_1<LastPosition)
{
cout << "\n\a\t\t\tSNAKE ENCOUNTERD!\n";
cout << "\t\t\t ** ** ** " << endl;
cout << "\t\t\t * * * * * * * " << endl;
cout << "\t\t\t ** ** ** ** " << endl;
cout << "\t\t\tNew Position: "<< Player_1 << "\n";
}
else if(Player_1>LastPosition+6)
{
cout << "\n\a\t\t\tLADDER FOUND!\n";
cout << "\t\t\t-----------------------------" << endl;
cout << "\t\t\t | | | | | | | | | | | | | | " << endl;
cout << "\t\t\t-----------------------------" << endl;
cout << "\t\t\tNew Position: "<< Player_1 << "\n";
}
if(Player_1==100)
{
break;
}
cout<<"\n\n\t\t\t***"<<Player_2_Name<<"'s Turn!***\n\t\t\tPress Any Key To Play";
getch();
LastPosition=Player_2;
obj.PlayDice(Player_2);
if(Player_2<LastPosition)
{
cout << "\n\a\t\t\tSNAKE ENCOUNTERD!\n";
cout << "\t\t\t ** ** ** " << endl;
cout << "\t\t\t * * * * * * * " << endl;
cout << "\t\t\t ** ** ** ** " << endl;
cout << "\t\t\tNew Position: "<< Player_2 << "\n";
}
else if(Player_2>LastPosition+6)
{
cout << "\n\a\t\t\tLADDER FOUND!\n";
cout << "\t\t\t------------------------------" << endl;
cout << "\t\t\t | | | | | | | | | | | | | | " << endl;
cout << "\t\t\t------------------------------" << endl;
cout << "\t\t\tNew Position: "<< Player_2 << "\n";
}
if(Player_2==100)
{
break;
}
}
cout<<"\n\n";
obj.GameScore(Player_1_Name,Player_2_Name,Player_1,Player_2);
cout<<"\n\n";
obj.DrawLine(70,'*');
cout<<"\n\n\t\t\t\t\t\t RESULT\n\n";
obj.DrawLine(70,'*');
cout<<endl;
cout<<"\n\n";
if(Player_1 >= Player_2)
cout <<"\t\t\t"<< Player_1_Name << " IS THE WINNER!\n\n";
else
cout <<"\t\t\t"<< Player_2_Name << " IS THE WINNER!\n\n";
obj.DrawLine(70,'*');
getch();
}