C语言实现扫雷游戏

该文章展示了一个C语言实现的扫雷游戏代码,包括布置雷、排查雷的逻辑,以及游戏菜单和用户交互。代码中定义了初始化雷盘、打印棋盘、设置雷和查找雷的函数,并在主函数中实现了游戏循环。游戏使用9x9的雷盘,周围有额外的行用于边界处理。
摘要由CSDN通过智能技术生成

test.c源文件 - 扫雷游戏测试

game.h头文件 - 扫雷游戏函数的声明

game.c源文件 - 扫雷游戏函数的实现

1.布置雷  -- 存放雷的雷盘 9*9  数组设计成11*11 上下左右方各多一行,保证周围8的范围   雷 - 1    不是雷  -   0         

2.排查雷

 

主题测试源文件代码 :

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

// 雷 - 1    不是雷  -   0    
void menu()
{
	printf("****************************************\n");
	printf("*******        扫雷游戏        *********\n");
	printf("*******   author:小凡同学     *********\n");
	printf("*******        1.play          *********\n");
	printf("*******        0.exit          *********\n");
	printf("****************************************\n");
}

void game()
{
	char mine[ROWS][COLS];//存放布置好雷的信息
	char show[ROWS][COLS];//存放排查出雷的信息
	//两个数组坐标严格一一对应

	//初始化雷盘
	InitBoard(mine, ROWS, COLS,'0');//初始化为 全0
	InitBoard(show, ROWS, COLS,'*');//初始化为 全*

	//打印一下雷盘
	DisplayBoard(mine, ROW, COL);
	DisplayBoard(show, ROW, COL);

	//布置雷  使用的雷盘是9*9
	SetMine(mine, ROW, COL);
	//DisplayBoard(mine, ROW, COL);
	
	//排查雷
	FindMine(mine,show, ROW, COL);//对mine进行排查,排查结果放入show
}
int main()
{
	int input;
	srand((unsigned int)time(NULL));//使用rand()随机生成函数前提 #include<stdlib.h> #include<time.h>
	//(unsigned int)time(NULL)强制类型转换
	do 
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			/*printf("扫雷游戏\n");*/
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择有误,请重新选择\n");
			break;
		}
	} while (input);
	return 0;
}

封装声明头文件代码: 

#pragma once
#define ROW 9
#define COL 9

#define ROWS ROW+2
#define COLS COL+2

#define EASY_COUNT 10
//存放头文件
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

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

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

//布置雷  使用的雷盘是9*9
void SetMine(char mine[ROWS][COLS], int row, int col);

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

游戏函数功能实现源文件代码: 

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"   //自己编写的头文件用引号""引用

//初始化雷盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
	int i, j;
	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");
}

//布置雷  使用的雷盘是9*9
void SetMine(char mine[ROWS][COLS], int row, int col)
{
	//布置10个雷
	int count = EASY_COUNT;
	while (count)
	{
		//生成随机的下标 1-9 
		int x = rand() % row + 1; //使用rand()前需要调用srand()
		int y = rand() % col + 1;
		// 雷 - 1    不是雷  -   0    
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;//布置一个减少一个
		}

	}
}
//统计x,y坐标周围有几个雷

//static
1.修饰局部变量
//2.修饰全局变量
//3.修饰函数
static int get_mine_count(char mine[ROWS][COLS], int x, int y)  //加上static,让该函数只能在自己所在的源文件内部看到,其他源文件不可以用该函数
{
	return mine[x - 1][y] +
		mine[x - 1][y - 1] +
		mine[x][y - 1] +
		mine[x + 1][y - 1] +
		mine[x + 1][y] +
		mine[x + 1][y + 1] +
		mine[x][y + 1] +
		mine[x - 1][y + 1] - 8 * '0';//利用的ASCII码值计算
}


//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	//1.输入排查的坐标
	//2.检查坐标处是不是雷
	//(1)是雷 - 很遗憾炸死了 - 游戏结束
	//(2)不是雷 - 统计坐标周围有几个雷 - 存储排查雷的信息到show数组,游戏继续
	//
	int x=0,y=0,win=0;

	while(win <row*col-EASY_COUNT)
	{
		printf("请输入要排查的坐标:>");
		scanf("%d %d", &x, &y);
		//判断坐标的合法性 1-9
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾,你踩雷了!!!\n");
				DisplayBoard(mine, row, col);
				break;
			}
			else//排雷成功
			{
				//不是雷的情况下,统计x,y坐标周围有几个雷
				int count = get_mine_count(mine, x, y);
				show[x][y] = count+'0';//获得的是字符对应地ASCII值  -  %c 打印出数字字符 数字字符与ASCII对应关系
				//ASCII码
				/*
				0 '0'  -  48
				1 '1'  -  49
				*/
				//显示排查出来的信息
				DisplayBoard(show, row, col);
				win++;
			}
		}
		else
		{
			printf("坐标不合法,请重新输入\n");
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("恭喜你,排雷成功!!!\n");
		DisplayBoard(mine, row, col);
	}

}

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小凡同学zero

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值