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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
| #include <conio.h>
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include "mouse.c"
#define YES 1
#define NO 0
#define XPX 15 /* X pixels by square */
#define YPX 15 /* Y pixels by square */
#define DEFCX 30 /* Default number of squares */
#define DEFCY 28
#define MINE 255-'0' /* So that when it prints, it prints char 0xff */
#define STSQUARE struct stsquare
STSQUARE {
unsigned char value; /* Number of mines in the surround squares */
unsigned char sqopen; /* Square is open */
unsigned char sqpress; /* Square is pressed */
unsigned char sqmark; /* Square is marked */
} *psquare;
#define value(x,y) (psquare+(x)*ncy+(y))->value
#define sqopen(x,y) (psquare+(x)*ncy+(y))->sqopen
#define sqpress(x,y) (psquare+(x)*ncy+(y))->sqpress
#define sqmark(x,y) (psquare+(x)*ncy+(y))->sqmark
int XST, /* Offset of first pixel X */
YST,
ncx, /* Number of squares in X */
ncy,
cmines, /* Mines discovered */
initmines, /* Number of initial mines */
sqclosed, /* Squares still closed */
maxy; /* Max. number of y pixels of the screen */
void Graph_init(void);
void Read_param(int argc, char *argv[]);
void Set_mines(int nminas);
void Set_square(int x, int y, int status);
void Mouse_set(void);
void Draw_squares(void);
int Do_all(void);
void Blow_up(void);
void Open_square(int x, int y);
int Open_near_squares(int x, int y);
/************************************************************************/
void main(int argc, char *argv[])
{
if (!mouse_reset()) {
cputs(" ERROR: I can't find a mouse driver\r\n");
exit(2);
}
Graph_init();
Read_param(argc, argv);
Mouse_set();
do {
randomize();
cleardevice();
Set_mines(cmines=initmines);
mouse_enable();
Draw_squares();
}
while (Do_all() != '\x1b');
closegraph();
}
/*************************************************************************
* *
* F U N C T I O N S *
* *
*************************************************************************/
/*----------------------------------------------------------------------*/
void Graph_init(void)
{
int graphdriver=DETECT, graphmode, errorcode;
errorcode=registerbgidriver(EGAVGA_driver);
if(errorcode < 0) {
cprintf("\n\rGraphics System Error: %s\n",grapherrormsg(errorcode));
exit(98);
}
initgraph(&graphdriver, &graphmode, "");
errorcode=graphresult();
if(errorcode!=grOk) {
printf(" Graphics System Error: %s\n",grapherrormsg(errorcode));
exit(98);
}
maxy=getmaxy();
} /* Graph_init */
/*----------------------------------------------------------------------*/
void Read_param(int argc, char *argv[])
{
int x, y, m;
x=y=m=0;
if (argc!=1) {
if (!isdigit(*argv[1])) {
closegraph();
cprintf("Usage is: %s [x] [y] [m]\r\n\n"
"Where x is the horizontal size\r\n"
" y is the vertical size\r\n"
" m is the number of mines\r\n\n"
" Left mouse button: Open the square\r\n"
"Right mouse button: Mark the square\r\n"
" -The first time puts a 'mine' mark\r\n"
" -The second time puts a 'possible "
"mine' mark\r\n"
" -The third time unmarks the square\r\n"
"Left+Right buttons: Open the surrounded squares only if "
"the count of mines\r\n"
" is equal to the number in the square",argv[0]);
exit (1);
}
switch (argc) {
case 4: m=atoi(argv[3]);
case 3: y=atoi(argv[2]);
case 2: x=atoi(argv[1]);
}
}
XST=100;
ncx=DEFCX;
if (maxy==479) {
YST=30;
ncy=DEFCY;
}
else {
YST=25;
ncy=20;
}
if (x>0 && x<ncx)
ncx=x;
if (y>0 && y<ncy) {
YST+=((ncy-y)*YPX)>>1;
ncy=y;
}
initmines= m ? m : ncx*ncy*4/25; /* There are about 16% of mines */
if ((psquare=calloc(ncx*ncy, sizeof(STSQUARE)))==NULL) {
closegraph();
cputs("ERROR: Not enought memory");
exit(3);
}
} /* Read_param */
/*----------------------------------------------------------------------*/
void Set_mines(int nminas)
{
STSQUARE *p;
int i, x, y, a, b;
sqclosed=ncx*ncy-nminas;
p=psquare;
for (i=ncx*ncy; i>0; i--, p++)
p->value = p->sqopen = p->sqpress = p->sqmark = NO;
for (i=nminas; i>0; i--) {
while (value(x=random(ncx), y=random(ncy)) == MINE)
;
value(x,y)=MINE;
}
for (x=ncx-1; x>=0; x--) {
for (y=ncy-1; y>=0; y--) {
if (value(x,y) == MINE)
continue;
a=x-1;
b=y-1;
if (a>=0 && b>=0 && a<ncx && b<ncy)
value(x,y)+=(value(a,b) == MINE);
a++;
if (a>=0 && b>=0 && a<ncx && b<ncy)
value(x,y)+=(value(a,b) == MINE);
a++;
if (a>=0 && b>=0 && a<ncx && b<ncy)
value(x,y)+=(value(a,b) == MINE);
b++;
if (a>=0 && b>=0 && a<ncx && b<ncy)
value(x,y)+=(value(a,b) == MINE);
a-=2;
if (a>=0 && b>=0 && a<ncx && b<ncy)
value(x,y)+=(value(a,b) == MINE);
b++;
if (a>=0 && b>=0 && a<ncx && b<ncy)
value(x,y)+=(value(a,b) == MINE);
a++;
if (a>=0 && b>=0 && a<ncx && b<ncy)
value(x,y)+=(value(a,b) == MINE);
a++;
if (a>=0 && b>=0 && a<ncx && b<ncy)
value(x,y)+=(value(a,b) == MINE);
}
}
} /* Set_mines */
/*----------------------------------------------------------------------*/
/*
If status=1 then draw the square up, if it's 0 then draw it down
*/
void Set_square(int x, int y, int status)
{
int xl, xr, yt, yb;
mouse_disable();
xl=XST+XPX*x;
xr=xl+XPX-1;
yt=YST+YPX*y;
yb=yt+YPX-1;
setfillstyle(SOLID_FILL, LIGHTGRAY);
bar(xl, yt, xr, yb);
if (status) {
setcolor(WHITE);
line(xl,yt,xl,yb-1);
line(xl,yt,xr-1,yt);
setcolor(DARKGRAY);
line(xr,yb,xl,yb);
line(xr,yb,xr,yt);
switch (sqmark(x,y)) {
case 2 : setfillstyle(SOLID_FILL, LIGHTBLUE);
setcolor(LIGHTBLUE);
break;
case 1 : setfillstyle(SOLID_FILL, LIGHTRED);
setcolor(LIGHTRED);
break;
default: setcolor(LIGHTGRAY);
}
fillellipse(XST+XPX*x+XPX/2, YST+YPX*y+YPX/2, 3, 3);
switch (sqmark(x,y)) {
case 2 :
case 1 : setfillstyle(SOLID_FILL, YELLOW);
setcolor(YELLOW);
fillellipse(XST+XPX*x+XPX/2, YST+YPX*y+YPX/2, 1, 1);
}
}
else {
sqclosed--;
setcolor(DARKGRAY);
line(xl,yt,xl,yb);
line(xl,yt,xr,yt);
}
mouse_enable();
} /* Set_square */
/*----------------------------------------------------------------------*/
void Mouse_set(void)
{
mouse_horizontal_range(XST, XST+XPX*ncx-3);
mouse_vertical_range(YST, YST+YPX*ncy-3);
} /* Mouse_set */
/*----------------------------------------------------------------------*/
void Draw_squares(void)
{
int x, y;
mouse_disable();
for (x=0; x<ncx; x++)
for (y=0; y<ncy; y++)
Set_square(x,y,1);
mouse_enable();
} /* Draw_squares */
/*----------------------------------------------------------------------*/
/*
Here we do all the job
*/
int Do_all(void)
{
int x, y, xant=-1, yant, mlstant, mrstant, cminasant=0, ttrans, tant=0;
long tst, tact;
char str[] ="xxxxx: 0000",
blk[] ="ЫЫЫЫЫЫЫЫЫЫЫ";
mouse_enable();
time(&tst);
while (!kbhit()) {
if (!sqclosed && !mouse_status) { /* All mines discovered */
settextjustify(LEFT_TEXT, CENTER_TEXT);
setcolor(BLACK);
outtextxy(0, maxy/2-10, blk);
sprintf(str,"Mines: 0");
setcolor(LIGHTRED);
outtextxy(0, maxy/2-10, str);
settextjustify(LEFT_TEXT, CENTER_TEXT);
setcolor(BLACK);
outtextxy(0, maxy/2+10, blk);
sprintf(str," Time: %4d",ttrans);
setcolor(LIGHTGREEN);
outtextxy(0, maxy/2+10, str);
for (x=ncx-1; x>=0; x--)
for (y=ncy-1; y>=0; y--)
if (value(x,y)==MINE) {
sqmark(x,y)=1;
Set_square(x,y,1);
}
break;
}
if (cminasant != cmines) { /* Update Mines: */
cminasant=cmines;
settextjustify(LEFT_TEXT, CENTER_TEXT);
setcolor(BLACK);
outtextxy(0, maxy/2-10, blk);
sprintf(str,"Mines: %4d",cmines);
setcolor(LIGHTRED);
outtextxy(0, maxy/2-10, str);
}
if (tant != (ttrans=(int)(time(&tact)-tst))) { /* Update Time: */
tant=ttrans;
settextjustify(LEFT_TEXT, CENTER_TEXT);
setcolor(BLACK);
outtextxy(0, maxy/2+10, blk);
sprintf(str," Time: %4d",ttrans);
setcolor(LIGHTRED);
outtextxy(0, maxy/2+10, str);
}
mouse_read_cursor();
mouse_status&=3;
x=(mouse_x-XST)/XPX;
y=(mouse_y-YST)/YPX;
if (xant == -1) {
xant=x;
yant=y;
}
if (x!=xant || y!=yant) { /* Position change */
if (sqpress(xant,yant) && !sqopen(xant,yant)) {
sqpress(xant,yant)=NO;
Set_square(xant,yant,1);
}
xant=x;
yant=y;
}
if (!mouse_status)
mlstant=mrstant=0;
if ((mouse_status == 2) &&
!sqopen(x,y) &&
mlstant!=mouse_status) { /* Right button pressed */
mlstant=mouse_status;
mouse_disable();
switch (sqmark(x,y)=(sqmark(x,y)+1) % 3) {
case 1 : cmines--; break;
case 2 : cmines++;
}
Set_square(x,y,1);
mouse_enable();
}
else if (mouse_status==1 && /* Left button pressed */
!sqopen(x,y) &&
!sqpress(x,y) && /* And square not pressed */
sqmark(x,y)!=1) {
sqpress(x,y)=YES;
Set_square(x,y,0);
}
else if (!mouse_status && /* Left button released */
!sqopen(x,y) &&
sqpress(x,y) &&
sqmark(x,y)!=1) { /* Open the square */
if (value(x,y) == MINE) {
Blow_up();
break;
}
else
Open_square(x,y);
}
else if (mouse_status==3 &&
sqopen(x,y) &&
mrstant!=mouse_status) { /* Open near squares */
mrstant=mouse_status;
if (Open_near_squares(x,y) == YES) {
Blow_up();
break;
}
}
}
mouse_disable();
return (getch());
} /* Do_all */
/*----------------------------------------------------------------------*/
void Blow_up(void)
{
int x, y, xl, yt, xr, yb;
mouse_disable();
for (x=0; x<ncx; x++) {
for (y=0; y<ncy; y++) {
if (value(x,y) == MINE) {
settextjustify(CENTER_TEXT, CENTER_TEXT);
setcolor(LIGHTRED);
outtextxy(XST+XPX*x+XPX/2-1, YST+YPX*y+YPX/2, "Ы");
setcolor(BLACK);
outtextxy(XST+XPX*x+XPX/2, YST+YPX*y+YPX/2+1, "*");
}
else if (sqmark(x,y)==1) {
setcolor(YELLOW);
xl=XST+XPX*x;
xr=xl+XPX-1;
yt=YST+YPX*y;
yb=yt+YPX-1;
line(xl,yt,xr,yb);
line(xl,yb,xr,yt);
}
}
}
} /* Blow_up */
/*----------------------------------------------------------------------*/
void Open_square(int x, int y)
{
char num[2]="0";
int a, b, c;
sqopen(x,y)=YES;
if ((num[0]=value(x,y)) != 0) {
num[0]+='0';
switch(num[0]) {
case '1' : c=LIGHTBLUE; break;
case '2' : c=LIGHTGREEN; break;
case '3' : c=YELLOW; break;
case '4' : c=LIGHTRED; break;
default : c=LIGHTMAGENTA;
}
mouse_disable();
setcolor(c);
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(XST+XPX*x+XPX/2, YST+YPX*y+YPX/2+1, num);
mouse_enable();
}
else { /* Open near squares if the current number is 0 */
a=x-1;
b=y-1;
if (a>=0 && b>=0 && a<ncx && b<ncy && !sqopen(a,b) && sqmark(a,b)!=1) {
Set_square(a,b,0);
Open_square(a,b);
}
a++;
if (a>=0 && b>=0 && a<ncx && b<ncy && !sqopen(a,b) && sqmark(a,b)!=1) {
Set_square(a,b,0);
Open_square(a,b);
}
a++;
if (a>=0 && b>=0 && a<ncx && b<ncy && !sqopen(a,b) && sqmark(a,b)!=1) {
Set_square(a,b,0);
Open_square(a,b);
}
b++;
if (a>=0 && b>=0 && a<ncx && b<ncy && !sqopen(a,b) && sqmark(a,b)!=1) {
Set_square(a,b,0);
Open_square(a,b);
}
a-=2;
if (a>=0 && b>=0 && a<ncx && b<ncy && !sqopen(a,b) && sqmark(a,b)!=1) {
Set_square(a,b,0);
Open_square(a,b);
}
b++;
if (a>=0 && b>=0 && a<ncx && b<ncy && !sqopen(a,b) && sqmark(a,b)!=1) {
Set_square(a,b,0);
Open_square(a,b);
}
a++;
if (a>=0 && b>=0 && a<ncx && b<ncy && !sqopen(a,b) && sqmark(a,b)!=1) {
Set_square(a,b,0);
Open_square(a,b);
}
a++;
if (a>=0 && b>=0 && a<ncx && b<ncy && !sqopen(a,b) && sqmark(a,b)!=1) {
Set_square(a,b,0);
Open_square(a,b);
}
}
} /* Open_square */
/*----------------------------------------------------------------------*/
/*
Open the sorrounded squares. Returns != 0 if I activated a mine
*/
int Open_near_squares(int x, int y)
{
int a, b, suma=0;
a=x-1;
b=y-1;
if (a>=0 && b>=0 && a<ncx && b<ncy && sqmark(a,b)==1)
suma++;
a++;
if (a>=0 && b>=0 && a<ncx && b<ncy && sqmark(a,b)==1)
suma++;
a++;
if (a>=0 && b>=0 && a<ncx && b<ncy && sqmark(a,b)==1)
suma++;
b++;
if (a>=0 && b>=0 && a<ncx && b<ncy && sqmark(a,b)==1)
suma++;
a-=2;
if (a>=0 && b>=0 && a<ncx && b<ncy && sqmark(a,b)==1)
suma++;
b++;
if (a>=0 && b>=0 && a<ncx && b<ncy && sqmark(a,b)==1)
suma++;
a++;
if (a>=0 && b>=0 && a<ncx && b<ncy && sqmark(a,b)==1)
suma++;
a++;
if (a>=0 && b>=0 && a<ncx && b<ncy && sqmark(a,b)==1)
suma++;
if (suma == value(x,y)) {
suma=0;
a=x-1;
b=y-1;
if (a>=0 && b>=0 && a<ncx && b<ncy && !sqopen(a,b) && sqmark(a,b)!=1) {
suma|=(value(a,b)==MINE);
Set_square(a,b,0);
Open_square(a,b);
}
a++;
if (a>=0 && b>=0 && a<ncx && b<ncy && !sqopen(a,b) && sqmark(a,b)!=1) {
suma|=(value(a,b)==MINE);
Set_square(a,b,0);
Open_square(a,b);
}
a++;
if (a>=0 && b>=0 && a<ncx && b<ncy && !sqopen(a,b) && sqmark(a,b)!=1) {
suma|=(value(a,b)==MINE);
Set_square(a,b,0);
Open_square(a,b);
}
b++;
if (a>=0 && b>=0 && a<ncx && b<ncy && !sqopen(a,b) && sqmark(a,b)!=1) {
suma|=(value(a,b)==MINE);
Set_square(a,b,0);
Open_square(a,b);
}
a-=2;
if (a>=0 && b>=0 && a<ncx && b<ncy && !sqopen(a,b) && sqmark(a,b)!=1) {
suma|=(value(a,b)==MINE);
Set_square(a,b,0);
Open_square(a,b);
}
b++;
if (a>=0 && b>=0 && a<ncx && b<ncy && !sqopen(a,b) && sqmark(a,b)!=1) {
suma|=(value(a,b)==MINE);
Set_square(a,b,0);
Open_square(a,b);
}
a++;
if (a>=0 && b>=0 && a<ncx && b<ncy && !sqopen(a,b) && sqmark(a,b)!=1) {
suma|=(value(a,b)==MINE);
Set_square(a,b,0);
Open_square(a,b);
}
a++;
if (a>=0 && b>=0 && a<ncx && b<ncy && !sqopen(a,b) && sqmark(a,b)!=1) {
suma|=(value(a,b)==MINE);
Set_square(a,b,0);
Open_square(a,b);
}
return (suma);
}
else
return 0;
} /* Open_near_squares */ |