-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.h
More file actions
336 lines (309 loc) · 7.86 KB
/
game.h
File metadata and controls
336 lines (309 loc) · 7.86 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//Earlier I made a class nameg game with static memeber but I dun know for some reasons when I assigned a value to private static members of the class the
//complier showed an error saying unresolved externals so I switched to namespaces for semantic reasons.
//all functions were parameterized that the did not take global variable for the chosen word etc tht gave me better extensibility but tht created confusion
//for me so i removed them for now
namespace game
{
int level;
int const numberofwords=5;
bool stopgame;
struct status
{
int score,correct,wrong;
}status;
struct statusloc
{
int correctstatusX,correctstatusY;
int wrongstatusX,wrongstatusY;
int scorestatusX,scorestatusY;
}statusloc;
struct word
{
int wordstartX,wordstartY;
int wordendX,wordendY;
int length;
int speedofanimation;
bool bothcases;//upper and lower cases both?
}word;
struct currentword
{
char shabd[max];
int correctcharactercount;
bool timeover;
bool done;
}currentword;
int getrandom(int range_min,int range_max)
{
int random;
do
{
random = (double)rand() / (RAND_MAX + 1) * (range_max - range_min) + range_min;
}while(random>=91 && random<=96);
return random;
}
void generateword(int length)
{
char randomword[max];
srand((unsigned int)time(NULL));
for (int i=0;i<length;i++)
{
if (word.bothcases) randomword[i]=getrandom(65,123);
else randomword[i]=getrandom(97,123);
randomword[i+1]=0;
}
strcpy(currentword.shabd,randomword);
}
void renderword(int x,int y)
{
console::gotoxy(x,y);
int i;
for (i=0;i<currentword.correctcharactercount;i++)
{
console::textcolor(FOREGROUND_GREEN);
cout.put(currentword.shabd[i]);
}
console::textcolor(FOREGROUND_RED);
for (;i<word.length;i++) cout.put(currentword.shabd[i]);
}
void renderheader(int level)
{
console::textcolor(4|2);
console::gotoxy(34,2);
switch(level)
{
case 1:
cout<<"VERY EASY\n\n";
break;
case 2:
cout<<"EASY\n\n";
break;
case 3:
cout<<"BEGINNER\n\n";
break;
case 4:
cout<<"INTERMEDIATE\n\n";
break;
case 5:
cout<<"ADVANCED\n\n";
break;
default:
cout<<"Invalid Level Selected\n";
cout<<"The application will exit now\n";
getch();
exit(0);
}
console::drawHline(1,80);
}
void rendercorrectbox()
{
console::textcolor(4|2);
console::gotoxy(5,console::getcursory()+1);
console::drawHline(console::getcursorx(),console::getcursorx()+15);
console::gotoxy(5,console::getcursory()+1);
cout<<"| CORRECT : |\n";
int correctinptxy[]={17,console::getcursory()-1};
console::gotoxy(5,console::getcursory());
console::drawHline(console::getcursorx(),console::getcursorx()+15);
statusloc.correctstatusX=correctinptxy[0];
statusloc.correctstatusY=correctinptxy[1];
}
void renderwrongbox()
{
console::textcolor(4|2);
console::gotoxy(5,console::getcursory()+1);
console::drawHline(console::getcursorx(),console::getcursorx()+13);
console::gotoxy(5,console::getcursory()+1);
cout<<"| WRONG : |\n";
int correctinptxy[]={5+10,console::getcursory()-1};
console::gotoxy(5,console::getcursory());
console::drawHline(console::getcursorx(),console::getcursorx()+13);
statusloc.wrongstatusX=correctinptxy[0];
statusloc.wrongstatusY=correctinptxy[1];
}
void renderscorebox()
{
console::textcolor(4|2);
console::gotoxy(60,statusloc.correctstatusY-1);
console::drawHline(console::getcursorx(),console::getcursorx()+13);
console::gotoxy(60,console::getcursory()+1);
cout<<"| SCORE : |\n";
int correctinptxy[]={60+10,console::getcursory()-1};
console::gotoxy(60,console::getcursory());
console::drawHline(console::getcursorx(),console::getcursorx()+13);
statusloc.scorestatusX=correctinptxy[0];
statusloc.scorestatusY=correctinptxy[1];
}
void renderseparators()
{
console::textcolor(4|2);
console::gotoxy(0,56);
console::drawHline(console::getcursorx(),console::getcursorx()+79);
//add more separators to make the game look beautiful
}
void renderfooter(int level)
{
console::textcolor(4|2);
console::gotoxy(0,48);
console::drawHline(1,80);
rendercorrectbox();
renderwrongbox();
renderscorebox();
renderseparators();
}
void animateword(void* dummy)
{
int startY=word.wordstartY;
int endY=word.wordendY;
int startX=word.wordstartX;
for(int i=startY;i<=endY;i+=word.speedofanimation)
{
if (stopgame) _endthread();
renderword(startX,i);
Beep(800,100);
Sleep(300);
if (currentword.done==TRUE)
{
renderword(startX,i);
Beep(800,100);
_endthread();
}
}
currentword.timeover=TRUE;
console::gotoxy(25,47);
cout<<"TIME OVER. Press anny key to continue.";
_endthread();
}
void updatestatus(int score,int correct,int wrong)
{
console::gotoxy(statusloc.correctstatusX,statusloc.correctstatusY);
cout<<correct;
console::gotoxy(statusloc.wrongstatusX,statusloc.wrongstatusY);
cout<<wrong;
console::gotoxy(statusloc.scorestatusX,statusloc.scorestatusY);
cout<<score;
}
void start()
{
HANDLE threadhandle;
threadhandle=(HANDLE) _beginthread(animateword,0,NULL);//creating a new thread here and starting it which does the animation of the word
char ch;
for (int i=0;i<word.length;i++)
{
if (currentword.timeover)
{
updatestatus(status.score,status.correct,++status.wrong);
return;
}
if ((ch=getch())==currentword.shabd[i]) currentword.correctcharactercount++;
else
{
if (ch==27)//to RETURN TO MENU WHEN USER PRESSES ESC KEY
{
stopgame=TRUE;
return;
}
i--;
}
}
currentword.done=TRUE;
WaitForSingleObject(threadhandle,INFINITE);
//Sleep(1000);//this is bad technique Im letting this thread sleep so tht the animateword thread gets finished. I shoud use some sync techniques
//new I have used a proper sync operation waitforsingleobject will wait unless _endthread is called on animate thread.
//I dun hav multiple threading sharing a resource like if I had to cout from 2 different threads I would have to use mutex sync objects.
updatestatus(++status.score,++status.correct,status.wrong);
console::gotoxy(25,47);
cout<<"Press any key to continue.";
getch();
}
void setlevelparameters(int level)
{
switch (level)
{
case 1:
word.length=3;
word.speedofanimation=1;
word.bothcases=FALSE;
break;
case 2:
word.length=5;
word.speedofanimation=1;
word.bothcases=FALSE;
break;
case 3:
word.length=5;
word.speedofanimation=3;
word.bothcases=FALSE;
break;
case 4:
word.length=7;
word.speedofanimation=5;
word.bothcases=FALSE;
break;
case 5:
word.length=10;
word.speedofanimation=7;
word.bothcases=TRUE;
}
}
void inline setwordstartendcords()
{
word.wordstartX=34;
word.wordstartY=5;
word.wordendX=34;
word.wordendY=45;
}
void resetcurrentwordstats()
{
currentword.correctcharactercount=0;
currentword.done=FALSE;
currentword.timeover=FALSE;
}
void newwordinit()
{
system("CLS");
resetcurrentwordstats();
renderheader(level);
renderfooter(level);
updatestatus(status.score,status.correct,status.wrong);
generateword(word.length);
}
void reset()
{
status.correct=0;
status.score=0;
status.wrong=0;
stopgame=FALSE;
currentword.correctcharactercount=0;
currentword.done=FALSE;
currentword.timeover=FALSE;
}
void init(int level)
{
system("CLS");
reset();
game::level=level;
setlevelparameters(game::level);
setwordstartendcords();
renderheader(level);
renderfooter(level);
console::gotoxy(25,47);
cout<<"Press any key to start the game";
getch();
console::gotoxy(25,47);
cout<<" ";
generateword(word.length);
for (int i=0;i<numberofwords;i++)
{
if (stopgame) return;
start();
newwordinit();
}
console::gotoxy(25,25);
console::textcolor(FOREGROUND_RED);
cout<<"THANK YOU FOR PLAYING THE GAME";
console::gotoxy(25,25+2);
console::textcolor(4|2);
cout<<"YOUR SCORE IS : "<<status.score;
getch();
}
}