#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LINE 10 //设置行数
#define LIST 10 //设置列数
void init_board(char arr[LINE + 2][LIST + 2])//初始化雷区
{
char *p = &arr[0][0];
for (int i = 0; i < (LINE + 2) *(LIST + 2); i++)
{
*(p + i) = ' ';
}
}
void setc4_board(char arr[LINE + 2][LIST + 2], int num)//设置雷
{
srand((unsigned)time(NULL));
while (num)
{
int i = rand() % LINE + 1;
int j = rand() % LIST + 1;
if (' ' == arr[i][j])
{
if ((i >= 1 && i <= LINE) && (j >= 1 && j <= LIST))
{
arr[i][j] = 'o';
num--;
}
}
}
}
void reckon_board(char arr[LINE + 2][LIST + 2])//计算非雷处数字
{
for (int i = 1; i <= LINE; i++)
{
for (int j = 1; j <= LIST; j++)
{
if (arr[i][j] != 'o')
{
arr[i][j] = ((arr[i - 1][j - 1] == 'o') + (arr[i - 1][j] == 'o') + (arr[i - 1][j + 1] == 'o') + (arr[i][j - 1] == 'o')
+ (arr[i][j + 1] == 'o') + (arr[i + 1][j - 1] == 'o') + (arr[i + 1][j] == 'o') + (arr[i + 1][j + 1] == 'o')) + '0';
}
if (arr[i][j] == '0')
{
arr[i][j] = '*';
}
}
}
}
void print_board(char arr[LINE + 2][LIST + 2])//打印
{
for (int i = 1; i <= LIST; i++)
{
printf(" %2d ",i);
}
printf("\n");
for (int i = 1; i <= LINE; i++)
{
for (int j = 1; j <= LIST; j++)
{
printf("%2c |", arr[i][j]);
}
printf("%d", i);
printf("\n");
for (int d = 1; d<= LIST; d++)
{
printf("%____|");
}
printf("\n");
}
}
int count_board(char arr[LINE + 2][LIST + 2])//计数
{
int count = 0;
for (int i = 1; i <= LINE; i++)
{
for (int j = 1; j <= LIST; j++)
{
if ((arr[i][j] != ' ') && (arr[i][j] != 'o'))
count++;
}
}
return count;
}
int judge(char arr1[LINE + 2][LIST + 2], char arr2[LINE + 2][LIST + 2], int x, int y, int p)//判断
{
if (arr1[x][y] == 'o')
{
return 1;//炸死
}
else if (count_board(arr2) == (LINE*LIST - p))
{
return -1;//玩家赢
}
return NULL;
}
void arr1_arr2(char arr1[LINE + 2][LIST + 2], char arr2[LINE + 2][LIST + 2],int a,int b)//周围没雷直接展开
{
if (arr1[a][b] == '*')
{
for (int i = 1; i <= LINE; i++)
{
for (int j = 1; j <= LIST; j++)
{
if (arr1[i][j] == '*')
{
arr2[i][j] = arr1[i][j];
arr2[i - 1][j - 1] = arr1[i - 1][j - 1];
arr2[i - 1][j] = arr1[i - 1][j];
arr2[i - 1][j + 1] = arr1[i - 1][j + 1];
arr2[i][j - 1] = arr1[i][j - 1];
arr2[i][j + 1] = arr1[i][j + 1];
arr2[i + 1][j - 1] = arr1[i + 1][j - 1];
arr2[i + 1][j] = arr1[i + 1][j];
arr2[i + 1][j + 1] = arr1[i + 1][j + 1];
}
}
}
}
else
{
arr2[a][b] = arr1[a][b];
}
}
char *find(char arr[LINE + 2][LIST + 2])//找到一个非雷处
{
for (int i = 1; i <= LINE; i++)
{
for (int j = 1; j <= LIST; j++)
{
if (arr[i][j] != 'o')
{
return &arr[i][j];
}
}
}
return NULL;
}
void meau()//菜单
{
printf("********************\n");
printf("*****1.开始游戏*****\n");
printf("*****0.退出游戏*****\n");
printf("********************\n");
}
int main()
{
int x, y, c4, num;//x y-> 坐标 c->雷的个数
char arr1[LINE + 2][LIST + 2];
char arr2[LINE + 2][LIST + 2];
do
{
meau();
printf("请选择>:");
scanf("%d", &num);
switch (num)
{
case 1:
init_board(arr1);
init_board(arr2);
printf("设置雷的个数:");
scanf("%d", &c4);
setc4_board(arr1, c4);
reckon_board(arr1);
print_board(arr2);
int total = 1;
while (1)
{
while (total <= LINE*LIST)
{
printf("输入想要的位置坐标>");
scanf("%d %d", &x, &y);
if ((x >= 1 && x <= LINE) && (y >= 1 && y <= LIST))
{
if ((total == 1) && (arr1[x][y] == 'o')&&(c4 != LINE * LIST))//第一次若是雷,与一个非雷处进行交换
{
char *e = find(arr1);
char temp = arr1[x][y];
arr1[x][y] = *e;
*e = temp;
reckon_board(arr1);
}
total++;
break;
}
else{
printf("坐标超出范围\n>");
}
}
arr1_arr2(arr1, arr2,x,y);
print_board(arr2);
if (judge(arr1, arr2, x, y, c4) == 1)
{
print_board(arr1);
printf("你被炸死了!游戏结束\n");
break;
}
else if (judge(arr1, arr2, x, y, c4) == -1)
{
print_board(arr1);
printf("你赢了!\n");
break;
}
}
break;
case 0:
break;
default:
printf("输入错误重新选择>\n:");
break;
}
} while (num);
return 0;
}
C语言实现扫雷
最新推荐文章于 2022-07-03 14:05:58 发布