Minesweeper game code based on C language
- 2020-04-02 02:34:51
- OfStack
This article describes the minesweeper game code based on C language in detail, the code has more detailed comments, easy for readers to read and understand. Hope to learn the game development friends can have a little reference value.
The complete example code is as follows:
#include <graphics.h>
#include <math.h>
#include <stdio.h>
#include <dos.h>
#include <stdlib.h>
#include <conio.h>
#include <alloc.h>
union REGS regs;
int size=15;
int pix,piy=50;
char b[2]="1";
int pan[30][16];
int pan1[30][16];
int tt;
int Eflags;
int Msinit();
void Draw(int x,int y,int sizex,int sizey);
void Facedraw(int x,int y,int sizel,int k);
void Dead(int sizel,int x,int y);
void Setmouse(int xmax,int ymax,int x,int y);
int Msread(int *xp,int *yp,int *bup,struct time t1,int k);
void Draw1(int x,int y);
int Open(int x,int y);
float Random();
void Have(int sum,int x,int y,int xx,int yy);
void Help();
void Coread();
void Ddraw2(int x,int y);
main()
{
int mode=VGAHI,devices=VGA;
char ams;
int xms,yms,bms;
int i,j,k,k1=0;
int x=9,y=9,flags=0;
int sum=10;
int x1=0,y1=0;
int x11=0,y11=0;
int sizel=10;
int cflags=1;
struct time t1={0,0,0,0};
int co[3];
void far *Map;
char name[3][20];
FILE * p;
Msinit();
initgraph(&devices,&mode,"C:\tc");
if((Map=farmalloc(imagesize(0,0,20,20)))==NULL)
{
printf("Memory ererr!n");
printf("Press any key to out!n");
exit(1);
}
while((p = fopen("score.dat", "r")) == NULL)
{
if((p = fopen("score.dat", "w")) == NULL)
{
printf("The file cannot open!n");
printf("Presss any key to exit!n");
getch();
exit(1);
}
fprintf(p,"%d %d %d,%sn%sn%sn",999,999,999,"xiajia","xiajia","xiajia");
fclose(p);
}
fscanf(p,"%d %d %d,",&co[0],&co[1],&co[2]);
for(i=0;i<3;i++)
fscanf(p,"%sn",name[i]);
fclose(p);
Setmouse(1,1,615,460);
pix=250-x*size/2;
bss: b[0]='1';
flags=0;
k=0;
Eflags=0;
tt=0;
cleardevice();
setbkcolor(LIGHTCYAN);
for(i=0;i<30;i++)
for(j=0;j<16;j++)
{
pan[i][j]=0;
pan1[i][j]=0;
}
for(;;)
{
i=(int)(x*Random());
j=(int)(y*Random());
if(pan[i][j]==0)
{
pan[i][j]=9;
k=k+1;
}
if(k==sum) break;
}
for(i=0;i<x;i++)
for(j=0;j<y;j++)
{
if(pan[i][j]==0)
{
if(i-1>=0 && j-1>=0 && pan[i-1][j-1]==9) pan[i][j]++;
if(j-1>=0 && pan[i][j-1]==9) pan[i][j]++;
if(i+1<x && j-1>=0 && pan[i+1][j-1]==9) pan[i][j]++;
if(i-1>=0 && pan[i-1][j]==9) pan[i][j]++;
if(i+1<x && pan[i+1][j]==9) pan[i][j]++;
if(i-1>=0 && j+1<y && pan[i-1][j+1]==9) pan[i][j]++;
if(j+1<y && pan[i][j+1]==9) pan[i][j]++;
if(i+1<x && j+1<y && pan[i+1][j+1]==9) pan[i][j]++;
}
}
if(sum==10)
{
Draw(170,1,162,200);
setcolor(1);
outtextxy(175,5,"Game( )");
setcolor(4);
outtextxy(175,5," G");
}
else
if(sum==40)
{
Draw(115,1,272,307);
setcolor(1);
outtextxy(120,5,"Game(G)");
setcolor(4);
outtextxy(120,5," G");
}
else
if(sum==99)
{
Draw(40,1,470,307);
setcolor(1);
outtextxy(45,5,"Game(G)");
setcolor(4);
outtextxy(45,5," G");
}
setcolor(1);
outtextxy(100,350," : Help.");
setcolor(4);
outtextxy(100,350,"H");
setcolor(1);
outtextxy(200,350," / : exit.");
setcolor(4);
outtextxy(200,350,"Q");
outtextxy(220,350,"ESC");
setcolor(1);
outtextxy(330,350," : cores.");
setcolor(4);
outtextxy(330,350,"R");
Facedraw(250,30,sizel,2);
for(i=0;i<x;i++)
for(j=0;j<y;j++)
{
Draw(i*size+pix,j*size+piy,size,size);
}
Have(sum,pix+20,30,x,y);
k=0;
setfillstyle(1, 0);
bar(460-pix,25,490-pix,40);
setcolor(4);
outtextxy(465-pix,30,"000");
for(;;)
{
ams=Msread(&xms,&yms,&bms,t1,k);
if(flags!=0)
{
putimage(x11,y11,Map,COPY_PUT);
}
x11=xms;
y11=yms;
flags=1;
if((xms>=pix-10 && xms<=pix+26 && yms>=2 && yms<=14 && bms==1) || ams=='g' || ams=='G')
{
Draw(pix-10,14,100,88);
cflags=0;
setcolor(1);
outtextxy(pix-4,19,"Restar(R)");
setcolor(4);
outtextxy(pix-4,19," R");
setcolor(1);
outtextxy(pix-4,30,"Easy(E)");
setcolor(4);
outtextxy(pix-4,30," E");
setcolor(1);
outtextxy(pix-4,41,"Nomall(N)");
setcolor(4);
outtextxy(pix-4,41," N");
setcolor(1);
outtextxy(pix-4,52,"Hard(H)");
setcolor(4);
outtextxy(pix-4,52," H");
setcolor(1);
outtextxy(pix-4,63,"Corse(C)");
setcolor(4);
outtextxy(pix-4,63," C");
setcolor(1);
outtextxy(pix-4,74,"Help(L)");
setcolor(4);
outtextxy(pix-4,74," L");
setcolor(1);
outtextxy(pix-4,85,"Quit(Q)");
setcolor(4);
outtextxy(pix-4,85," Q");
bms=0;
}
if(Eflags==2 && (bms!=0 || ams!=-1))
{
goto bss;
}
if(cflags==0)
{
if((xms>=pix-6 && xms<=pix+85 && yms>=17 && yms<=28 && bms==1)|| (ams=='r' || ams=='R'))
{
cflags=1;
goto bss;
}
else
if((xms>=pix-6 && xms<=pix+85 && yms>=28 && yms<=39 && bms==1) || ams=='e' || ams=='E')
{
cflags=1;
x=9;
y=9;
sum=10;
pix=250-x*size/2;
goto bss;
}
else
if((xms>=pix-6 && xms<=pix+85 && yms>=39 && yms<=50 && bms==1) || ams=='n' || ams=='N')
{
cflags=1;
x=16;
y=16;
sum=40;
pix=250-x*size/2;
goto bss;
}
else
if((xms>=pix-6 && xms<=pix+85 && yms>=50 && yms<=61 && bms==1) || ams=='h' || ams=='H')
{
cflags=1;
x=30;
y=16;
sum=99;
pix=250-x*size/2+25;
goto bss;
}
else
if((xms>=pix-6 && xms<=pix+85 && yms>=61 && yms<=72 && bms==1) || ams=='c' || ams=='C')
{
cflags=1;
Coread();
}
else
if((xms>=pix-6 && xms<=pix+85 && yms>=72 && yms<=83 && bms==1) || ams=='l' || ams=='L')
{
cflags=1;
Help();
}
else
if((xms>=pix-6 && xms<=pix+85 && yms>=83 && yms<=94 && bms==1) || ams=='q' || ams=='Q' || ams==27)
{
cflags=1;
fclose(p);
closegraph();
farfree(Map);
exit(1);
}
else
if(bms!=0)
{
cflags=1;
goto bss;
}
}
if(cflags!=0 && xms >=250-sizel-2 && yms >= 30-sizel-2 && xms<=250+sizel+2 && yms<=30+sizel+2 && bms==1 )
{
goto bss;
}
if(Eflags==0 && cflags!=0 && xms-pix>=0 && yms-piy >=0 && xms<pix+x*size && yms<piy+y*size )
{
x1=(int)((xms-pix)/size);
y1=(int)((yms-piy)/size);
if(ams==-1 && bms==1 && pan1[x1][y1]==0)
{
if(k==0)
{
gettime(&t1);
k=1;
}
if(pan[x1][y1]!=9)
{
Draw1(x1,y1);
Open(x,y);
}
else
{
Dead(sizel,x,y);
k=0;
}
}
if(ams==-1 && bms==2 && pan1[x1][y1]==0)
{
delay(100);
setcolor(10);
circle(x1*size+pix+7,y1*size+piy+7,4);
pan1[x1][y1]=2;
Have(sum,pix+20,30,x,y);
}
else
if(ams==-1 && bms==2 && pan1[x1][y1]==2)
{
delay(100);
Draw(x1*size+pix,y1*size+piy,size,size);
pan1[x1][y1]=0;
Have(sum,pix+20,30,x,y);
}
if(ams==-1 && bms==3 && pan1[x1][y1]==1)
{
i=0;
if(x1-1>=0 && y1-1>=0 && pan1[x1-1][y1-1]==2) i++;
if(y1-1>=0 && pan1[x1][y1-1]==2)i++;
if(x1+1<x && y1-1>=0 && pan1[x1+1][y1-1]==2) i++;
if(x1-1>=0 && pan1[x1-1][y1]==2) i++;
if(x1+1<x && pan1[x1+1][y1]==2) i++;
if(x1-1>=0 && y1+1<y && pan1[x1-1][y1+1]==2) i++;
if(y1+1<y && pan1[x1][y1+1]==2) i++;
if(x1+1<x && y1+1<y && pan1[x1+1][y1+1]==2) i++;
if(i==pan[x1][y1])
{
if(x1-1>=0 && y1-1>=0 && pan1[x1-1][y1-1]!=2)
{
if(pan[x1-1][y1-1]!=9)
Draw1(x1-1,y1-1);
else
{
Dead(sizel,x,y);
k=0;
}
}
if(y1-1>=0 && pan1[x1][y1-1]!=2)
{
if(pan[x1][y1-1]!=9)
Draw1(x1,y1-1);
else
{
Dead(sizel,x,y);
k=0;
}
}
if(x1+1<x && y1-1>=0 && pan1[x1+1][y1-1]!=2)
{
if(pan[x1+1][y1-1]!=9)
Draw1(x1+1,y1-1);
else
{
Dead(sizel,x,y);
k=0;
}
}
if(x1-1>=0 && pan1[x1-1][y1]!=2)
{
if(pan[x1-1][y1]!=9)
Draw1(x1-1,y1);
else
{
Dead(sizel,x,y);
k=0;
}
}
if(x1+1<x && pan1[x1+1][y1]!=2)
{
if(pan[x1+1][y1]!=9)
Draw1(x1+1,y1);
else
{
Dead(sizel,x,y);
k=0;
}
}
if(x1-1>=0 && y1+1<y && pan1[x1-1][y1+1]!=2)
{
if(pan[x1-1][y1+1]!=9)
Draw1(x1-1,y1+1);
else
{
Dead(sizel,x,y);
k=0;
}
}
if(y1+1<y && pan1[x1][y1+1]!=2)
{
if(pan[x1][y1+1]!=9)
Draw1(x1,y1+1);
else
{
Dead(sizel,x,y);
k=0;
}
}
if(x1+1<x && y1+1<y &&pan1[x1+1][y1+1]!=2)
{
if(pan[x1+1][y1+1]!=9)
Draw1(x1+1,y1+1);
else
{
Dead(sizel,x,y);
k=0;
}
}
Open(x,y);
}
}
k1=0;
for(i=0;i<x;i++)
for(j=0;j<y;j++)
if(pan1[i][j]!=1) k1++;
if(k1==sum)
{
for(i=0;i<x;i++)
for(j=0;j<y;j++)
if(pan1[i][j]==0)
{
setcolor(10);circle(i*size+pix+7,j*size+piy+7,4);
}
Facedraw(250,30,sizel,3);
if(x==9 && tt<co[0]) {j=0;co[0]=tt;i=0;}
if(x==16 && tt<co[1]) {j=1;co[1]=tt;i=0;}
if(x==30 && tt<co[2]) {j=2;co[2]=tt;i=0;}
if(i==0)
{
Draw(10,10,300,55);
setcolor(4);
outtextxy(20,15,"You break the corse!");
outtextxy(20,25,"Please input your name(0 - 19):");
setfillstyle(1, 14);
bar(20,35,200,50);
for(i=0;i<19;i++)
{
name[j][i]=getch();
if(name[j][i]==8) i=i-2;
if(name[j][i]==13) {name[j][i]='0';break;}
name[j][i+1]='0';
setfillstyle(1, 14);
bar(20,35,200,50);
setcolor(1);
outtextxy(20,40,name[j]);
}
if((p = fopen("score.dat", "w")) == NULL)
{
printf("The file cannot open!n");
exit(1);
}
fprintf(p,"%d %d %d,",co[0],co[1],co[2]);
for(i=0;i<3;i++)
fprintf(p,"%sn",name[i]);
fclose(p);
Coread();
}
getch();
goto bss;
}
}
if(ams=='q'||ams=='Q'|| ams==27 ) {fclose(p);closegraph();farfree(Map);exit(1);}
if(ams=='c'||ams=='C') goto bss;
if(ams=='h'||ams=='H') {Help();k=0;}
if(ams=='r'||ams=='R') {Coread();k=0;}
getimage(xms,yms,xms+20,yms+20,Map);
Ddraw2(xms,yms);
delay(10);
}
}
int Msinit()
{
int recored;
regs.x.ax=0;
int86 (0x33, & regs, & regs);
recored=regs.x.ax;
if(recored==0)
{
printf("Mouse not found or Mouse driver not installed.n");
return 0;
}
else
return recored;
}
void Draw(int x,int y,int sizex,int sizey)
{
int sizx=sizex-1;
int sizy=sizey-1;
setcolor(15);
line(x,y,x+sizx-1,y);
line(x,y+1,x+sizx-2,y+1);
line(x,y,x,y+sizy-1);
line(x+1,y,x+1,y+sizy-2);
setcolor(8);
line(x+1,y+sizy,x+sizx,y+sizy);
line(x+2,y+sizy-1,x+sizx,y+sizy-1);
line(x+sizx-1,y+1,x+sizx-1,y+sizy);
line(x+sizx,y+2,x+sizx,y+sizy);
setcolor(7);
putpixel(x,y+sizy,3);
putpixel(x+1,y+sizy-1,3);
putpixel(x+sizx,y,3);
putpixel(x+sizx-1,y+1,3);
setfillstyle(1, 7);
bar(x+2,y+2,x+sizx-2,y+sizy-2);
}
void Facedraw(int x,int y,int sizel,int k)
{Draw(x-sizel-3,y-sizel-3,2*(sizel+4),2*(sizel+4));
setcolor(14);
setfillstyle(1, 14);
pieslice(x, y, 0, 360, sizel);
setfillstyle(1, 0);
setcolor(0);
if(k==1)
{
line(x-sizel*7/20,y-sizel*7/20,x-sizel*3/20,y-sizel*3/20);
line(x-sizel*7/20,y-sizel*3/20,x-sizel*3/20,y-sizel*7/20);
line(x+sizel*3/20,y-sizel*7/20,x+sizel*7/20,y-sizel*3/20);
line(x+sizel*3/20,y-sizel*3/20,x+sizel*7/20,y-sizel*7/20);
arc(x,y+sizel,20,160, sizel/2);
}
if(k==2)
{
bar(x-sizel*7/20,y-sizel*7/20,x-sizel*3/20,y-sizel*3/20);
bar(x+sizel*3/20,y-sizel*7/20,x+sizel*7/20,y-sizel*3/20);
arc(x,y,200,340, sizel*3/4);
}
if(k==3)
{
bar(x-sizel/2,y-sizel/2,x-sizel*3/20,y-sizel*3/20);
bar(x+sizel*3/20,y-sizel/2,x+sizel/2,y-sizel*3/20);
line(x-sizel*3/20,y-sizel/2,x+sizel*3/20,y-sizel/2);
line(x-sizel/2,y-sizel*5/20,x-sizel,y);
line(x+sizel/2,y-sizel*5/20,x+sizel,y);
arc(x,y,200,340, sizel*3/4);
}
}
void Dead(int sizel,int x,int y)
{
int i,j;
setcolor(4);
for(i=0;i<x;i++)
for(j=0;j<y;j++)
if(pan[i][j]==9)
{
circle(i*size+pix+7,j*size+piy+7,4);
}
Facedraw(250,30,sizel,1);
Eflags=1;
}
void Setmouse(int xmax,int ymax,int x,int y)
{
int left,right,top,buttom;
left=xmax;
right=x;
top=ymax;
buttom=y;
regs.x.ax=7;
regs.x.cx=left;
regs.x.dx=right;
int86(0x33,& regs,& regs);
regs.x.ax=8;
regs.x.cx=top;
regs.x.dx=buttom;
int86(0x33,& regs,& regs);
}
int Msread(int *xp,int *yp,int *bup,struct time t1,int k)
{
int x0=*xp,y0=*yp,bu0=*bup;
int xnew,ynew,ch;
int tt1=0;
struct time t2;
char ttt[4]="000";
do
{
if(kbhit()) return getch();
regs.x.ax=3;
int86(0x33,& regs,& regs);
xnew=regs.x.cx;
ynew=regs.x.dx;
*bup=regs.x.bx;
if(k==1)
{
gettime(&t2);
tt1=(t2.ti_hour- t1.ti_hour)*3600 + (t2.ti_min- t1.ti_min)*60 + t2.ti_sec - t1.ti_sec;
if(tt1<=999 && tt!=tt1)
{
tt=tt1;
ttt[0]=(tt-tt%100)/100+48;
ttt[1]=(tt%100-tt%10)/10 +48;
ttt[2]=tt%10+48;
setfillstyle(1, 0);
bar(460-pix,25,490-pix,40);
setcolor(4);
outtextxy(465-pix,30,ttt);
}
}
}
while(xnew==x0&&ynew==y0&&*bup==bu0);
*xp=xnew;
*yp=ynew;
return -1;
}
void Draw1(int x,int y)
{
setfillstyle(1, 7);
bar(x*size+pix,y*size+piy,x*size+size+pix-1,y*size+size+piy-1);
setcolor(8);
setlinestyle(1, 0, 1);
rectangle(x*size+pix,y*size+piy,x*size+size+pix-1,y*size+size+piy-1);
setlinestyle(0, 0, 1);
if(pan[x][y]!=0)
{
b[0]=pan[x][y]+48;
if(pan[x][y]!=7)
setcolor(pan[x][y]);
else setcolor(9);
outtextxy (x*size+pix+3,y*size+piy+3,b);
}
pan1[x][y]=1;
}
int Open(int x,int y)
{
int i,j,k;
int pan2[30][16]={0};
for(;;)
{
k=0;
for(i=0;i<x;i++)
for(j=0;j<y;j++)
{
if (pan[i][j]==0 && pan1[i][j]==1 && pan2[i][j]==0)
{
pan2[i][j]=1;
k=k+1;
if(i-1>=0 && j-1>=0 && pan1[i-1][j-1]==0) Draw1(i-1,j-1);
if(j-1>=0 && pan1[i][j-1]==0) Draw1(i,j-1);
if(i+1<x && j-1>=0 && pan1[i+1][j-1]==0) Draw1(i+1,j-1);
if(i-1>=0 && pan1[i-1][j]==0) Draw1(i-1,j);
if(i+1<x && pan1[i+1][j]==0) Draw1(i+1,j);
if(i-1>=0 && j+1<y && pan1[i-1][j+1]==0) Draw1(i-1,j+1);
if(j+1<y && pan1[i][j+1]==0) Draw1(i,j+1);
if(i+1<x && j+1<y && pan1[i+1][j+1]==0) Draw1(i+1,j+1);
}
}
if(k==0) return 0;
}
}
float Random()
{float aa;
int i,j;
struct time t;
gettime(&t);
i=t.ti_min+t.ti_hour+t.ti_sec+t.ti_hund;
for(j=0;j<i;j++)
rand();
aa=rand()/32767.0;
return aa;
}
void Have(int sum,int x,int y,int xx,int yy)
{
int i,j,k=0;
char a[3]="1";
for(i=0;i<xx;i++)
for(j=0;j<yy;j++)
{if (pan1[i][j]==2) k++;}
k=sum-k;
if(k<0) k=0;
a[0]=(k-(k%10))/10+48;
a[1]=k%10+48;
setfillstyle(1, 0);
bar(x-5,y-5,x+20,y+10);
setcolor(4);
outtextxy(x,y,a);
}
void Help()
{
Draw(40,1,560,160);
setcolor(1);
outtextxy(300,10,"Help");
setcolor(4);
outtextxy(50,30,"Welcome to play this game. The mouse is supplied.");
outtextxy(50,40,"Please press left button of the mouse where there is no boom.");
outtextxy(50,50,"If you think there is a boom please press right button.");
outtextxy(50,60,"You can choose the size of the game with press the right buttons.");
outtextxy(50,70,"'Big' means 99 booms,'Normal' means 40 booms,'Small' means 10 booms.");
outtextxy(50,80,"You can press the laugh-face-button or press C to play again.");
outtextxy(50,90,"When you fail or success you can press any key to play again. ");
outtextxy(50,100,"Whenever you want to quit this game please press Q. ");
outtextxy(50,110,"If you find out all booms then you are winning. ");
setcolor(9);
outtextxy(50,140,"Press any key to play again.");
Eflags=2;
}
void Coread()
{
int co[3],i;
char name[3][20];
char cco[3][4];
FILE * p;
for(i=0;i<3;i++)
cco[i][3]='0';
if((p = fopen("score.dat", "rb")) == NULL)
{
printf("The file cannot open!n");
exit(1);
}
fscanf(p,"%d %d %d,",&co[0],&co[1],&co[2]);
for(i=0;i<3;i++)
fscanf(p,"%sn",name[i]);
fclose(p);
Draw(10,20,320,90);
setcolor(1);
outtextxy(140,30,"HighCore");
setcolor(2);
outtextxy(20,50,"Easy");
outtextxy(20,60,"Normal");
outtextxy(20,70,"Hard");
for(i=0;i<3;i++)
{
cco[i][0]=(co[i]-co[i]%100)/100+48;
cco[i][1]=(co[i]%100-co[i]%10)/10 +48;
cco[i][2]=co[i]%10+48;
}
setcolor(3);
outtextxy(100,50,cco[0]);
outtextxy(100,60,cco[1]);
outtextxy(100,70,cco[2]);
setcolor(5);
outtextxy(150,50,name[0]);
outtextxy(150,60,name[1]);
outtextxy(150,70,name[2]);
setcolor(9);
outtextxy(30,85,"Press any key to play again.");
Eflags=2;
}
void Ddraw2(int x,int y)
{
setcolor(1);
line(x,y,x,y+18);
line(x+1,y,x+1,y+18);
line(x,y,x+12,y+12);
line(x+1,y,x+13,y+12);
line(x,y+18,x+4,y+14);
line(x,y+17,x+4,y+13);
line(x+4,y+14,x+6,y+20);
line(x+4,y+13,x+6,y+19);
line(x+6,y+20,x+10,y+18);
line(x+6,y+19,x+10,y+17);
line(x+10,y+18,x+8,y+12);
line(x+10,y+17,x+8,y+11);
line(x+8,y+12,x+12,y+12);
line(x+8,y+11,x+12,y+11);
}
At this point the program is complete, direct compilation can run!