-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.cpp
501 lines (434 loc) · 9.37 KB
/
game.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
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
490
491
492
493
494
495
496
497
498
499
500
501
#include<graphics.h>
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<timer.h>
#include<math.h>
#include<dos.h>
int gameOver = 1;
union REGS i, o;
void welcome(int maxx, int maxy) {
int midx=maxx/2;
int midy=maxy/2;
int p1=midx-220,p2=midy-120,p3=midx+220,p4=midy+120;
int pts[]={p1,p2,p3,p2,p3,p4,p1,p4,p1,p2};
setfillstyle(1,RED);
fillpoly(4,pts);
rectangle(p1-2,p2-2,p3+2,p4+2);
settextjustify(1,1);
settextstyle(4,0,0);
outtextxy(midx,p2+40,"Welcome");
outtextxy(midx,p2+80,"to");
settextstyle(4,0,5);
outtextxy(midx,p2+120,"The Minesweeper Game");
settextstyle(2,0,0);
outtextxy(midx,p4-10,"(Press any key to continue..)");
getch();
cleardevice();
}
void bgConsole(int maxx, int maxy) {
setbkcolor(9);
//border
rectangle(2,2,maxx-2,maxy-2);
rectangle(4,4,maxx-4,maxy-4);
//title background
setfillstyle(1,15);
int p[10]={maxx/2-100,0,maxx/2+100,0,maxx/2+100,15,maxx/2-100,15,maxx/2-100,15};
fillpoly(4,p);
//title
settextjustify(1,1);
settextstyle(2,0,7);
setcolor(9);
outtextxy(maxx/2,4,"MINESWEEPER GAME");
}
void smallBlock(int x, int y, int n) {
int pts[]={x,y,x+20,y,x+20,y+20,x,y+20,x,y};
if(n==1)
setfillstyle(1,8);
else
setfillstyle(1,7);
fillpoly(4,pts);
if(n==3) {
setfillstyle(1,8);
setcolor(8);
fillellipse(x+10,y+10,4,4);
setcolor(15);
}
if(n==4) {
settextjustify(1,1);
setcolor(8);
settextstyle(2,0,5);
outtextxy(x+10,y+7,"n");
setcolor(15);
}
}
void sidePanel(int maxx, int maxy) {
//STATISTICS
//Container
rectangle(450,45,maxx-25,maxy/2-12);
int pts1[]={510,40,555,40,555,60,510,60,555,40};
setcolor(9);
setfillstyle(1,9);
fillpoly(4,pts1);
setcolor(15);
settextstyle(2,0,5);
outtextxy(533,45,"STATS");
//Content
settextjustify(0,1);
outtextxy(460,70,"Score:");
outtextxy(460,120,"Moves:");
outtextxy(460,170,"Time:");
//HELP
int y=maxy/2+12;
//Container
rectangle(450,y,maxx-25,maxy-34);
int pts2[]={515,y-5,550,y-5,550,y+15,515,y+15,550,y-5};
setcolor(9);
setfillstyle(1,9);
fillpoly(4,pts2);
setcolor(15);
settextstyle(2,0,5);
settextjustify(1,1);
outtextxy(533,y,"HELP");
//Content
settextjustify(0,1);
outtextxy(460,y+25,"W - Move Up");
outtextxy(460,y+45,"A - Move Right");
outtextxy(460,y+65,"S - Move Down");
outtextxy(460,y+85,"D - Move Left");
outtextxy(460,y+105,"Space - Open Area");
settextstyle(2,0,4);
smallBlock(460,y+125,1);
outtextxy(485,y+128,"Closed");
outtextxy(485,y+138,"Area");
smallBlock(533,y+125,2);
outtextxy(558,y+128,"Mine-free");
outtextxy(558,y+138,"Area");
smallBlock(460,y+160,3);
outtextxy(485,y+163,"Mine in");
outtextxy(485,y+173,"the Area");
smallBlock(533,y+160,4);
settextstyle(2,0,4);
settextjustify(0,1);
outtextxy(558,y+163,"'n' Mines");
outtextxy(558,y+173,"Around");
}
void uncoveredBlock(int x, int y) {
int pts[]={x,y,x+50,y,x+50,y+50,x,y+50,x,y};
setfillstyle(1,7);
fillpoly(4,pts);
}
void bomb(int x, int y) {
uncoveredBlock(x,y);
setcolor(8);
setfillstyle(1,8);
fillellipse(x+25,y+25,10,10);
setcolor(15);
}
void bombAround(int x, int y, char ch[]) {
uncoveredBlock(x,y);
settextjustify(1,1);
settextstyle(1,0,5);
setcolor(8);
outtextxy(x+30,y+20,ch);
setcolor(15);
}
void coveredBlock(int x, int y) {
int pts[]={x,y,x+50,y,x+50,y+50,x,y+50,x,y};
setfillstyle(1,8);
fillpoly(4,pts);
}
void cursorBlock(int x, int y) {
setcolor(13);
rectangle(x,y,x+50,y+50);
setcolor(15);
}
char* convertChar(int a){
switch(a){
case 1:
return "1";
case 2:
return "2";
case 3:
return "3";
case 4:
return "4";
case 5:
return "5";
case 6:
return "6";
case 7:
return "7";
case 8:
return "8";
}
return "0";
}
class cell {
public:
int revealed,isBomb,neighbor;
int x,y,i,j,w;
void input(int i,int j,int w){
this->i = i;
this->j = j;
this->x = (i * w) + 25;
this->y = (j * w) + 45;
this->revealed = 0;
this->isBomb = 0;
this->neighbor = 0;
}
int countBomb(){
if(this->isBomb){
this->neighbor = -1;
return 0;
}
int total = 0;
for (int xoff = -1; xoff <= 1; xoff++) {
int i = this->i + xoff;
if (i < 0 || i > 7)
continue;
for (int yoff = -1; yoff <= 1; yoff++) {
int j = this->j + yoff;
if (j < 0 || j > 7)
continue;
cell n = grid[i][j];
if (n.isBomb) {
total++;
}
}
}
this->neighbor = total;
return 0;
}
void show(){
if(this->revealed){
if(this->isBomb){
//if cell is bomb
bomb(this->x,this->y);
} else if(this->neighbor > 0){
//if bomb present in neighbor
char* c = convertChar(this->neighbor);
bombAround(this->x,this->y,c);
} else {
//if blank cell
uncoveredBlock(this->x,this->y);
}
} else {
//if cell is not revealed
coveredBlock(this->x,this->y);
}
}
void gridCursor(){
cursorBlock(this->x,this->y);
}
void reveal(){
this->revealed = 1;
this->show();
if(this->neighbor == 0) {
// fill neighbors time
this->fillNeighbor();
//printf("Blank");
}
}
void fillNeighbor(){
for (int xoff = -1; xoff <= 1; xoff++) {
int i = this->i + xoff;
if (i < 0 || i > 7)
continue;
for (int yoff = -1; yoff <= 1; yoff++) {
int j = this->j + yoff;
if (j < 0 || j > 7)
continue;
if (!grid[i][j].isBomb && !grid[i][j].revealed) {
grid[i][j].reveal();
//printf("i : %d\tj: %d",i,j);
}
}
}
}
}grid[8][8];
int chCursor(int a, int s){
if(s==1 && a<7){
a++;
} else if(s==-1 && a>0){
a--;
}
return a;
}
void GameOver(int maxx, int maxy){
for(int i = 0 ; i < 8 ; i++){
for(int j = 0 ; j < 8 ; j++){
grid[i][j].revealed = 1;
grid[i][j].show();
}
}
delay(2500);
cleardevice();
setbkcolor(RED);
delay(1000);
setbkcolor(15);
setcolor(8);
settextstyle(1,0,3);
outtextxy(maxx/2,maxy/2-50,"You Failed, Mine Bursted!");
settextstyle(4,0,6);
setcolor(RED);
outtextxy(maxx/2,maxy/2,"Game Over");
delay(5000);
exit(0);
}
int initmouse() {
i.x.ax=0;
int86(0X33,&i,&o);
return o.x.ax;
}
void show_mouse() {
i.x.ax=1;
i.x.cx=getmaxx()/2;
i.x.dx=getmaxy()/2;
int86(0X33,&i,&o);
}
void get_mouse_pos(int *x,int *y,int *button) {
i.x.ax=3;
int86(0x33,&i,&o);
*x=o.x.cx;
*y=o.x.dx;
*button=o.x.bx;
}
void hide_mouse() {
i.x.ax=2;
int86(0x33,&i,&o);
}
int main() {
clrscr();
int gd=DETECT, gm, err, status;
initgraph(&gd,&gm,"..\\BGI");
err=graphresult();
if (err != grOk) { /* an error occurred */
printf("Graphics error: %s\n", grapherrormsg(err));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
int maxx=getmaxx();
int maxy=getmaxy();
status=initmouse();
if(status==0)
printf("\nMouse support not available.");
else
show_mouse();
welcome(maxx,maxy);
//background
bgConsole(maxx,maxy);
//board
setcolor(15);
rectangle(25,45,425,445);
//side panel
sidePanel(maxx,maxy);
int w = 50;
int i,j;
for(i = 0 ; i < 8 ; i++){
for(j = 0; j < 8 ; j++){
grid[i][j].input(i,j,w);
}
}
//calculating random position for bomb spot
int option[3][64];
int counter = 0;
for(i = 0 ; i < 8 ; i++){
for(j = 0; j < 8 ; j++){
option[0][counter] = i;
option[1][counter] = j;
option[2][counter] = 1;
counter++;
}
}
//planting bomb at random position
srand(time(NULL));
int plantBomb,plantI,plantJ,numBomb = 10;
for(i = 0; i < numBomb ; i++){
plantBomb = rand()%64;
if(option[2][plantBomb]){
plantI = option[0][plantBomb];
plantJ = option[1][plantBomb];
option[2][plantBomb] = 0;
grid[plantI][plantJ].isBomb = 1;
} else {
numBomb++;
}
}
//counting bombs
for(i = 0 ; i < 8 ; i++){
for(j = 0 ; j < 8 ; j++){
grid[i][j].countBomb();
}
}
int cursor_i=3, cursor_j = 4;
for(i = 0 ; i < 8 ; i++){
for(j = 0; j < 8 ; j++){
grid[i][j].show();
}
}
//main logic(game function)
int x,y,button;
do {
//display all grids
//grid[cursor_i][cursor_j].gridCursor();
/*switch(getch()){
case 119: //w
case 87:
cursor_j=chCursor(cursor_j,-1);
break;
case 97: //a
case 65:
cursor_i=chCursor(cursor_i,-1);
break;
case 115: //s
case 83:
cursor_j=chCursor(cursor_j,1);
break;
case 100: //d
case 68:
cursor_i=chCursor(cursor_i,1);
break;
case 27: //esc
gameOver = 0;
break;
case 32:
if(grid[cursor_i][cursor_j].isBomb){
GameOver(maxx,maxy);
//cout << "Opps!\t";
} else {
grid[cursor_i][cursor_j].reveal();
}
}*/
show_mouse();
get_mouse_pos(&x,&y,&button);
//x2=x1;
//y2=y1;
if(button==1)
{
get_mouse_pos(&x,&y,&button);
x-=25;
y-=45;
cursor_i=x/50;
cursor_j=y/50;
if(cursor_i>=0 && cursor_i<=7 && cursor_j>=0 && cursor_j<=7) {
if(grid[cursor_i][cursor_j].isBomb){
GameOver(maxx,maxy);
//cout << "Opps!\t";
} else {
grid[cursor_i][cursor_j].reveal();
// hide_mouse();
} }
//get_mouse_pos(&x2,&y2,&button);
}
if(kbhit()) {
gameOver=0;
break;}
// displayTime();
// delay(1000);
}while(gameOver);
closegraph();
return 0;
}