地雷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]='