地雷C语言
发布时间: 2023-02-14 08:13:18
在这上面不好说明, 我有C的代码, 你看一下(DEVC++)
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#definen15
intrestart=0;
intlast_sel_x,last_sel_y;
charin[20];
structPOINT
{
intx;
inty;
}pt;
//设置CMD窗口光标位置
voidsetxy(intx,inty)
{
COORDcoord={x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
//获取当前CMD当前光标所在位置
voidgetxy()
{
HANDLEhConsole=GetStdHandle(STD_OUTPUT_HANDLE);
COORDcoordScreen={0,0};//光标位置
CONSOLE_SCREEN_BUFFER_INFOcsbi;
if(GetConsoleScreenBufferInfo(hConsole,&csbi))
{
//printf("光标坐标:(%d,%d) ",csbi.dwCursorPosition.X,csbi.dwCursorPosition.Y);
pt.x=csbi.dwCursorPosition.X;
pt.y=csbi.dwCursorPosition.Y;
}
}
structA
{
intvalue;//-1为雷
intstate;//显示状态:0为未打开,1为已打开
intlock;//锁定状态
intbomb;//雷已标记:0为未标记,1为已标记
};
structAs[10][10];
intcalc()
{
inti,j,count=0;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(s[i][j].state==0)count++;
}
}
returncount;
}
intprt()
{
system("cls");
intcount=calc();
inti,j;
printf("%3c",'');
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|
FOREGROUND_GREEN);
for(i=0;i<10;i++)
{
printf("%3d",i);
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|
FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
for(i=0;i<10;i++)
{
printf(" ");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|
FOREGROUND_GREEN);
printf("%3d",i);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|
FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
for(j=0;j<10;j++)
{
if(s[i][j].bomb==1)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|
FOREGROUND_RED);
printf("%3c",'*');
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|
FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
}
elseif(s[i][j].state==1)
{
if(s[i][j].value==0)printf("%3c",'');
elseprintf("%3d",s[i][j].value);
}
else
{
printf("%3c",'-');
}
/* if(s[i][j].value==-1) printf("%3c",'*');
elseprintf("%3d",s[i][j].value);*/
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|
FOREGROUND_GREEN);
printf("%3d",i);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|
FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
}
printf(" ");
printf("%3c",'');
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|
FOREGROUND_GREEN);
for(i=0;i<10;i++)
{
printf("%3d",i);
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|
FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
printf(" ");
getxy();
setxy(45,0);
printf("%d",count);
setxy(40,5);
printf("说明");
setxy(40,7);
printf("1:输入*xy(如:*55),则把第5行第5列");
setxy(40,8);
printf("标记为地雷");
setxy(40,10);
printf("2:输入xy(如55),则把第5行第5列打开");
if(count==n)
{
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(s[i][j].value==-1&&s[i][j].bomb==0)s[i][j].bomb=1;
if(s[i][j].value!=-1&&s[i][j].state==0)s[i][j].state=1;
}
}
setxy(50,2);
printf("success!");
setxy(pt.x,pt.y);
fflush(stdin);
getchar();
return1;
}
setxy(pt.x,pt.y);
return0;
}
voidspace_process(intx,inty)
{
inti,j;
if(x-1>=0&&y-1>=0)
{
if(s[x-1][y-1].value==0&&s[x-1][y-1].lock==0){s[x-1][y-1].state=1;s[x-1][y-1].lock=1;space_process(x-1,y-1);}
elseif(s[x-1][y-1].value!=-1)s[x-1][y-1].state=1;
}
if(x-1>=0)
{
if(s[x-1][y].value==0&&s[x-1][y].lock==0){s[x-1][y].state=1;s[x-1][y].lock=1;space_process(x-1,y);}
elseif(s[x-1][y].value!=-1)s[x-1][y].state=1;
}
if(x-1>=0&&y+1<10)
{
if(s[x-1][y+1].value==0&&s[x-1][y+1].lock==0){s[x-1][y+1].state=1;s[x-1][y+1].lock=1;space_process(x-1,y+1);}
elseif(s[x-1][y+1].value!=-1)s[x-1][y+1].state=1;
}
if(y-1>=0)
{
if(s[x][y-1].value==0&&s[x][y-1].lock==0){s[x][y-1].state=1;s[x][y-1].lock=1;space_process(x,y-1);}
elseif(s[x][y-1].value!=-1)s[x][y-1].state=1;
}
if(y+1<10)
{
if(s[x][y+1].value==0&&s[x][y+1].lock==0){s[x][y+1].state=1;s[x][y+1].lock=1;space_process(x,y+1);}
elseif(s[x][y+1].value!=-1)s[x][y+1].state=1;
}
if(x+1<10&&y-1>=0)
{
if(s[x+1][y-1].value==0&&s[x+1][y-1].lock==0){s[x+1][y-1].state=1;s[x+1][y-1].lock=1;space_process(x+1,y-1);}
elseif(s[x+1][y-1].value!=-1)s[x+1][y-1].state=1;
}
if(x+1<10)
{
if(s[x+1][y].value==0&&s[x+1][y].lock==0){s[x+1][y].state=1;s[x+1][y].lock=1;space_process(x+1,y);}
elseif(s[x+1][y].value!=-1)s[x+1][y].state=1;
}
if(x+1<10&&y+1<10)
{
if(s[x+1][y+1].value==0&&s[x+1][y+1].lock==0){s[x+1][y+1].state=1;s[x+1][y+1].lock=1;space_process(x+1,y+1);}
elseif(s[x+1][y+1].value!=-1)s[x+1][y+1].state=1;
}
}
intprocess_char(char*t,int*i,int*j)
{
intlen=strlen(t);
intx,y=0;
for(x=0;x<len;x++)
{
if(t[x]=='')
{
continue;
}
else
{
t[y++]=t[x];
}
}
t[y]='