# include <dos.h> # include <time.h> # include <stdio.h> # include <stdlib.h>
# define BYTE unsigned char # define BOOL BYTE # define WORD unsigned int # define DWORD unsigned long
# define TRUE 1 # define FALSE !TRUE
# define MOUSE 0x33 # define LBUTTON 1 # define RBUTTON 2 # define SX 35 # define SY 7
# define MINE 9
BOOL InitMouse(); BYTE Scr(int,int); BYTE MouseState(); void Edge(int,int); void TurnBack(int,int); void InitArrow(); void InitCursor(); void MouseShow(); void MouseHide(); void CursorShow(); void CursorHide(); void SetRange(int,int,int,int); void GetXY(int*,int*); void SetXY(int,int); void WaitMouse(); void WaitKey(); void Prt(int,int,BYTE,BYTE); void Locate(BYTE,BYTE); void FailExitGame(); void ExitGame();
BOOL Turn[10][10]; BYTE Mine[10][10]; BYTE Mark[10][2]; WORD CursorMode; int x,y;
void main() { int i; int Total=0; BYTE Button; BOOL QUIT=FALSE;
if (!InitMouse()) { printf("Mouse not installed!"); exit(0); } clrscr(); CursorHide();
/* InitArrow(); InitCursor(); */
randomize(); Locate(36,20); printf("Mines:%d ",0);
for (y=0;y<10;y++) for (x=0;x<10;x++) { Mine[y][x]=NULL; Turn[y][x]=FALSE; }
for (i=0;i<10;i++) { x=random(10); y=random(10); while (Mine[y][x]) { x=random(10); y=random(10); } Mine[y][x]=MINE; }
for (y=0;y<10;y++) for (x=0;x<10;x++) { i=0; if (Mine[y][x]!=MINE) { if ((Mine[y][x-1]==MINE)&&((x-1)>=0)) i++; if ((Mine[y][x+1]==MINE)&&((x+1)<10)) i++; if ((Mine[y-1][x]==MINE)&&((y-1)>=0)) i++; if ((Mine[y+1][x]==MINE)&&((y+1)<10)) i++; if ((Mine[y-1][x-1]==MINE)&&((x-1)>=0)&&((y-1)>=0)) i++; if ((Mine[y-1][x+1]==MINE)&&((x+1)<10)&&((y-1)>=0)) i++; if ((Mine[y+1][x+1]==MINE)&&((x+1)<10)&&((y+1)<10)) i++; if ((Mine[y+1][x-1]==MINE)&&((x-1)>=0)&&((y+1)<10)) i++; Mine[y][x]=i; } Prt(x+SX,y+SY,'?',9) ; }
MouseShow(); while(!QUIT) { if (kbhit()) if (getch()==27) QUIT=TRUE; Button=MouseState()&3; if (Button) { GetXY(&x,&y); WaitMouse(); x>>=3; y>>=3; x-=SX; y-=SY; if (x>=0&&x<10&&y>=0&&y<10) { if (Button&LBUTTON) { if (Mine[y][x]==MINE) FailExitGame(); if (Mine[y][x]==NULL) Edge(x,y); else TurnBack(x,y); } if (Button&RBUTTON) { if (!(Turn[y][x]&1)) { switch (Turn[y][x]) { case 0: Turn[y][x]=2; MouseHide(); Prt(x+SX,y+SY,'*',15); Locate(36,20); printf("Mines:%d ",++Total); MouseShow(); Mark[Total][0]=x; Mark[Total][1]=y; if (Total==10) ExitGame(); break; case 2: Turn[y][x]=0; MouseHide(); Prt(x+SX,y+SY,'?',9) ; Locate(36,20); printf("Mines:%d ",--Total); MouseShow(); break; } } } } } } WaitKey(); MouseHide(); CursorShow(); }
BYTE MouseState() { union REGS regs; regs.x.ax = 3; int86(MOUSE, ®s, ®s); return(regs.x.bx); }
void MouseShow() { union REGS regs; regs.x.ax = 1; int86(MOUSE, ®s, ®s); }
void MouseHide() { union REGS regs; regs.x.ax = 2; int86(MOUSE, ®s, ®s); }
void InitArrow() { /* BYTE shape[16]={0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xf0,0xd8,0x98,0x0c,0x0c,0x06,0x07,0x03}; D(_DS,shape,3,1); */ }
void InitCursor() { union REGS regs; regs.x.ax = 10; regs.x.bx = 0; regs.x.cx = 0; regs.x.dx = (7 * 16 + 15) * 256 +3; int86(MOUSE, ®s, ®s); }
BOOL InitMouse() { union REGS regs; regs.x.ax = 0; int86(MOUSE, ®s, ®s); return (regs.x.ax); } void SetRange(int start_x,int start_y,int end_x,int end_y) { union REGS r; r.x.ax=7; r.x.cx=start_x; r.x.dx=end_x; int86(0x33,&r,&r); r.x.ax=8; r.x.cx=start_y; r.x.dx=end_y; int86(MOUSE,&r,&r); } void GetXY(int *x,int *y) { union REGS ireg,oreg; ireg.x.ax=3; int86(MOUSE,&ireg,&oreg); *x=oreg.x.cx; *y=oreg.x.dx; } void SetXY(int x,int y) { union REGS ireg; ireg.x.ax=4; ireg.x.cx=x; ireg.x.dx=y; int86(MOUSE,&ireg,&ireg); }
/* Waiting for releasing the mouse button */ void WaitMouse() { int xx,yy; while (MouseState()) { GetXY(&xx,&yy); if (xx!=x||yy!=y) SetXY(x,y); } }
void Prt(int x,int y,BYTE ch,BYTE color) { pokeb(0xb800,y*160+x*2,ch); pokeb(0xb800,y*160+x*2+1,color); }
BYTE Scr(int x,int y) { return peekb(0xb800,y*160+x*2); }
void CursorHide() { _AH=3; geninterrupt(0x10); CursorMode=_CX; _AH=1; _CH=32; geninterrupt(0x10); } void CursorShow() { _AH=1; _CX=CursorMode; geninterrupt(0x10); }
void WaitKey() { _AH=0; geninterrupt(0x16); }
void Edge(int xx,int yy) { if (xx<0||xx>9||yy<0||yy>9||Turn[yy][xx]) return; TurnBack(xx,yy); if (Mine[yy][xx]==NULL) { Edge(xx+1,yy); Edge(xx-1,yy); Edge(xx,yy+1); Edge(xx,yy-1); } }
void TurnBack(int xx,int yy) { if (!Turn[yy][xx]) { MouseHide(); Prt(xx+SX,yy+SY,Mine[yy][xx]+48,12); MouseShow(); Turn[yy][xx]=Turn[yy][xx]||1; } }
void Locate(BYTE Col,BYTE Line) { pokeb(0x0,0x450,Col); pokeb(0x0,0x451,Line); }
void FailExitGame() { sound(1000); delay(500); nosound(); for (y=0;y<10;y++) for (x=0;x<10;x++) if (Mine[y][x]==MINE) Prt(x+SX,y+SY,'*',14+128); MouseHide(); CursorShow(); printf("\nFail!\n"); exit(0); }
void ExitGame() { int i,m=0; for (i=0;i<10;i++) if (Mine[Mark[i][1]][Mark[i][0]]) m++; if (m<9) FailExitGame(); MouseHide(); CursorShow(); printf("\nOK,Good!\n"); exit(0); }