C语言编写的扫雷游戏

本文介绍了使用C语言编写的扫雷游戏实现过程,包括随机生成雷的位置、排雷规则、游戏结局判断等功能。详细讲解了游戏的初始化、布局、扫雷逻辑,并提供了游戏运行时的两种状态:被炸死和排雷成功。代码分为game.h、game.c和main.c三个文件,附带注释以便理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

扫雷游戏要实现的功能
1.随机生成雷的位置
2.在排雷时如果被炸死显示棋盘中所有雷的分布
3.在排雷时如果没有被炸死显示该位置周围8个坐标的雷的个数
4.坐标周围没有雷时,可实现展开
5.游戏的结局分为被炸死和排掉所有的雷

扫雷游戏功能如何实现:
1.初始化玩家棋盘和开发者的棋盘(玩家棋盘玩家可见,开发者棋盘玩家不可见,玩家棋盘初始化为‘’,开发者棋盘初始化为‘0’,’'和’0‘均代表无雷)
在这里插入的图片描述通过设置一个参数set让玩家棋盘和开发者棋盘各自传入代表无雷的字符

在这里插入图片描述mine数组代表开发者棋盘,show数组代表玩家棋盘
-在这里插入图片描述
2.打印初始化好的玩家棋盘,开发者棋盘不打印
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
3.布置雷
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
4.扫雷
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
5.扫雷过程中用到的函数
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

6.游戏运行效果图:
在这里插入图片描述游戏进行中

在这里插入图片描述被炸死
在这里插入图片描述排雷成功

主要思路差不多就是这样,下面贴上三个文件的代码,都有注释的,对着看就可以了

game.h

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <time.h>
#include <stdlib.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 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);
//目标点无雷就向外扩展的函数的声明
void expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

//初始化棋盘函数的实现
void InitBoard(char board[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++)
		{
			board[i][j] = set;
		}
	}
}

//打印棋盘函数的实现
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("---------------------------------------\n");
	for (i = 0; i <= col; 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");
	}
	printf("---------------------------------------\n");
}

//布置雷的函数的实现
void SetMine(char board[ROWS][COLS], int row, int col)
{
	int count = easy_count;//布置10个雷,并且随机生成
	while (count)//每生成1个雷count就--
	{
		//1.生成随机雷的坐标
		int x = rand()%row+1;//1-9的横坐标
		int y = rand()%col+1;//1-9的纵坐标
		//2.布置雷
		if (board[x][y] == '0')//'0'代表不是雷
		{
			board[x][y] = '1';//‘1’代表是
			count--;
		}
	}
}

//扫雷的函数的实现:
//统计无雷的坐标附近雷的个数的函数
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
	return mine[x - 1][y - 1] +
		mine[x - 1][y] +
		mine[x - 1][y + 1] +
		mine[x][y - 1] +
		mine[x][y + 1] +
		mine[x + 1][y - 1] +
		mine[x + 1][y] +
		mine[x + 1][y + 1]-7*'0';
}

//目标坐标无雷就扩展的函数
void expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
	if (mine[x - 1][y - 1] == '0')//如果该位置没有雷,则计算出其周围8个坐标雷的个数并标记在show棋盘对应位置
	{
		show[x - 1][y - 1] = GetMineCount(mine, x - 1, y - 1);
	}
	if (mine[x - 1][y] == '0')
	{
		show[x - 1][y] = GetMineCount(mine, x - 1, y);
	}
	if (mine[x - 1][y + 1] == '0')
	{
		show[x - 1][y + 1] = GetMineCount(mine, x - 1, y + 1);
	}
	if (mine[x][y - 1] == '0')
	{
		show[x][y - 1] = GetMineCount(mine, x, y - 1);
	}
	if (mine[x][y + 1] == '0')
	{
		show[x][y + 1] = GetMineCount(mine, x, y + 1);
	}
	if (mine[x + 1][y - 1] == '0')
	{
		show[x + 1][y - 1] = GetMineCount(mine, x + 1, y - 1);
	}
	if (mine[x - 1][y] == '0')
	{
		show[x - 1][y] = GetMineCount(mine, x - 1, y);
	}
	if (mine[x - 1][y + 1] == '0')
	{
		show[x - 1][y + 1] = GetMineCount(mine, x - 1, y + 1);
	}
}


//扫雷的过程函数
void FindMine(char mine[ROWS][COLS],
	char show[ROWS][COLS],
	int row,
	int col)
{
	int x = 0;
	int y = 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)
		{
			//判断x,y坐标处是否是雷
			if (mine[x][y] == '1')
			{
				printf("抱歉,你被炸死了\n");
				DisplayBoard(mine, row, col);
				break;
			}
			else
			{
				//如果x,y坐标不是雷,就统计周围有几个雷
				int count = GetMineCount(mine, x, y);
				show[x][y] = count;//这里的count统计出来的是数字,而show棋盘里显示雷的个数的数字其实是个字符,数字转换成字符+‘0’即可
				expand(mine, show, x, y);
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("坐标输入错误,请重新输入\n");
		}
	}
	if (win == ROW*COL - easy_count)
	{
		printf("恭喜你,排雷成功!\n");
		DisplayBoard(mine, row, col);
	}
}

main.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
 
void menu()
{
	printf("*******************************\n");
	printf("*********1.开始游戏************\n");
	printf("*********2.退出游戏************\n");
	printf("*******************************\n");
}


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);//打印排查出来雷的函数
	//DisplayBoard(mine, ROW, COL);布置好的雷不能打印
	//1.布置雷
	SetMine(mine, ROW, COL);//布置雷的函数
	//DisplayBoard(mine, ROW, COL);//
	//2.扫雷
	FindMine(mine, show, ROW, COL);
}
int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));//设置随机生成雷的坐标
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();//扫雷游戏
			break;
		case 2:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,请重新选择\n");
			break;
		}
	} while (input);
	return 0;
}

不妥之处,请多多指教!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值