-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
489 lines (434 loc) · 13.6 KB
/
main.cpp
File metadata and controls
489 lines (434 loc) · 13.6 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#define _WIN32_WINNT 0x0500 //MOUSE POS
#include <iostream> //COUT AND CIN
#include<windows.h> //COLOURS AND VARIOUS BELLS AND WHISTLES
#include<time.h> //SEDDING RANDOM VALUES
#include <limits.h> //FOR MOUSEPOS
#include<conio.h> //KBHIT() AND GETCH()
#include<fstream> //FOR HIGHSCORE FILE
#include<stdio.h>
using namespace std;
int main(); //TO CALL MAIN ;)
//GLOBAL VARIABLES
char ch;int num;int turn=29,t=29;int uc;int a;int mx,my;int xpre,ypre;
char title[124] = "DRENCH - the world's simplest flash game";
//CLASS AND OBJECTS
class board
{
public:
bool state;
char sym;
};
board coin[10][15];
class score
{
public:
char name[30];
int mark;
};
//SYSTEM CONTROL FUNCTIONS
void gotoxy (int x, int y) //CURSOR COORDINATES
{
COORD coord;
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void setcursor(bool visible,DWORD size) //REMOVE BLINKING CURSOR
{
HANDLE console=GetStdHandle(STD_OUTPUT_HANDLE);
if(size==0)
{
size=20;
}
CONSOLE_CURSOR_INFO lpCursor;
lpCursor.bVisible=visible;
lpCursor.dwSize=size;
SetConsoleCursorInfo(console,&lpCursor);
}
//USER DEFINED FUNCTIONS
void highscore()
{
score sc,ss;
char x;char name[20];
fstream file;
file.open("Score",ios::app|ios::in|ios::out|ios::binary);
cout<<"Want to Save Your Score?.....(y)";
cin>>x;
if(x=='y'||x=='Y')
{
cin.clear();
cout<<"Enter Name...";cin.ignore();
cin.getline(sc.name,30);
sc.mark=t-turn*10;
file.write((char *)&sc,sizeof(sc));
file.seekg(ios::beg);
file.read((char *)&ss,sizeof(ss));
cout<<ss.name<<"\t"<<ss.mark;
x='n';
}
file.close();
}
void border() //CUSTOM BORDER
{
int c,r;
for(c=0;c<80;c++) //HORIZONTAL
{
gotoxy(c,0);
cout<<'%';
gotoxy(c,24);
cout<<'%';
}
for(r=0;r<24;r++) //VERTICAL
{
gotoxy(0,r);
cout<<'%';
gotoxy(79,r);
cout<<'%';
}
}
void clear() //CLEAR DRENCH BOARD
{
for(int i=3;i<33;++i)
for(int k=2;k<22;++k)
{
gotoxy(i,k);
cout<<" ";
}
}
void mousepos() //MOUSE CHARACTER COORDINATES
{
// Get screen frame rate, as there's no point going any faster
DWORD frameRate = 0;
DEVMODEA devMode = {0};
devMode.dmSize = sizeof(DEVMODE);
if(EnumDisplaySettingsA(NULL, ENUM_CURRENT_SETTINGS, &devMode))
frameRate = devMode.dmDisplayFrequency;
else
frameRate = 60; // default value if fail to get actual value
// get our window
HWND hWnd = GetConsoleWindow();
// to remember where we were last time
POINT posLast = {LONG_MIN, LONG_MIN};
while(1)
{
// get current mouse position in screen coords
POINT pos = {0, 0};
if (GetCursorPos(&pos))
{
// convert position to client window coords
if (ScreenToClient(hWnd, &pos))
{
// get window's client rect
RECT rcClient = {0};
GetClientRect(hWnd, &rcClient);
// if mouse cursor is inside rect
if (PtInRect(&rcClient, pos))
{
// and if we've moved since last time
if ((posLast.x != pos.x) || (posLast.y != pos.y))
{
// get size and position of console window/buffer in rows and columns
CONSOLE_SCREEN_BUFFER_INFO info = {0};
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
const SMALL_RECT& smRect = info.srWindow;
// calculate row and column corresponding to mouse position
// (note this takes accounts of console buffer scrolling)
mx = smRect.Left + (pos.x * (smRect.Right - smRect.Left))
/ (rcClient.right - rcClient.left);
my = smRect.Top + (pos.y * (smRect.Bottom - smRect.Top ))
/ (rcClient.bottom - rcClient.top );
// and last position
posLast = pos;
}
}
else
{
//Do nothing
}
Sleep(10000 / frameRate);
break;
}
}
}
}
void disp(void) //DISPLAY FUNCTION
{
//CONVERTING SYMBOLS IN EACH ARRAY ELEMENT TO COLOURED SQUARE
cout<<"\n\n";
for(int i=0;i<20;++i)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);//COLOUR
cout<<"% ";
for(int j=0;j<30;++j)
{
switch(coin[i/2][j/2].sym)
{
case '!':
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
cout<<char(219);
break;
case '@':
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
cout<<char(219);
break;
case '#':
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
cout<<char(219);
break;
case '$':
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
cout<<char(219);
break;
case '%':
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
cout<<char(219);
break;
case '*':
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<char(219);
break;
}
}
cout<<endl;
}
//DRENCH BOARD BORDER
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
for(int i=0;i<30;++i)
{
gotoxy(3+i,1);
cout<<char(209);
gotoxy(3+i,22);
cout<<char(207);
}
for(int i=0;i<20;++i)
{
gotoxy(2,i+2);
cout<<char(186);
gotoxy(33,i+2);
cout<<char(186);
}
gotoxy(2,1);cout<<char(201);
gotoxy(33,1);cout<<char(187);
gotoxy(33,22);cout<<char(188);
gotoxy(2,22);cout<<char(200);
gotoxy(40,17);
cout<<"YOU HAVE "<<turn<<" MOVES TO DRENCH THE BOARD";
gotoxy(65,21);
cout<<"'H' FOR HELP";
gotoxy(63,22);
cout<<"'ESC' FOR EXIT";
}
void iface() //MOUSE AND KEYBOARD INPUT
{
uc=0;
while(uc!=1 && uc!=2 && uc!=3 && uc!=4 && uc!=5 && uc!=6)
{
//DISPLAY OPTIONS
gotoxy(40,10);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
cout<<" |1|";//<<char(2);
gotoxy(52,10);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
cout<<" |2|";//<<char(2);
gotoxy(64,10);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
cout<<" |3|";//<<char(2);
gotoxy(40,5);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
cout<<" |4|";//<<char(2);
gotoxy(52,5);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
cout<<" |5|";//<<char(2);
gotoxy(64,5);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<" |6|";//<<char(2);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
help:
if(kbhit()) //KEYBOARD INPUT
{
char f=getch();
if(f=='h') //HELP SECTION IF 'H'
{
gotoxy(3,4+40);cout<<"How to Play...";
gotoxy(3,7+40);cout<<"1. Drench the board from ";
gotoxy(3,8+40);cout<<" top left in one colour";
gotoxy(3,10+40);cout<<"2. Click on a colour";
gotoxy(3,12+40);cout<<"3. Enter a corresponding number";
gotoxy(3,15+40);cout<<"Enjoy!!!";
gotoxy(3,15+46);
Sleep(3210);
gotoxy(0,0);
goto help;
}
else //ELSE INPUT NUMBER
uc=f-48;
}
if((GetKeyState(0x1B)<-100)) //ESCAPE KEY PRESS
{
gotoxy(0,100);
exit(0);
}
if ((GetKeyState(VK_LBUTTON) & 0x80) != 0) //MOUSE INPUT
{
mousepos(); //GET MOUSE COORDINATES
//CHECK MOUSE POSITION TO DETERMINE COLOUR
if(mx>=42 && mx<=45 && my>=9 && my<=11)
uc=1;
else if(mx>=54 && mx<=57 && my>=9 && my<=11)
uc=2;
else if(mx>=66 && mx<=69 && my>=9 && my<=11)
uc=3;
else if(mx>=42 && mx<=45 && my>=4 && my<=6)
uc=4;
else if(mx>=54 && mx<=57 && my>=4 && my<=6)
uc=5;
else if(mx>=66 && mx<=69 && my>=4 && my<=6)
uc=6;
}
}
//ASSIGN APPROPRIATE SYMBOL
switch(uc)
{
case 1:ch='!'; break;
case 2:ch='@'; break;
case 3:ch='#'; break;
case 4:ch='$'; break;
case 5:ch='%'; break;
case 6:ch='*'; break;
}
gotoxy(0,0);
}
void init() //INITIALISE BOARD
{
for(int i=0;i<10;++i)
{
for(int j=0;j<15;++j)
{
coin[i][j].state=false;
num=rand()%6; //RANDOM NUMBER SELECTION
switch(num) //ASSIGN COLOURS
{
case 0:coin[i][j].sym='@';
break;
case 1:coin[i][j].sym='#';
break;
case 2:coin[i][j].sym='$';
break;
case 3:coin[i][j].sym='%';
break;
case 4:coin[i][j].sym='*';
break;
case 5:coin[i][j].sym='!';
break;
}
}
}
}
bool test() //CHECK FOR WIN CONDITION
{
int a=1;
char ch=coin[0][0].sym; //COLOUR OF TOP LEFT ELEMENT
for(int i=0;i<10;++i)
{
for(int j=0;j<15;++j)
{
if(coin[i][j].sym==ch)
continue;
else //RETURN FALSE IF DIFFERENT COLOUR 'CH'
{
a=0;
return false;
}
}
}
if(a==1) //RETURN TRUE IF ALL SAME COLOUR
return true;
return 0;
}
void game(void) //GAME FUNCTION
{
coin[0][0].state=true; //MAKE TOP LEFT ELEMENT TRUE
while(turn>=0)
{
iface(); //CALLING INTERFACE
for(int i=0;i<10;++i)
{
for(int j=0;j<15;++j)
{
if(coin[i][j].state==true) //IF ELEMENT IS TRUE
{ //MAKE ADJACENT ELEMENTS OF SAME COLOUR TRUE
coin[i][j].sym=ch;
if(coin[i+1][j].sym==ch && i<10)
coin[i+1][j].state=true;
if(coin[i][j+1].sym==ch && j<15)
coin[i][j+1].state=true;
if(coin[i-1][j].sym==ch && i>0)
coin[i-1][j].state=true;
if(coin[i][j-1].sym==ch && j>0)
coin[i][j-1].state=true;
}
}
}
clear(); //CALLING CLEAR BOARD
gotoxy(0,0);
disp(); //CALLING DISPLAY
if(test()) //WIN CONDITION
{
system("cls");
gotoxy(26,11);cout<<"YOU HAVE DONE IT......!!!";
gotoxy(28,13);cout<<"NEXT LEVEL?...(Y)";
char xx;
cin>>xx;
if(xx=='y' || xx=='Y')
{
t--; //DECREASE NUMBER OF TURNS
turn=t;
main(); //REPLAY
}
Sleep(101);
exit(0);
}
if(turn==0 && test()==false) //LOSE OR TURNS OVER CONDITION
{
system("cls");
gotoxy(29,11);cout<<"REFRESH?......(Y)!!!";
char xx;
cin>>xx;
if(xx=='y' || xx=='Y')
{
turn=t;
main(); //REPLAY
}
Sleep(101);
exit(0);
}
turn--;
}
}
//MAIN FUNCTION
int main() //MAIN FUNCTION
{
highscore();
/*
system("cls");
cout<<"Hello World!"<<endl;
Sleep(666);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15); //COLOUR
SetConsoleTitleA(title);
Sleep(101);
srand(time(NULL));
setcursor(0,0);
border();
gotoxy(0,0);
init();
disp();
gotoxy(40,17);
cout<<"YOU HAVE "<<turn+1<<" MOVES TO DRENCH THE BOARD";
game();
getch();
*/
return 0;
}
//************************************CREDITS******************************************
// FLASHBYNIGHT (THE ORIGINAL GAME ... http://www.flashbynight.com/drench/ )
// cplusplus.com ,FOR THE HELP AND SUPPORT (AND THE MOUSEPOS FUNCTION)
// VEDANT, RAVIKIRAN AND SUSHIL FOR IDEAS, TROUBLESHOOTING AND SUPPORT