源代码如下
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#define amu 10
#define Lshu 10
void menu()//菜单
{
printf("*************************************\n");
printf("****** 欢 迎 来 到 扫 雷 游 戏 ******\n");
printf("****** 1.开始游戏 0.退出 *******\n");
printf("*************************************\n");
printf("请输入选择\n");
}
void Show( char show[][amu + 2])//打印图像
{
int i, s;
system("cls");
printf(" 0 1 2 3 4 5 6 7 8 9 \n");
for (i = 0; i <= 9; i++)
{
printf("\n");
printf("%d", i);
for (s = 1; s <= 10; s++)
{
printf("%3c", show[i+1][s]);
}
printf("\n");
}
}
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("请输入要选择的坐标\n");
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] = ‘\0‘;
}
if (mine[n][m + 1] == ‘0‘)
{
show[n][m + 1] = ‘\0‘;
}
if (mine[n + 1][m] == ‘0‘)
{
show[n + 1][m] = ‘\0‘;
}
if (mine[n + 1][m+1] == ‘0‘)
{
show[n + 1][m+1] = ‘\0‘;
}
if (mine[n - 1][m+1] == ‘0‘)
{
show[n - 1][m+1] = ‘\0‘;
}
if (mine[n + 1][m-1] == ‘0‘)
{
show[n + 1][m-1] = ‘\0‘;
}
if (mine[n-1][m-1] == ‘0‘)
{
show[n-1][m-1] = ‘\0‘;
}
if (mine[n ][m-1] == ‘0‘)
{
show[n ][m-1] = ‘\0‘;
}
}
else
{
break;
}
Show(show);
}
else
{
printf("输入坐标不正确\n");
}
}
}
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\n You Lost\n");
system("pause");
return 0;
}
原文地址:https://blog.51cto.com/14232799/2379804
时间: 2024-11-14 12:29:04