#include #include #include #include #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 10 /* Default number of squares */ #define DEFCY 10 #define MINE 255 #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)*noy+(y))->value #define sqopen(x,y) (psquare+(x)*noy+(y))->sqopen #define sqpress(x,y) (psquare+(x)*noy+(y))->sqpress #define sqmark(x,y) (psquare+(x)*noy+(y))->sqmark int XST, /* Offset of first pixel X */ YST, nox, /* Number of squares in X */ noy, dmines, /* Mines discovered */ initmines, /* Number of initial mines */ sqclosed, /* Squares still closed */ maxy; /* Max number of y pixels of the screen */ void Graph_init(); void Init_param(); void Set_mines(int ); void Set_square(int, int, int); void Mouse_set(); void Draw_squares(); int Do_all(); void Blow_up(); void Open_square(int ,int ); /************************************************************************/ void main() { Graph_init(); Init_param(); Mouse_set(); do { randomize(); cleardevice(); Set_mines(dmines=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; initgraph(&graphdriver, &graphmode, "c:\\tc\\bgi"); maxy=getmaxy(); } /*----------------------------------------------------------------------*/ void Init_param() { XST=100; nox=DEFCX; YST=30; noy=DEFCY; initmines= nox*noy*4/25; (void*)psquare=calloc(nox*noy, sizeof(STSQUARE)); } /*----------------------------------------------------------------------*/ void Set_mines(int nminas) { STSQUARE *p; int i, x, y, a, b; sqclosed=nox*noy-nminas; p=psquare; for (i=nox*noy; i>0; i--, p++) p->value = p->sqopen = p->sqpress = p->sqmark = NO; for (i=nminas; i>0; i--) { x=random(nox); y=random(noy); value(x,y)=MINE; } for (x=nox-1; x>=0; x--) { for (y=noy-1; y>=0; y--) { if (value(x,y) == MINE) continue; a=x-1; b=y-1; if (a>=0 && b>=0 && a=0 && b>=0 && a=0 && b>=0 && a=0 && b>=0 && a=0 && b>=0 && a=0 && b>=0 && a=0 && b>=0 && a=0 && b>=0 && a=0; x--) for (y=noy-1; y>=0; y--) if (value(x,y)==MINE) { sqmark(x,y)=1; Set_square(x,y,1); } break; } if (cminasant != dmines) { /* Update Mines: */ cminasant=dmines; settextjustify(LEFT_TEXT, CENTER_TEXT); setcolor(BLACK); outtextxy(0, maxy/2-10, blk); sprintf(str,"Mines: %4d",dmines); 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(); 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=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 : dmines--; break; case 2 : dmines++; } 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); } } mouse_disable(); return (getch()); } /*----------------------------------------------------------------------*/ void Blow_up(void) { int x, y, xl, yt, xr, yb; mouse_disable(); for (x=0; x=0 && b>=0 && a=0 && b>=0 && a=0 && b>=0 && a=0 && b>=0 && a=0 && b>=0 && a=0 && b>=0 && a=0 && b>=0 && a=0 && b>=0 && a