源代码如下
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#define amu 10
#define Lshu 10
void menu()//菜单
{
printf("*************************************
");
printf("****** 欢 迎 来 到 扫 雷 游 戏 ******
");
printf("****** 1.开始游戏 0.退出 *******
");
printf("*************************************
");
printf("请输入选择
");
}
void Show( char show[][amu + 2])//打印图像
{
int i, s;
system("cls");
printf(" 0 1 2 3 4 5 6 7 8 9
");
for (i = 0; i <= 9; i++)
{
printf("
");
printf("%d", i);
for (s = 1; s <= 10; s++)
{
printf("%3c", show[i+1][s]);
}
printf("
");
}
}
int getnum(char mine[][amu+2], int i, int j)//所选坐标周围的雷数量
{
return mine[i - 1][j - 1] + mine[i - 1][j] +
mine[i - 1][j + 1] + mine[i][j - 1] +
mine[i][j + 1] + mine[i + 1][j - 1] +
mine[i + 1][j] + mine[i + 1][j + 1] - 8 * '0';
}
void fullmine(char mine[][amu + 2], int sta, int end)//埋雷
{
int sum = 0, n, m;
srand((unsigned int)time(NULL));
while (1)
{
n = rand() % (end - sta + 1) + sta;
m = rand() % (end - sta + 1) + sta;
if (mine[n][m] == '0')
{
mine[n][m] = '1';
sum++;
}
if (sum == Lshu)
{
return;
}
}
}
int judge(char mine[][amu + 2], int n, int m)//判断是否踩雷
{
if (mine[n][m] != '1')
{
return 1;
}
return 0;
}
void game(char show[][amu + 2], char mine[][amu + 2], int sta, int end)
{
int sum = 0, n, m,p;
Show(show);
fullmine(mine, sta, end);
while (1)
{
printf("请输入要选择的坐标
");
scanf("%d%d", &n, &m);
n++; m++;
if (n >=1 && n <= amu && m >= 1 && m <= amu)
{
p = judge(mine, n, m);
if (p == 1)
{
sum = getnum(mine, n, m);
show[n][m] = sum + '0';
if (mine[n-1][m] == '0')//坐标周围展开
{
show[n-1][m] = ' ';
}
if (mine[n][m + 1] == '0')
{
show[n][m + 1] = ' ';
}
if (mine[n + 1][m] == '0')
{
show[n + 1][m] = ' ';
}
if (mine[n + 1][m+1] == '0')
{
show[n + 1][m+1] = ' ';
}
if (mine[n - 1][m+1] == '0')
{
show[n - 1][m+1] = ' ';
}
if (mine[n + 1][m-1] == '0')
{
show[n + 1][m-1] = ' ';
}
if (mine[n-1][m-1] == '0')
{
show[n-1][m-1] = ' ';
}
if (mine[n ][m-1] == '0')
{
show[n ][m-1] = ' ';
}
}
else
{
break;
}
Show(show);
}
else
{
printf("输入坐标不正确
");
}
}
}
int main()
{
int input, sta = 1, end = 10;
char mine[amu + 2][amu + 2];
char show[amu + 2][amu + 2];
memset(show, '#', sizeof(show));
memset(mine, '0', sizeof(mine));
menu();
scanf("%d", &input);
if (input == 1)
{
game(show, mine, sta, end);//游戏进程
}
else
{
return 0;
}
Show(mine);
printf("Game Over
You Lost
");
system("pause");
return 0;
}