C语言实现扫雷 【递归扩展】 【超详细解析】

扫雷游戏介绍

《扫雷》是一款大众类的益智小游戏,于1992年发行。 游戏目标是在最短的时间内根据点击格子出现的数字找出所有非雷格子,同时避免踩雷,踩到一个雷即全盘皆输。

游戏规则

 扫雷是电脑上一款非常经典的单机游戏。它的基本规则是,如果你点击一个小网格,就会有一个小块。上面显示的数字表示周围八个网格中有几个雷。例如,1周围只有一个雷。右键可以插入这个网格,左键可以打开网格。

 代码的大致框架

对于扫雷游戏,我们可以将代码进行模块化,与三子棋中一样,将代码分为三个文件

                game.h------用于存放游戏中对代码的声明及头文件引用

                game.c------用于存放代码中实现游戏功能的函数

                 test.c---------代码的逻辑测试、菜单函数的使用

游戏代码的实现 

        对于扫雷函数,代码主要分成三部分  1、雷盘的初始化及定义  2、放置雷   3、扫雷  接下来我们首先进行第一部分的讲解

一、雷盘初始化

我们首先要对雷盘的行、列进行规定,在定义中,我们以ROW代表行,以COL代表列,以此来规定雷盘的大小

同时,我们设置一个变量来存放游戏中雷的数量,便于后面判断游戏的输赢

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<windows.h>

#define ROWS 11
#define COLS 11

#define ROW 9
#define COL 9

#define MINE_COUNT 10

 注意!!!在定义数据时,我们同时创建了一个变量ROWS与COLS,因为在扫雷的规则中,我们需要对目标周围的八个棋格进行判定是否有雷,如果此时我们的排查对象位于雷盘边界,容易造成数组越界,因此我们对雷盘的上下左右四个方位各自多加一行的空间避免了数组越界的情况发生

二、雷盘初始化

在雷盘初始化时,我们选择建立两个雷盘,一个用于存放雷,另一个用于向玩家展示所排查区域附近八个棋格内雷的数量,而对于存放雷的棋盘,我们将无雷处存放‘0’,雷区存放‘1’

对于向玩家展示的雷盘,我们对未知情况的格子存放‘*‘

 雷盘初始化代码如下:

//数组初始化函数的实现
void Init_board(char arr[ROWS][COLS], int rows, int cols, char set)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			arr[i][j] = set;//set表示要存放的字符
		}
	}
}

 

三、雷盘的打印

 在进行雷盘的打印时,我们可以将对应的雷盘的行、列打印在雷盘的前面,便于玩家在扫雷时判断自己所需排查的点的坐标

代码如下:

//打印棋盘
void Display_board(char arr[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("-----------------\n");
	for (i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", arr[i][j]);
		}
		printf("\n");
	}
}

打印效果如图

  接下来,我们将进入第二部分----放置雷

四、雷的放置

在放置雷的过程中,我们需要用到之前在《三子棋》游戏中提及过的srand函数,其次我们要注意随机生成的坐标是否已经有雷放置到该位置上

ps:srand函数可参考《三子棋》中的使用 http://t.csdn.cn/LggZc

代码如下:

//布置地雷函数
void Set_mine(char mine[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int ret = MINE_COUNT;//用于确保雷的数量为设定的数量
	while (ret)
	{
		x = rand() % 9 + 1;//使雷在数组中行下标1~9的位置
		y = rand() % 9 + 1;//使雷在数组中列下标1~9的位置
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			ret--;
		}
	}
}

五、判断周围雷的个数

在玩家扫雷的过程中,我们需要创建一个函数来判断周围的雷的个数,并将个数返回至展示的雷盘中打印出来,此时玩家可根据数字判断雷的坐标

代码如下:

//统计雷的个数
int Find_count(char mine[ROWS][COLS], int x, int y)
{
	return mine[x + 1][y - 1] +
		mine[x][y - 1] +
		mine[x - 1][y - 1] +
		mine[x + 1][y] +
		mine[x - 1][y] +
		mine[x + 1][y + 1] +
		mine[x][y + 1] +
		mine[x - 1][y + 1] - 8 * '0';
}

 六、递归展开空白区域

如果只使用上述代码,那么在游戏过程中玩家需要逐个排查雷,使得操作变得繁琐,此时我们可以创建一个函数,当玩家排查的区域周围雷的个数为0时,周围坐标会进行相应的展开

 在此处,我们可以使用递归的方法来实现

 大致效果如下:

 代码实现:

void BoomBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y,int *pw)//展开一片
{
	int count = 0;
	if (mine[x][y] == '*')
		return ;
	if (x == 0 || x == ROWS - 1 || y == 0 || y == COLS - 1)
		return;
	int ret = Find_count( mine,x,y);
	if (ret == 0)
	{
		(*pw)++;
		show[x][y] = '0';
		if(show[x-1][y-1]=='*')
		BoomBoard(mine, show, x - 1, y - 1,pw);
		if (show[x - 1][y] == '*')
		BoomBoard(mine, show, x - 1, y,pw);
		if (show[x - 1][y + 1] == '*')
		BoomBoard(mine, show, x - 1, y + 1,pw);
		if (show[x][y - 1] == '*')
		BoomBoard(mine, show, x, y - 1,pw);
		if (show[x][y + 1] == '*')
		BoomBoard(mine, show, x, y + 1,pw);
		if (show[x + 1][y - 1] == '*')
		BoomBoard(mine, show, x + 1, y - 1,pw);
		if (show[x + 1][y] == '*')
		BoomBoard(mine, show, x + 1, y,pw);
		if (show[x + 1][y + 1] == '*')
		BoomBoard(mine, show, x + 1, y + 1,pw);
	}
	else if(ret > 0)
	{
		(*pw)++;
		show[x][y] = ret + '0';
		return count;
	}
}

完整函数代码展示:

game.h文件:

#include <time.h>
#include<windows.h>

#define ROWS 11
#define COLS 11

#define ROW 9
#define COL 9

#define MINE_COUNT 10


//数组的初始化函数
void Init_board(char arr[ROWS][COLS], int rows, int cols, char set);

//打印棋盘
void Display_board(char arr[ROWS][COLS], int row, int col);

//布置地雷的函数
void Set_mine(char mine[ROWS][COLS], int row, int col);

//排查地雷
void Find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);


void BoomBoard(char mine[ROWS][COLS], char show[ROWS][COLS],int x,int y,int *pw);

game.c文件:

#include "game.h"

//数组初始化函数的实现
void Init_board(char arr[ROWS][COLS], int rows, int cols, char set)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			arr[i][j] = set;
		}
	}
}

//打印棋盘
void Display_board(char arr[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("-----------------\n");
	for (i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", arr[i][j]);
		}
		printf("\n");
	}
}


//布置地雷函数
void Set_mine(char mine[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int ret = MINE_COUNT;
	while (ret)
	{
		x = rand() % 9 + 1;
		y = rand() % 9 + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			ret--;
		}
	}
}



//统计雷的个数
int Find_count(char mine[ROWS][COLS], int x, int y)
{
	return mine[x + 1][y - 1] +
		mine[x][y - 1] +
		mine[x - 1][y - 1] +
		mine[x + 1][y] +
		mine[x - 1][y] +
		mine[x + 1][y + 1] +
		mine[x][y + 1] +
		mine[x - 1][y + 1] - 8 * '0';
}


//排查地雷
void Find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int count = 0;
	int* pw = &count;
	while (count < row * col - MINE_COUNT)
	{
		printf("请输入想要排查的坐标:");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾,你被炸死了!\n");
				break;
			}
			else
			{
				//Find_count是统计周围雷的个数的函数
				int ret = Find_count(mine, x, y);
				show[x][y] = ret + '0';
				//输入坐标正确后,再打印一次棋盘,方便玩家观察
				if (ret == 0)
				{
					BoomBoard(mine, show, x, y,pw);
				}
				system("cls");			
				Display_board(show, ROW, COL);
			}
		}
		else
		{
			printf("坐标输入错误,请重新输入!\n");
		}
	}
	if (count == row * col - MINE_COUNT)
	{
		printf("恭喜你,排雷成功!\n");
	}
}

void BoomBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y,int *pw)//展开一片
{
	int count = 0;
	if (mine[x][y] == '*')
		return ;
	if (x == 0 || x == ROWS - 1 || y == 0 || y == COLS - 1)
		return;
	int ret = Find_count( mine,x,y);
	if (ret == 0)
	{
		(*pw)++;
		show[x][y] = '0';
		if(show[x-1][y-1]=='*')
		BoomBoard(mine, show, x - 1, y - 1,pw);
		if (show[x - 1][y] == '*')
		BoomBoard(mine, show, x - 1, y,pw);
		if (show[x - 1][y + 1] == '*')
		BoomBoard(mine, show, x - 1, y + 1,pw);
		if (show[x][y - 1] == '*')
		BoomBoard(mine, show, x, y - 1,pw);
		if (show[x][y + 1] == '*')
		BoomBoard(mine, show, x, y + 1,pw);
		if (show[x + 1][y - 1] == '*')
		BoomBoard(mine, show, x + 1, y - 1,pw);
		if (show[x + 1][y] == '*')
		BoomBoard(mine, show, x + 1, y,pw);
		if (show[x + 1][y + 1] == '*')
		BoomBoard(mine, show, x + 1, y + 1,pw);
	}
	else if(ret > 0)
	{
		(*pw)++;
		show[x][y] = ret + '0';
		return count;
	}
}

test.c文件:

#include"game.h"

void game()
{
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };

	//两个数组的初始化
	//mine数组元素全部初始化为字符0
	//show数组元素全部初始化为字符*
	Init_board(mine, ROWS, COLS, '0');
	Init_board(show, ROWS, COLS, '*');


	//打印棋盘,以便让玩家可以清楚的选择相应的位置对应的坐标
	Display_board(show, ROW, COL);

	//布置地雷
	Set_mine(mine, ROW, COL);
	//Display_board(mine, ROW, COL);
	//排查地雷
	Find_mine(mine, show, ROW, COL);
	//两个数组都传入是因为排查时两个数组元素都会有相应的改变
	//排查结束,再打印一次地雷棋盘,向玩家展示地雷的位置
	Display_board(mine, ROW, COL);
}


void menu()
{
	printf("*******************************\n");
	printf("*********  1. play  ***********\n");
	printf("*********  0. exit  ***********\n");
	printf("*******************************\n");
}


void test()
{
	int input = 0;
	do
	{
		menu();
		srand((unsigned int)time(NULL));
		printf("请选择:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出扫雷!\n");
			break;
		default:
			printf("输入错误,请重新输入!\n");
			break;
		}
	} while (input);
}

int main()
{
	test();
	return 0;
}

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值