-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_with_classes.cpp
309 lines (273 loc) · 4.61 KB
/
game_with_classes.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <ctime>
using namespace std;
class Name
{
private:
string name1, name2;
int remenu;
public:
bool p1 = true;
int counter = 1;
bool start1 = false;
bool start2 = false;
int score1;
int score2;
int run;
void getname(string n1, string n2)
{
name1 = n1;
name2 = n2;
}
void setname()
{
cout << endl
<< "Players are : " << name1 << " & " << name2 << endl
<< endl;
}
void menu()
{
int n;
cout << "********** Main Menu *************" << endl;
cout << "Press 1 to start new Game" << endl;
cout << "Press 2 to Clear Screen" << endl;
cout << "Press 3 to Close Game" << endl;
cout << "------------------------------------->" << endl;
cin >> n;
cout << endl
<< endl;
remenu = n;
}
void againturn()
{
if (p1 == true)
cout << name1 << " Turn" << endl
<< endl;
else
cout << name2 << " Turn" << endl
<< endl;
}
void scoreandturnchnage()
{
if (counter % 2 == 0)
{
p1 = false;
if (run == 6)
{
start2 = true;
}
if (start2)
{
if (score2 == 26)
{
score2 += 32;
}
else if (score2 == 97)
{
score2 -= 62;
}
if (score2 + run > 100)
{
score2 -= run;
}
else
{
score2 += run;
}
}
}
else
{
p1 = true;
if (run == 6)
{
start1 = true;
}
if (start1)
{
if (score1 == 26)
{
score1 += 32;
}
else if (score1 == 97)
{
score1 -= 62;
}
if (score1 + run > 100)
{
score1 -= run;
}
else
{
score1 += run;
}
}
}
}
void turn()
{
int z;
srand(time(0)); // Initialize random number generator.
run = (rand() % 6) + 1;
cout << "Press 0 to take turn :" << endl;
cout << "Press 1 to Go back to menu" << endl;
cin >> z;
if (run == 6)
{
cout << "Take one more Turn due to 6" << endl;
}
else
{
counter++;
}
switch (z)
{
case 0:
system("CLS");
cout << name1 << "\x1b[5m Score is: \x1b[0m" << score1 << " || " << name2 << "\x1b[5m Score is: \x1b[0m" << score2 << endl;
cout << "------------------------------------->" << endl;
cout << "You Got: " << run << "." << endl
<< endl;
if (run == 6)
{
cout << "Take one more Turn due to you got 6 in last turn." << endl;
}
break;
case 1:
menu();
menuselect();
break;
default:
cout << "Wrong Selection" << endl;
// cout << "program closed";
turn();
// exit(3);
break;
}
scoreandturnchnage();
if (score1 == 100)
{
system("CLS");
cout << name1 << "\x1b[5m is WINNER \x1b[0m" << endl;
setscore();
menu();
menuselect();
}
if (score2 == 100)
{
system("CLS");
cout << name2 << "\x1b[5m is WINNER \x1b[0m" << endl;
setscore();
menu();
menuselect();
}
againturn();
startgame();
}
void startgame()
{
int row, col, number = 100, temp = 0;
cout << "# # # # # # # # # # # #" << endl;
cout << "# #" << endl;
for (row = 10; row > 0; row--)
{
cout << "# ";
for (col = 10; col > 0; col--)
{
number--;
if (number < 10)
{
cout << temp + number << " ";
}
else
{
if (number == 26)
{
cout << "\x1b[7m26\x1b[0m ";
continue;
}
if (number == 58)
{
cout << "\x1b[7m58\x1b[0m ";
continue;
}
if (number == 35)
{
cout << "\x1b[41m35\x1b[0m ";
continue;
}
if (number == 97)
{
cout << "\x1b[41m97\x1b[0m ";
continue;
}
else
{
cout << number << " ";
}
}
}
if (row == 1)
{
cout << " #" << endl;
}
else
{
cout << " #" << endl;
}
}
cout << "# #" << endl;
cout << "# # # # # # # # # # # #" << endl
<< endl;
cout << "\x1b[1mNOTE:\x1b[0m" << endl;
cout << "1. \x1b[3mJump: 26 -> 58 \x1b[0m" << endl;
cout << "2. \x1b[3mSnake Bite: 97 -> 35 \x1b[0m" << endl;
cout << "----------------------------->" << endl;
turn();
}
void menuselect()
{
switch (remenu)
{
case 1:
setscore();
startgame();
break;
case 2:
setscore();
system("CLS");
menu();
break;
case 3:
cout << "program closed";
exit(3);
break;
default:
cout << "Wrong Selection" << endl;
menuselect();
break;
}
}
void setscore()
{
score1 = 0;
score2 = 0;
start1 = false;
start2 = false;
}
};
int main()
{
string n1, n2;
cout << "Enter player 1 Name : ";
cin >> n1;
cout << "Enter player 2 Name : ";
cin >> n2;
Name s;
s.getname(n1, n2);
s.setname();
s.menu();
s.menuselect();
}