easyX库下的扫雷代码

作者在 2014-07-24 18:43:24 发布以下内容
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<graphics.h>


#define TRUE 1;
#define FALSE 0;
#define PI 3.1415926
int m_maxx,m_maxy,m_wh,m_row,m_col,x0,y0,minenum;
int countMine,countTrueMine;
int **p,***flag;
clock_t start,finish;




void draw_box(int x1,int y1,int x2,int y2,int type);
int draw_area();
void draw_back_rectangle();
int count_x0_y0();
int set_mine();
void draw_mine(int i,int j);
void RoundNum();
void found_mine();
void clickl(int i,int j);
void game_over(int i,int j);
void win();
void char_int(int number,char *str);
void out_num(int i,int j);
void out_mine_num();
void fun(int i,int j);
void draw_flag(int i,int j);
void draw_face(int n);
void left_down(int x,int y);
void left_up(int x,int y);
void right_down(int x,int y);
void middle_up(int x,int y);
void middle_down();
void again_game();
void windows();
void read();
void write();
void define();
int init_system();
void close_system();


void main()
{
windows();
if (!init_system())
{
outtextxy(200,100,"初始化失败!");
exit(0);
}
cleardevice();
draw_area();
finish=clock();
found_mine();
}


void draw_box(int x1,int y1,int x2,int y2,int type)
{
setcolor(WHITE);
rectangle(x1,y1,x2,y2); 
setfillcolor(LIGHTGRAY);
floodfill((x1+x2)/2,(y1+y2)/2,WHITE);
if (type==1)
{
setcolor(WHITE);
line(x1+1,y1+1,x1+1,y2-1);
line(x1+1,y1+1,x2-1,y1+1);
setcolor(DARKGRAY);
line(x1,y2,x2,y2);
line(x1,y2,x2,y2);
line(x2-1,y1+1,x2-1,y2-1);
line(x1+1,y2-1,x2-1,y2-1);
}
else
{
setcolor(DARKGRAY);
line(x1,y1,x1,y2);
line(x1,y1,x2,y1);
line(x1+1,y1+1,x1+1,y2-1);
line(x1+1,y1+1,x2-1,y1+1);
setcolor(WHITE);
line(x2-1,y1+1,x2-1,y2-1);
line(x1+1,y2-1,x2-1,y2-1);
}
}


int draw_area()
{
int i,j,x,y;
if (!count_x0_y0())
return FALSE;
if (set_mine())
{
draw_back_rectangle();
draw_face(1);
y=y0;
for (i=0;i<m_row;i++)
{
x=x0;
for (j=0;j<m_col;j++)
{

draw_box(x,y,x+m_wh,y+m_wh,1);
x=x+m_wh+1;
}
y=y+m_wh+1;
}Sleep(50);
return TRUE;
}
else
{
outtextxy(0,0,"雷数太多");
getch();
exit(0);
}
}


int count_x0_y0()
{
if(m_maxx<=m_col*(m_wh+1)+m_wh/2 ||m_maxy<=m_row*(m_wh+1)+m_wh/2 )
{
outtextxy(0,0,"图形太大!");
getch();
exit(0);
}
x0=(m_maxx-m_col*(m_wh+1)-1)/2;
y0=(m_maxy-m_row*(m_wh+1)-1)/2;
return TRUE;
}


void draw_face(int n)
{
int x,y;
x=x0+m_col*(m_wh+1)/2;
y=y0-1.5*m_wh;
draw_box(x-m_wh,y-m_wh,x+m_wh,y+m_wh,1);
setcolor(YELLOW);
circle(x,y,m_wh*3/4);
setfillcolor(YELLOW);
fillcircle(x,y,m_wh*3/4);
if (n==1||n==2)
{
setcolor(BLACK);
circle(x-m_wh/4,y-m_wh/4,m_wh/8);
setfillcolor(BLACK);
fillcircle(x-m_wh/4,y-m_wh/4,m_wh/8);
fillcircle(x+m_wh/4,y-m_wh/4,m_wh/8);
if  (n==1)
arc(x+m_wh/3,y,x-m_wh/3,y+m_wh/2,PI,0);
else 
{
setfillcolor(BLACK);
ellipse(x-m_wh/8,y-m_wh/10,x+m_wh/8,y+m_wh/3);
fillellipse(x-m_wh/8,y-m_wh/10,x+m_wh/8,y+m_wh/3);
}
}
if (n==3)
{
setcolor(BLACK);
line(x-m_wh/2,y-m_wh*3/16,x-m_wh/6,y-m_wh/2);
line(x+m_wh/2,y-m_wh*3/16,x+m_wh/6,y-m_wh/2);
line(x-m_wh*3/8,y+m_wh*2/5,x,y);
line(x+m_wh*3/8,y+m_wh*2/5,x,y);


}
if (n==4)
{
setcolor(BLACK);
circle(x-m_wh*3/8,y-m_wh*3/8,m_wh/4);
circle(x+m_wh*3/8,y-m_wh*3/8,m_wh/4);
line(x-m_wh*3/8,y-m_wh*3/8,x+m_wh*3/8,y-m_wh*3/8);
setfillcolor(BLACK);
fillcircle(x-m_wh*3/8,y-m_wh*3/8,m_wh/4);
fillcircle(x+m_wh*3/8,y-m_wh*3/8,m_wh/4);
arc(x-m_wh/2,y,x+m_wh/2,y+m_wh/2,4.14,0);
}


}
void draw_back_rectangle()
{
int x1,y1,x2,y2;
x1=x0-m_wh/2;
y1=y0-m_wh*3;
x2=x0+m_col*(m_wh+1)+m_wh/2;
y2=y0+m_row*(m_wh+1)+m_wh/2;
if (y0-m_wh*3 < 0)
{
outtext("图形太大");
getch();
exit(1);
}
setcolor(WHITE);
rectangle(x1,y1,x2,y2);
setfillcolor(LIGHTGRAY);
fillrectangle(x1,y1,x2,y2);
draw_box(x0,y0-m_wh*2,x0+3*m_wh,y0-0.5*m_wh,2);
draw_box(x2-3.5*m_wh,y0-2*m_wh,x2-0.5*m_wh,y0-0.5*m_wh,2);
out_mine_num();
}
void draw_mine(int i,int j)
{
int x, y;
x=j*(m_wh+1)+x0;
y=i*(m_wh+1)+y0;
draw_box(x,y,x+m_wh,y+m_wh,2);
setcolor(BLACK);
circle(x+m_wh/2,y+m_wh/2,m_wh/3);
setfillcolor(BLACK);
fillcircle(x+m_wh/2,y+m_wh/2,m_wh/3);
}


void draw_flag(int i,int j)
{
int x,y;
x=j*(m_wh+1)+x0;
y=i*(m_wh+1)+y0;
setcolor(RED);
setfillcolor(RED);
line(x+m_wh/4,y+m_wh/4,x+m_wh/4,y+m_wh-1);
line(x+m_wh/4,y+m_wh/4,x+m_wh/4*3,y+m_wh/3*2);
line(x+m_wh/4*3,y+m_wh/3*2,x+m_wh/4,y+m_wh/3*2);
floodfill(x+m_wh/4+1,y+m_wh/3*2-1,RED);
}
int set_mine()
{
int i,j,count=0;
if (minenum > m_row*m_col)
return FALSE;
srand(time(NULL));
while (count < minenum)
{
i=rand()%m_row;
j=rand()%m_col;
if (p[i][j]!=-1)
{
p[i][j]=-1;
count++;
}
}
RoundNum();
return TRUE;
}


void RoundNum()
{
int count,i,j;
for (i=0;i<m_row;i++)
{
for (j=0;j<m_col;j++)
{
count=0;
if (p[i][j]==-1)
continue;
else
{
if( j+1<m_col)
if (p[i][j+1]==-1 )
count++;
if(i+1<m_row)
if (p[i+1][j]==-1)
count++;
if( j-1>=0)
if (p[i][j-1]==-1 )
count++;
if ( i-1>=0)
if(p[i-1][j]==-1)
count++;
if( i+1<m_row && j+1<m_col)
if (p[i+1][j+1]==-1 )
count++;
if(i-1>=0 && j-1>=0)
if (p[i-1][j-1]==-1)
count++;
if( i+1<m_row && j-1>=0)
if (p[i+1][j-1]==-1 )
count++;
if( i-1>=0 && j+1 <m_col)
if (p[i-1][j+1]==-1 )
count++;
p[i][j]=count;
}
}
}
}
void game_over(int i,int j)
{
int ii,jj,x,y;
setcolor(WHITE);
outtextxy(0,0,"游戏结束");
draw_face(3);
x=j*(m_wh+1)+x0;
y=i*(m_wh+1)+y0;
draw_box(x,y,x+m_wh,y+m_wh,2);
setcolor(BLACK);
circle(x+m_wh/2,y+m_wh/2,m_wh/3);
setfillcolor(RED);
fillcircle(x+m_wh/2,y+m_wh/2,m_wh/3);
Sleep(100);
for (ii=0;ii<m_row;ii++)
for (jj=0;jj<m_col;jj++)
{
if (!flag[2][ii][jj] && !flag[1][ii][jj] && p[ii][jj]==-1&& (i != ii || j != jj))
{
Sleep(10);
draw_mine(ii,jj);
}
}
again_game();
}


void win()
{
int i,j;
if (countMine==minenum && countTrueMine==minenum)
{
draw_face(4);
for (i=0;i<m_row;i++)
for (j=0;j<m_col;j++)
{
if (p[i][j]!=-1 && !flag[1][i][j])
out_num(i,j);
}
setcolor(WHITE);
settextstyle(16,0,_T("宋体"));
outtextxy(0,0,"你胜利了!");
Sleep(1000);
closegraph();
write();
again_game();
}
}


void fun(int i,int j)
{
if (p[i][j]!=0)
{
out_num(i,j);
flag[1][i][j]=1;
}
else
{
out_num(i,j);
flag[1][i][j]=1;
if (i-1 >=0 && flag[1][i-1][j]==0)
fun(i-1,j);
if (j-1 >=0 && flag[1][i][j-1]==0)
fun(i,j-1);
if (i+1 < m_row && flag[1][i+1][j]==0)
fun(i+1,j);
if (j+1 < m_col && flag[1][i][j+1]==0)
fun(i,j+1);
if (i-1 >=0 && j-1>=0 && flag[1][i-1][j-1]==0)
fun(i-1,j-1);
if (i+1<m_row && j-1 >=0 && flag[1][i+1][j-1]==0)
fun(i+1,j-1);
if (i-1>=0 && j+1 <m_col && flag[1][i-1][j+1]==0)
fun(i-1,j+1);
if (i+1<m_row && j+1<m_col && flag[1][i+1][j+1]==0)
fun(i+1,j+1);
}
}


void clickl(int i,int j)
{
if (p[i][j]==-1)
{
game_over(i,j);
}
else if (p[i][j]==0)
{
fun(i,j);
}
else
{
out_num(i,j);
flag[1][i][j]=1;
}
}


void out_num(int i,int j)
{
int x,y;
char str[2]="\0";
x=j*(m_wh+1)+x0;
y=i*(m_wh+1)+y0;
settextstyle(m_wh*0.8,0,_T("宋体"));
switch (p[i][j])
{
case 0:
draw_box(x,y,x+m_wh,y+m_wh,2);
return;
case 1:
draw_box(x,y,x+m_wh,y+m_wh,2);
setcolor(GREEN);
break;
case 2:
draw_box(x,y,x+m_wh,y+m_wh,2);
setcolor(BLUE);
break;
case 3:
draw_box(x,y,x+m_wh,y+m_wh,2);
setcolor(RED);
break;
case 4:
draw_box(x,y,x+m_wh,y+m_wh,2);
setcolor(YELLOW);
break;
case 5:
setcolor(BROWN);
draw_box(x,y,x+m_wh,y+m_wh,2);
break;
case 6:
setcolor(MAGENTA);
draw_box(x,y,x+m_wh,y+m_wh,2);
break;
case 7:
setcolor(LIGHTRED);
draw_box(x,y,x+m_wh,y+m_wh,2);
break;
case 8:
setcolor(LIGHTMAGENTA);
draw_box(x,y,x+m_wh,y+m_wh,2);
break;
}
str[0]=p[i][j]+'0';
outtextxy(x+m_wh/3,y+m_wh/6,str);
}


void char_int(int number,char *str)
{
int n,m,k;
if (number < 0)
{
char_int(-number,str);
str[4]=str[3];
str[3]=str[2];
str[2]=str[1];
str[1]=str[0];
str[0]='-';
}
else if (number ==0)
{
str[0]='0';
str[1]='\0';
}
else if (number < 10)
{
str[0]='0';
str[1]=number+'0';
str[2]='\0';
}
else if (number < 100)
{
n=number/10;
m=number%10;
str[0]='0';
str[1]=n+'0';
str[2]=m+'0';
str[3]='\0';
}
else if(number < 1000)
{
n=number/100;
m=number/10%10;
k=number%10;
str[0]=n+'0';
str[1]=m+'0';
str[2]=k+'0';
str[3]='\0';
}
}


void out_mine_num()
{
int x,y;
char  str[5];
x=x0;
y=y0-m_wh*2;
char_int(minenum-countMine,str);
settextstyle(16,0,_T("宋体"));
setcolor(RED);
outtextxy(x+0.75*m_wh,y0-1.75*m_wh,str);
}


void left_down(int x,int y)
{
int x1,y1;
draw_face(2);
y1=(x-x0)/(m_wh+1);
x1=(y-y0)/(m_wh+1);
if (flag[2][x1][y1]==1)
return ;
if (!flag[1][x1][y1])
draw_box(y1*(m_wh+1)+x0,x1*(m_wh+1)+y0,y1*(m_wh+1)+x0+m_wh,x1*(m_wh+1)+y0+m_wh,2);
}
void left_up(int x,int y)
{
int x1,y1;
draw_face(1);
y1=(x-x0)/(m_wh+1);
x1=(y-y0)/(m_wh+1);
if (flag[2][x1][y1]==1)
return ;
if (!flag[1][x1][y1])
clickl(x1,y1);
}


void right_down(int x,int y)
{
int x1,y1;
draw_face(2);
y1=(x-x0)/(m_wh+1);
x1=(y-y0)/(m_wh+1);
if (!flag[2][x1][y1] && !flag[1][x1][y1] && p[x1][y1]==-1)
{
draw_flag(x1,y1);
countMine++;
countTrueMine++;
}
else if (!flag[2][x1][y1] && !flag[1][x1][y1] && p[x1][y1]!=-1)
{
draw_flag(x1,y1);
countMine++;
}
if (flag[2][x1][y1]==1 && !flag[1][x1][y1] && p[x1][y1]==-1)
{
draw_box(y1*(m_wh+1)+x0,x1*(m_wh+1)+y0,y1*(m_wh+1)+x0+m_wh,x1*(m_wh+1)+y0+m_wh,1);
countMine--;
countTrueMine--;
flag[2][x1][y1]=0;
}
else if(flag[2][x1][y1]==1 && !flag[1][x1][y1] && p[x1][y1]!=-1)
{
draw_box(y1*(m_wh+1)+x0,x1*(m_wh+1)+y0,y1*(m_wh+1)+x0+m_wh,x1*(m_wh+1)+y0+m_wh,1);
countMine--;
flag[2][x1][y1]=0;
}
else
flag[2][x1][y1]=1;
}
void middle_up_draw_box(int x1,int y1)
{
int i,j;
for (i=-1;i<=1;i++)
for (j=-1;j<=1;j++)
{
if (x1+i >=0 && x1+i<m_row && y1+j >=0 && y1+j<m_col )
{
if (!flag[1][x1+i][y1+j] && !flag[2][x1+i][y1+j])
draw_box((j+y1)*(m_wh+1)+x0,(i+x1)*(m_wh+1)+y0,+(j+y1)*(m_wh+1)+x0+m_wh,(i+x1)*(m_wh+1)+y0+m_wh,1);
}
}
}
void middle_up(int x,int y)
{
int x1,y1,countmine=0,countruemine=0,i,j;
draw_face(1);
y1=(x-x0)/(m_wh+1);
x1=(y-y0)/(m_wh+1);
if (flag[1][x1][y1]==0)
{
middle_up_draw_box(x1, y1);
return;
}
for (i=-1;i<=1;i++)
for (j=-1;j<=1;j++)
{
if (j!=0 || i!=0) 
{
if (x1+i >=0 && x1+i<m_row && y1+j >=0 && y1+j<m_col)
{
if (flag[2][x1+i][y1+j]==1)
{
countmine++;
if (p[x1+i][y1+j]==-1)
countruemine++;
}
}
}
}
if (countmine==p[x1][y1] && countruemine==p[x1][y1])
{
for (i=-1;i<=1;i++)
for (j=-1;j<=1;j++)
{
if (j!=0 || i!=0) 
{
if (x1+i >=0 && x1+i<m_row && y1+j >=0 && y1+j<m_col )
{
if (!flag[1][x1+i][y1+j] && !flag[2][x1+i][y1+j])
clickl(x1+i,y1+j);
}
}
}
}
else
{
middle_up_draw_box(x1, y1);
}
}
void middle_down(int x,int y)
{
int x1,y1,i,j;
draw_face(2);
y1=(x-x0)/(m_wh+1);
x1=(y-y0)/(m_wh+1);
for (i=-1;i<=1;i++)
for (j=-1;j<=1;j++)
{
if (i!=0 || j!=0)
{
if (x1+i >=0 && x1+i<m_row && y1+j >=0 && y1+j<m_col )
{
if (!flag[1][x1+i][y1+j] && !flag[2][x1+i][y1+j])
draw_box((j+y1)*(m_wh+1)+x0,(i+x1)*(m_wh+1)+y0,+(j+y1)*(m_wh+1)+x0+m_wh,(i+x1)*(m_wh+1)+y0+m_wh,2);
}
}
}
}
void found_mine()
{
FlushMouseMsgBuffer();
MOUSEMSG m;
int x, y,x1,y1,x2,y2;
char str[4];
x=x0+m_col*(m_wh+1)/2;
y=y0-1.5*m_wh;
x1=x0-m_wh/2;
y1=y0-m_wh*3;
x2=x0+m_col*(m_wh+1)+m_wh/2;
y2=y0+m_row*(m_wh+1)+m_wh/2;
while (1)
{
out_mine_num();
start=clock()-finish;
char_int(start/1000,str);
setcolor(RED);
outtextxy(x2-2.5*m_wh,y0-1.75*m_wh,str);
win();
if (MouseHit()==true)
{
m=GetMouseMsg();
start=clock();
outtextxy(x2-2.5*m_wh,y0-1.75*m_wh,str);
if (m.x>=x0 && m.x<x0+m_col*(m_wh+1) && m.y>=y0 && m.y<y0+m_row*(m_wh+1))
{
switch(m.uMsg)
{
case WM_LBUTTONDOWN:
left_down(m.x,m.y);
break;
case  WM_LBUTTONUP:
left_up(m.x,m.y);
break;
case WM_RBUTTONDOWN:
right_down(m.x,m.y);
break;
case WM_RBUTTONUP :
draw_face(1);
break;
case WM_MBUTTONDOWN:
middle_down(m.x,m.y);
break;
case WM_MBUTTONUP:
middle_up(m.x,m.y);
break;
}
}
if (m.x>=x-m_wh && m.x<=x+m_wh && m.y>=y-m_wh && m.y<=y+m_wh)
{
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
draw_face(2);
break;
case  WM_LBUTTONUP:
draw_face(1);
again_game();
break;
}
}
}
}
}
void again_game()
{
int x,y;
FlushMouseMsgBuffer();
MOUSEMSG m;
x=x0+m_col*(m_wh+1)/2;
y=y0-1.5*m_wh;
while (1)
{
m=GetMouseMsg();
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
if (m.x>=x-m_wh && m.x<=x+m_wh && m.y>=y-m_wh && m.y<=y+m_wh)
draw_face(2);
break;
case WM_LBUTTONUP:
if (m.x>=x-m_wh && m.x<=x+m_wh && m.y>=y-m_wh && m.y<=y+m_wh)
{
cleardevice();
close_system();
init_system();
finish=clock();
draw_face(1);
draw_area();
found_mine();
}
break;
}
}

}
void define()
{
int gdriver=DETECT,gmode;
closegraph();
m_row=16;
m_col=30;
printf("\n\n雷数:");
scanf("%d",&minenum);
m_wh=18;
fflush(stdin);
initgraph(&gdriver,&gmode,"");
}


void windows()
{
int gdriver,gmode;
initgraph(&gdriver,&gmode,"");
MOUSEMSG m;
rectangle(200,70,400,140);
outtextxy(275,100,"初级");
rectangle(200,160,400,230);
outtextxy(275,190,"中级");
rectangle(200,250,400,320);
outtextxy(275,280,"高级");
rectangle(200,340,400,410);
outtextxy(275,360,"自定义");
while (1)
{
m=GetMouseMsg();
if (m.uMsg==WM_LBUTTONDOWN)
{
if (m.x>=200 && m.x < 400 && m.y >= 70 && m.y < 140)
{
m_row=9;
m_col=9;
minenum=10;
m_wh=25;
return ;
}
if (m.x>=200 && m.x <=400 && m.y >= 160 && m.y  <230)
{
m_row=16;
m_col=16;
m_wh=20;
minenum=30;
return;
}
if (m.x>=200 && m.x <=400 && m.y >= 250 && m.y <320)
{
m_row=16;
m_col=30;
minenum=99;
m_wh=18;
return;
}
if (m.x>=200 && m.x <=400 && m.y >= 340 && m.y < 410)
{
define();
return;
}
}
}
}
void write()
{
FILE *fp;
char name[20];
system("cls");
if ((fp=fopen("排行榜","a"))==NULL)
{
printf("打开失败");
exit(0);
}
printf("输入你的大名:");
scanf("%s",name);
fprintf(fp,"姓名:  %s\n",name);
fprintf(fp,"成绩:  %d\n",start/1000);
printf("保存成功!");
exit(0);
fclose(fp);
}
void read()
{
FILE *fp;
char name[20],str[8];
int n;
if ((fp=fopen("排行榜","r"))==NULL)
{
printf("打开失败");
exit(0);
}
system("cls");
while(!feof(fp))
{
fscanf(fp,"姓名:  %s\n",name);
fscanf(fp,"成绩:  %d\n",&n);
printf("姓名:  %s\n",name);
printf("成绩:  %d\n",n);
}
fclose(fp);
}
int init_system()
{
int i,j,k;
m_maxx=getmaxx();
m_maxy=getmaxy();
countMine=0;
countTrueMine=0;
p=(int **)malloc(sizeof(int *)*m_row);
if (!p)
return FALSE;
for (i=0;i<m_row;i++)
{
p[i]=(int *)malloc(sizeof(int)*m_col);
if (!p[i])
return FALSE;
}
for (i=0;i<m_row;i++)
for (j=0;j<m_col;j++)
{
p[i][j]=0;
}
flag=(int ***)malloc(sizeof(int **)*3*m_row*m_col);
if (!flag)
return FALSE;
for (i=0;i<3;i++)
{
flag[i]=(int **)malloc(sizeof(int *)*m_row);
if (!flag[i])
return FALSE;
}
for (i=0;i<3;i++)
{
for (j=0;j<m_row;j++)
{
flag[i][j]=(int *)malloc(sizeof(int)*m_col);
if (!flag[i][j])
return FALSE;
}
}
for (i=0;i<3;i++)
for (j=0;j<m_row;j++)
for (k=0;k<m_col;k++)
{
flag[i][j][k]=0;
}


return TRUE;
}
void close_system()
{
int i,j;
for (i=0;i<m_row;i++)
free(p[i]);
free(p);
for (i=0;i<3;i++)
for (j=0;j<m_row;j++)
free(flag[i][j]);
for (i=0;i<3;i++)
free(flag[i]);
free(flag);
}

默认分类 | 阅读 1120 次
文章评论,共0条
游客请输入验证码
文章分类
文章归档
最新评论