初识C语言——扫雷游戏(内附完整代码+详细解释)


一、游戏各功能模块实现

1.雷区的定义

//雷区的行列数
//实际显示的大小,即所显示雷区的大小
#define ROW 9
#define COL 9

//实际定义的数组大小,
#define ROWS ROW+2
#define COLS COL+2

目的是在判断每一格周围8格时,不需要判断是否是边界还是角落。如图:
在这里插入图片描述

char mine[ROWS][COLS] = { 0 };//存放地雷的信息,不会显示到屏幕上
char show[ROWS][COLS] = { 0 };//存放排查结果,随着玩家每次扫雷而变化

2.雷区初始化

初始化两个数组,分别存放‘#’和‘*’

	//初始化
	InitBoard(mine, ROWS, COLS, '#');
	InitBoard(show, ROWS, COLS, '*');
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ch)
{
	int i = 0, j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = ch;
		}
	}
}

3.显示雷区

//打印雷区
DisplayBoard(show, ROW, COL);
//打印雷区
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0, 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 ", board[i][j]);
		}
		printf("\n");
	}
}

4.布置雷区

随机布置雷,在mine数组里随机设置easy_count个雷。用0表示地雷,替换#。

	//布置雷区
	SetMine(mine, ROW, COL);
//设置的雷的个数
#define easy_count 80
//布置雷区
void SetMine(char board[ROWS][COLS], int row, int col)
{

	//随机布置雷,在mine数组里随机设置easy_count个雷
	int count = easy_count;
	int x = 0;
	int y = 0;
	while (count)
	{
		x = rand() % row + 1;
		y = rand() % col + 1;
		if (board[x][y]!= '0')
		{
			board[x][y] = '0';//布置成功,用0表示地雷,替换#。
			count--;
		}
	}
}

5.获取某坐标周围8个位置地雷的个数

计算所排位置周围的8个位置雷的个数,如果有一个’0’,则代表有一个雷。

//计算所排位置周围的8个位置雷的个数
int GetMineNumber(char mine[ROWS][COLS], int x, int y)
{
	int count = 0;
	for (int i = x - 1; i <= x + 1; i++)
	{
		for (int j = y - 1; j <= y + 1; j++)
		{
			if (mine[i][j] == '0')
			{
				count++;
			}
		}
	}
	return count;
}

6.扫雷

注:递归函数见7。

	//扫雷
	FindMine(mine, show, ROW, COL);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0, y = 0;
	int num = 0;
	int win = 0;//纪录整个雷区安全区域的格子数
	while (win < row * col - easy_count)
	{
		printf("请输入排雷坐标:");
		scanf("%d %d", &x, &y);
		//判断坐标是否越界,如果越界,重新输入
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			//判断是否扫到雷
			if (mine[x][y] == '0')
			{
				printf("\n点有点背,你被炸死了\n");
				break;
			}
			else
			{

				expand(mine, show, x, y, &win);//递归展开周围的格子
				system("cls");//清理屏幕
				DisplayBoard(show, ROW, COL);//显示当前扫雷结果
			}
		}
		else
		{
			printf("\n你扫到雷区外了,请重新输入\n");
		}
	}
	if (win == row * col - easy_count)
	{
		printf("\n扫雷成功,你他娘的还真是个天才!!!\n");
		DisplayBoard(mine, ROW, COL);//显示雷区
		printf("\n请选择继续or退出\n");
	}
	else
	{
		DisplayBoard(mine, ROW, COL);
		printf("\n请选择继续or退出\n");
	}
}

7.递归展开

如下图,这一步是为了使某一格周围没有地雷时,展开周围所用没有地雷的地方,直至有雷。
在这里插入图片描述

expand(mine, show, x, y, &win);
//递归展开

void expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int* win)
{

	if (x >= 1 && x <= ROW && y >= 1 && y <= COL) //防止越界
	{
		int count = GetMineNumber(mine, x, y);//获取雷数
		//如果周围8格没雷,递归展开
		if (count == 0) 
		{
			//没雷的地方用空格代替
			show[x][y] = ' ';

			int i = 0;
			//分别对周围8个格子递归
			for (i = x - 1; i <= x + 1; i++)
			{
				int j = 0;
				for (j = y - 1; j <= y + 1; j++)
				{

					//只递归没有扫过的格子
					if (show[i][j] == '*')
					{
						expand(mine, show, i, j, win);
					}

				}
			}
		}
		//纪录周围的地雷个数
		else   
		{
			show[x][y] = count + '0';
		}
		//记录扫过的格数
		(*win)++;
	}
}

二、运行结果

游戏设置:
雷区大小:9*9
地雷个数:10
在这里插入图片描述

1.扫雷成功

在这里插入图片描述

2.扫雷失败

在这里插入图片描述

三、完整代码

在这里插入图片描述

1.game.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//雷区的行列数
//实际显示的大小,即所显示雷区的大小
#define ROW 9
#define COL 9

//实际定义的数组大小
#define ROWS ROW+2
#define COLS COL+2

//设置的雷的个数
#define easy_count 10

//初始化雷区
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ch);

//打印雷区
void DisplayBoard(char board[ROWS][COLS], int row, int col);

//布置雷区
void SetMine(char board[ROWS][COLS], int row, int col);

//计算所排位置周围的雷的个数
int GetMineNumber(char mine[ROWS][COLS], int x, int y);

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

//递归展开
void expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int* win);

2.game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include<string.h>
#include"game.h"

//初始化雷区
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ch)
{
	int i = 0, j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = ch;
		}
	}
}

//打印雷区
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0, 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 ", board[i][j]);
		}
		printf("\n");
	}
}

//布置雷区
void SetMine(char board[ROWS][COLS], int row, int col)
{
	int count = easy_count;
	int x = 0;
	int y = 0;
	while (count)
	{
		x = rand() % row + 1;
		y = rand() % col + 1;
		if (board[x][y]!= '0')
		{
			board[x][y] = '0';
			count--;
		}
	}
}

//计算所排位置周围的雷的个数
int GetMineNumber(char mine[ROWS][COLS], int x, int y)
{
	int count = 0;
	for (int i = x - 1; i <= x + 1; i++)
	{
		for (int j = y - 1; j <= y + 1; j++)
		{
			if (mine[i][j] == '0')
			{
				count++;
			}
		}
	}
	return count;
}

//排雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0, y = 0;
	int num = 0;
	int win = 0;
	while (win < row * col - easy_count)
	{
		printf("请输入排雷坐标:");
		scanf("%d %d", &x, &y);
		//判断坐标是否越界,如果越界,重新输入
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '0')
			{
				printf("\n点有点背,你被炸死了\n");
				break;
			}
			else
			{

				expand(mine, show, x, y, &win);
				system("cls");
				DisplayBoard(show, ROW, COL);
				//num = GetMineNumber(mine, x, y);
				//show[x][y] = '0' + num;
				//win++;
				//DisplayBoard(show, ROW, COL);
			}
		}
		else
		{
			printf("\n你扫到雷区外了,请重新输入\n");
		}
	}
	if (win == row * col - easy_count)
	{
		printf("\n扫雷成功,你他娘的还真是个天才!!!\n");
		DisplayBoard(mine, ROW, COL);
		printf("\n请选择继续or退出\n");
	}
	else
	{
		DisplayBoard(mine, ROW, COL);
		printf("\n请选择继续or退出\n");
	}
}

//递归展开

void expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int* win)
{

	if (x >= 1 && x <= ROW && y >= 1 && y <= COL) //防止越界
	{
		int count = GetMineNumber(mine, x, y);//获取雷数
		//如果周围8格没雷,递归展开
		if (count == 0) 
		{
			//没雷的地方用空格代替
			show[x][y] = ' ';

			int i = 0;
			//分别对周围8个格子递归
			for (i = x - 1; i <= x + 1; i++)
			{
				int j = 0;
				for (j = y - 1; j <= y + 1; j++)
				{

					//只递归没有扫过的格子
					if (show[i][j] == '*')
					{
						expand(mine, show, i, j, win);
					}

				}
			}
		}
		//纪录周围的地雷个数
		else   
		{
			show[x][y] = count + '0';
		}
		//记录扫过的格数
		(*win)++;
	}
}

3.test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

void menu()
{
	printf("**************************\n");
	printf("********* 1.Game**********\n");
	printf("********* 0.Exit**********\n");
	printf("**************************\n");
}

void game()
{
	char mine[ROWS][COLS] = { 0 };//存放地雷的信息
	char show[ROWS][COLS] = { 0 };//存放排查结果

	//初始化
	InitBoard(mine, ROWS, COLS, '#');
	InitBoard(show, ROWS, COLS, '*');

	//打印雷区
	//DisplayBoard(mine, ROW, COL);
	DisplayBoard(show, ROW, COL);

	//布置雷区
	SetMine(mine, ROW, COL);
	//DisplayBoard(mine, ROW, COL);
	
	//扫雷
	FindMine(mine, show, ROW, COL);
}


void test()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		//打印菜单
		menu();

		printf("请选择:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("游戏开始\n");
			game();
			break;
		case 0:
			printf("退出游戏!!\n");
			break;
		default:
			printf("请输入正确指令,否则你的电脑将会爆炸!!!\n");
			break;
		}

	} while (input);
}

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

总结

本文对扫雷游戏的各个功能模块进行了描述和实现,并展示了运行结果。附有完整代码供参考。如果有疑问的地方,欢迎留言或私信。

  • 21
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值