懒得注释,直接上代码!
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define YUTUHEIGHT 10
#define YUTUWIDHT 10
#define BOON_NUM 10
struct square
{
bool isOpen;
int groundBoon;
};
square map[YUTUHEIGHT][YUTUWIDHT];
void csh()
{
int *p;
for (int i = 0;i < YUTUHEIGHT;i++)
for (int j = 0;j < YUTUWIDHT;j++)
{
map[i][j].groundBoon = 0;
map[i][j].isOpen = false;
}
srand(time(NULL));
p = &map[rand() % YUTUHEIGHT][rand() % YUTUWIDHT].groundBoon;
*p = -1;
for(int i=0;i<BOON_NUM-1;i++)
{
while (*p == -1)
{
p = &map[rand() % YUTUHEIGHT][rand() % YUTUWIDHT].groundBoon;
}
*p = -1;
}
}
void sc()
{
printf(" ");
for (int i = 0;i < YUTUWIDHT;i++)
printf("%d ",i);
printf("\n");
for (int i = 0;i < YUTUHEIGHT;i++)
{
printf("%d ",i);
for (int j = 0;j < YUTUWIDHT;j++)
{
if (map[i][j].isOpen)
if (map[i][j].groundBoon==-1)
printf("* ");
else if (map[i][j].groundBoon == 0)
printf(" ");
else
printf("%d ", map[i][j].groundBoon);
else
printf("# ");
}
printf("\n");
}
printf("\nYUTU:“有%d个雷”\n",BOON_NUM);
}
void bigOpen(int openH, int openW)
{
int temp;
int ground(int openH, int openW);
map[openH][openW].groundBoon = 0;
map[openH][openW].isOpen = true;
if (openH - 1 >= 0 && openW - 1 >= 0&&map[openH - 1][openW - 1].isOpen == false)
{
temp = ground(openH-1, openW-1);
map[openH - 1][openW - 1].groundBoon = temp;
map[openH - 1][openW - 1].isOpen = true;
}
if (openH - 1 >= 0 && map[openH - 1][openW].isOpen == false)
{
temp = ground(openH-1, openW);
map[openH - 1][openW].groundBoon = temp;
map[openH - 1][openW].isOpen = true;
}
if (openH - 1 >= 0 && openW + 1 < YUTUWIDHT && map[openH - 1][openW + 1].isOpen == false)
{
temp = ground(openH-1, openW+1);
map[openH - 1][openW + 1].groundBoon = temp;
map[openH - 1][openW + 1].isOpen = true;
}
if (openW - 1 >= 0 && map[openH][openW - 1].isOpen == false)
{
temp = ground(openH, openW-1);
map[openH][openW - 1].groundBoon = temp;
map[openH][openW - 1].isOpen = true;
}
if (openW + 1 < YUTUWIDHT && map[openH][openW + 1].isOpen == false)
{
temp = ground(openH, openW+1);
map[openH][openW + 1].groundBoon = temp;
map[openH][openW + 1].isOpen = true;
}
if (openH + 1 < YUTUHEIGHT && openW - 1 >= 0 && map[openH + 1][openW - 1].isOpen == false)
{
temp = ground(openH+1, openW-1);
map[openH + 1][openW - 1].groundBoon = temp;
map[openH + 1][openW - 1].isOpen = true;
}
if (openH + 1 < YUTUHEIGHT && map[openH + 1][openW].isOpen == false)
{
temp = ground(openH+1, openW);
map[openH + 1][openW].groundBoon = temp;
map[openH + 1][openW].isOpen = true;
}
if (openH + 1 < YUTUHEIGHT && openW + 1 < YUTUWIDHT && map[openH + 1][openW + 1].isOpen == false)
{
temp = ground(openH+1, openW+1);
map[openH + 1][openW + 1].groundBoon = temp;
map[openH + 1][openW + 1].isOpen = true;
}
}
int ground(int openH, int openW)
{
int sum = 0;
if (openH - 1 >= 0 && openW - 1 >= 0 && map[openH - 1][openW - 1].groundBoon == -1)
sum++;
if (openH - 1 >= 0 && map[openH - 1][openW].groundBoon == -1)
sum++;
if (openH - 1 >= 0 && openW + 1 < YUTUWIDHT && map[openH - 1][openW + 1].groundBoon == -1)
sum++;
if ( openW - 1 >= 0 && map[openH ][openW - 1].groundBoon == -1)
sum++;
if (openW + 1 < YUTUWIDHT && map[openH][openW +1].groundBoon == -1)
sum++;
if (openH + 1 < YUTUHEIGHT && openW - 1 >= 0 && map[openH +1][openW - 1].groundBoon == -1)
sum++;
if (openH + 1 < YUTUHEIGHT && map[openH+ 1][openW ].groundBoon == -1)
sum++;
if (openH +1 < YUTUHEIGHT && openW + 1 < YUTUWIDHT && map[openH + 1][openW + 1].groundBoon == -1)
sum++;
if (sum == 0)
bigOpen(openH, openW);
return sum;
}
bool open(int openH,int openW)
{
if (map[openH][openW].isOpen)
return false;
if (map[openH][openW].groundBoon == -1)
return true;
map[openH][openW].groundBoon=ground(openH, openW);
map[openH][openW].isOpen = true;
return false;
}
void end()
{
for (int i = 0;i < YUTUHEIGHT;i++)
for (int j = 0;j < YUTUWIDHT;j++)
if (map[i][j].groundBoon == -1)
map[i][j].isOpen = true;
sc();
}
bool win()
{
for (int i = 0;i < YUTUHEIGHT;i++)
for (int j = 0;j < YUTUWIDHT;j++)
if (!map[i][j].isOpen && map[i][j].groundBoon != -1)
return false;
return true;
}
int main()
{
bool isEnd=false,isWin=false;
int openH, openW;
int isAgain;
csh();
while(!isEnd&&!isWin)
{
sc();
printf("请输入要打开的格子行数:");
scanf_s("%d",&openH);
while (openH<0||openH>YUTUHEIGHT)
{
printf("错误!请输入正确的行数:");
scanf_s("%d", &openH);
}
printf("请输入要打开的格子列数:");
scanf_s("%d", &openW);
while (openW<0 || openW>YUTUHEIGHT)
{
printf("错误!请输入正确的列数:");
scanf_s("%d", &openW);
}
isEnd=open(openH,openW);
isWin = win();
system("cls");
}
end();
if (isWin)
printf("成功!\n");
else
printf("失败!\n");
printf("输入0重开游戏,输入其他任意数字退出游戏:");
scanf_s("%d",&isAgain);
if (isAgain == 0)
{
system("cls");
main();
}
}