C语言实现扫雷(包含递归展开)

C语言实现扫雷

扫雷介绍

此为用控制台实现的简单扫雷 默认为9*9 10个雷

所用函数包括

1.InitBoard()—初始化棋盘
2.DisplayBoard()—打印棋盘
3.Set_mine()—布置雷
4.Find_mine()—排查雷

4.1Open_show()展开函数

1.战前准备

在这里插入图片描述
这是棋盘 虽然是99但我们需要的是1111 单打印时只显示99
因为在排查边界时 要11
11才能完美处理边界的格子

有两个数组 show _mine[][] real_mine[][]

show 就是我们展示给玩家的棋盘 而real 这个真实的棋盘负责接收布置的10个雷
当玩家排查雷时 我们把排查信息给到show数组 而real数组不需要变
想一下 这个布置雷的棋盘 我们是不是只用来计算玩家排查出雷的信息

在这里插入图片描述
这是所需文件

2.代码实现

1.数据准备

game.h

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define COUNT 10 —雷的个数
char mine[ROWS][COLS];
char show[ROWS][COLS];

2.初始化数组(棋盘 )

void InitBoard(char arr[][COLS], int rows, int cols,char set)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
arr[i][j] = set;
}
}
}

char set 是我们要初始化的内容 show数组初始化为‘0’ 代表目前还没有雷
而 mine数组 初始化为 ’ * ’

3.打印棋盘

void DisplayBoard(char arr[][COLS], int row, int col) {
for (int i = 0; i <= row; i++)
{
printf(“%d “, i);
}printf(”\n”);
for (int i = 1; i <= row; i++)
{
printf(“%d “, i);
for (int j = 1; j <= col; j++)
{
printf(”%c “, arr[i][j]);
}printf(”\n”);
}
}

4.布置雷

void Set_mine(char mine[][COLS], int row, int col)
{
int x = 0, y = 0,count=0;
count = COUNT;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;

	if (mine[x][y] == '0')
	{
		mine[x][y] = '1';
		count--;
	}

}

}

5.扫雷

void Find_mine_show(char mine[][COLS], char show[][COLS], int rows, int cols)
{
int x = 0, y = 0;
while (win<ROW*COL-COUNT)
{
printf(“请输入坐标\n”);
scanf_s(“%d%d”, &x, &y);
if (x >= 1 && x <= rows - 2 && y >= 1 && y <= cols - 2)
{
if (mine[x][y] == ‘0’)
{
//返回排查雷的个数 并展开
Count_set_mine(mine, show, x,y,rows, cols,&win);
DisplayBoard(show, rows - 2, cols - 2);
//计算win
Win(show, rows - 2, cols - 2);
printf(“%d”, win);
}
else if(mine[x][y]==‘1’)
{
printf(“你被炸死了\n”);
DisplayBoard(show, rows-2, cols-2);
break;
}
else
{
printf(“输入错误 请重新输入\n”);
}
}
}

}

4,1 返回扫雷结果和展开周围

void Count_set_mine(char mine[][COLS],char show[][COLS],int x,int y,int rows,int cols)
{
if (mine[x][y] == ‘0’ && x >= 1 && x <= rows - 2 && y >= 1 && y <= cols - 2 &&show[x][y]==‘*’)
{
int ret = mine[x][y + 1] + mine[x][y - 1]
+ mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1]
+ mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1]
- 8 * ‘0’;
show[x][y] = ret + ‘0’;

	if (show[x][y] == '0')
	{
		show[x][y] = ' ';
		Count_set_mine(mine, show, x + 1, y, rows, cols);

		Count_set_mine(mine, show, x + 1, y - 1, rows, cols);

		Count_set_mine(mine, show, x + 1, y + 1, rows, cols);

		Count_set_mine(mine, show, x, y - 1, rows, cols);

		Count_set_mine(mine, show, x, y + 1, rows, cols);

		Count_set_mine(mine, show, x - 1, y, rows, cols);

		Count_set_mine(mine, show, x - 1, y - 1, rows, cols);

		Count_set_mine(mine, show, x - 1, y + 1, rows, cols);
	}
}
else
	return  ;

}

4.2 计算已经排查的进度

void Win(char show[][COLS], int rows,int cols)
{
for (int i = 1; i <= rows - 2; i++)
{
for (int j = 1; j <= cols - 2; j++)
{
if (show[i][j] != ‘*’)
win++;
}
}
}

5 打印菜单

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值