用c语言实现简易c语言扫雷游戏

cpp 复制代码
void test(void)
{
	int input = 0;
	
	do
	{
		menu();
		printf("请选择: >");
		scanf("%d", &input);
		switch (input)
		{
			menu();
			case 1:
				printf("扫雷\n");
				game();
				break;
			case 2:
				printf("退出游戏\n");
				break;
			default:
				printf("输入错误,请重新输入");
				break;
		}
	} while (input);//0退出游戏


}
int main(void)
{

	test();
	return 0;
}

1、主函数及游戏开始结束的界面设计

cpp 复制代码
void menu()
{
	printf("*************************\n");
	printf("*******  1:开始游戏      *******\n");
	printf("*******  0:退出游戏      *******\n");
	printf("*************************\n");

}

2、菜单函数

cpp 复制代码
void game()
{
	char mine[ROWS][COLS] = { 0 };//存雷
	char show[ROWS][COLS] = { 0 };//存放排查出的雷的信息

	//初始化棋盘   尽量设计成一样的
	InitBoard(mine,ROWS, COLS,'0');
    
	InitBoard(show,ROWS, COLS,'*');//调试时打印该表,玩的时候可以不打印

	//打印棋盘
	DisplayBoard(show, ROW, COL);

	//布置雷
	SetMine(mine, ROW, COL);
	DisplayBoard(mine, ROW, COL);//棋盘雷信息
	//排查雷
	FindMine(mine, show, ROW, COL);


}

3、游戏模块

头文件参数封装

cpp 复制代码
#define ROW 9        //棋盘显示区域
#define COL 9

#define ROWS ROW + 2//棋盘宽度
#define COLS COL + 2

#define EASY_COUNT 20//雷的数量


void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);

void DisplayBoard(char board[ROWS][COLS], int row, int col);//实际显示的棋盘区域

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

初始化棋盘 尽量设计成一样的

cpp 复制代码
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
	int i = 0;
	for (i = 0;i < rows;i++)
	{
		int j = 0;
		for (j = 0;j < cols;j++)
		{
			board[i][j] = set;
		}
	}
}

打印棋盘

cpp 复制代码
void DisplayBoard(char board[ROWS][COLS], int row, int col)//实际显示的棋盘区域
{
	int i = 0;
	for (i = 0;i <= col;i++)
	{
		printf("%d ", i);//打印行列
	}
	printf("\n");

	for (i = 1;i <= row;i++)
	{
		printf("%d ", i);
		int j = 0;
		for (j = 1;j <= col;j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}

}

布置雷

cpp 复制代码
//布置雷是在棋盘上随机的找10个坐标
//x范围1~9
//y范围1~9
void SetMine(char mine[ROWS][COLS], int row, int col)
{ 
	int count = EASY_COUNT;//80个雷
	int x = 0;
	int y = 0;
	srand((unsigned int)time(NULL));//随机数清空
	while (count)
	{
		x = rand() % row + 1;//防止出界
		y = rand() % col + 1;

		if (mine[x][y] != '1')
		{
			mine[x][y] = '1';//判断是否有雷
			count--;
		}
	}
}

排查雷

cpp 复制代码
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col)
{
	int x = 0;
	int y = 0;
	int win = 1;
	while (1)
	{
		printf("请输入要排查的坐标:");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)//是雷
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾,你踩雷了,游戏结束\n");
				DisplayBoard(mine, ROW, COL);
				break;
			}
			else
			{
				int count = GetMineCount(mine, x, y);
				show[x][y] = count + '0';
				DisplayBoard(show, ROW, COL);
			}
		}
		else
		{
			printf("输入的坐标有误x(1~9),y(1~9) 请重新输入");
		}
		//游戏胜利
		if (win == row * col - EASY_COUNT)
		{
			printf("恭喜你,排雷成功\n");
			break;
		}
	}
}
统计周围有几个雷

目录

1、主函数及游戏开始结束的界面设计

2、菜单函数

3、游戏模块

[初始化棋盘 尽量设计成一样的](#初始化棋盘 尽量设计成一样的)

打印棋盘

布置雷

排查雷

[法2 统计周围有几个雷](#法2 统计周围有几个雷)


cpp 复制代码
int GetMineCount(char mine[ROWS][COLS], char x, char y)
{
	int i = 0;
	int count = 0;
	for(i = -1;i <= 1;i++)
	{ 
		int j = 0;
		for (j = -1;j <= 1;j++)
		{
			count += mine[x + i][y + j] - '0';
		}
	}
	return count;
}
相关推荐
类球状12 分钟前
顺序表 —— OJ题
算法
Miraitowa_cheems36 分钟前
LeetCode算法日记 - Day 11: 寻找峰值、山脉数组的峰顶索引
java·算法·leetcode
CoovallyAIHub1 小时前
方案 | 动车底部零部件检测实时流水线检测算法改进
深度学习·算法·计算机视觉
CoovallyAIHub1 小时前
方案 | 光伏清洁机器人系统详细技术实施方案
深度学习·算法·计算机视觉
lxmyzzs1 小时前
【图像算法 - 14】精准识别路面墙体裂缝:基于YOLO12与OpenCV的实例分割智能检测实战(附完整代码)
人工智能·opencv·算法·计算机视觉·裂缝检测·yolo12
洋曼巴-young1 小时前
240. 搜索二维矩阵 II
数据结构·算法·矩阵
丑小鸭是白天鹅1 小时前
嵌入式C语言学习笔记之枚举、联合体
c语言·笔记·学习
GUET_一路向前2 小时前
【C语言防御性编程】if条件常量在前,变量在后
c语言·开发语言·if-else·防御性编程
楼田莉子2 小时前
C++算法题目分享:二叉搜索树相关的习题
数据结构·c++·学习·算法·leetcode·面试
pusue_the_sun3 小时前
数据结构——栈和队列oj练习
c语言·数据结构·算法··队列