[C语言]扫雷小游戏的实现

今天我要来实现一个扫雷小游戏, 扫雷小游戏, 想要实现的类似游戏效果是这样的:

扫雷游戏网页版 - Minesweeper 初级版本9*9类型

具体思路如下:

1.创建三个文件及用途:

game.h->存放游戏函数的函数声明, 以及函数的具体用途

game.c->存放游戏函数的实现

test.c->存放扫雷的主函数, 用于测试.

2.是雷->1, 不是雷->0, 但这样会出现一个问题, 当雷的信息和排查雷的信息都为1时怎么表示?

---->解决办法: 创建两个数组, 一个放布置雷的信息mine[ ], 一个放排查雷的信息show[ ].

3.棋盘设计: 想要实现9*9的棋盘, 数组大小设置为11*11的比较:

好处是, 对于放排查雷的信息show[]数组, 可以有一个坐标系方便定位(如图)

             对于放布置雷的信息mine[]数组, 把最外围设置雷的个数为0, 更方便计算雷的个数.

 数组最终效果如下:

以下是我的代码, 编写代码时我把注释也写在旁边, 方便大家阅读

test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"
//准备两个表格, 一个放排查出雷的信息char show[9][9], 一个放布置好雷的信息char mine[9][9]
//统计每个坐标周围的坐标时候容易越界, 所以要[11][11];

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

void game() {
	char mine[ROWS][COLS] = { 0 };//存放布置好雷的信息
	char show[ROWS][COLS] = { 0 };//存放排查出雷的信息
	//初始化数组的内容为指定的内容
	//mine数组在没有布置雷的时候, 都是'0';
	InitBoard(mine, ROWS, COLS, '0');
	//show数组在没有排查雷的时候,都是'*';
	InitBoard(show, ROWS, COLS,'*');
	//设置雷
	Setmine(mine, ROW, COL);
	//DisplayBoard(mine, ROW, COL);
	DisplayBoard(show, ROW, COL);
	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 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输入错误, 请重新输入\n");
			break;
		}
	} while (input);


	return 0;
}

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
//初始化11*11的棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) {
	for (int i = 0; i < rows; i++)
	{
		for (int 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 (int 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;
	while (count) { 
		//用到随机数的函数, 需要用srand()随机种子
		//但是只用到一次, 所以放到主函数里面就可以了
		int x = rand() % row + 1;
		int y = rand() % col + 1;

		if (board[x][y] == '0') {
			board[x][y] = '1';
			count--;
		}
	}
}

//查找x,y坐标周围有几个雷
int get_mine_count(char board[ROWS][COLS], int x, int y) {
	return board[x - 1][y] +
		board[x - 1][y - 1] +
		board[x][y - 1] +
		board[x + 1][y - 1] +
		board[x + 1][y] +
		board[x + 1][y + 1] +
		board[x][y+1]+
		board[x - 1][y + 1] - 8 * '0';
}


//查找雷的坐标
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) {
	int x = 0;
	int y = 0;
	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//如果不是雷, 则要统计周围8个坐标中有几个雷
			{
				//统计mine数组中x,y坐标周围有几个雷
				int count = get_mine_count(mine, x, y);
				show[x][y] = count + '0';//转换成数字3
				DisplayBoard(show,ROW,COL);


			}
		}
		else
		{
			printf("输入的坐标非法, 请重新输入");
		}
	}
}

game.h

#include<stdio.h>
#include<time.h>//用到时间的函数
#include<stdlib.h>//用到随机数的函数

#define ROW 9
#define COL 9
#define EASY_COUNT 10
//布置简单版本的十个雷

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

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

void DisplayBoard(char board[ROWS][COLS], int row, int col);
//注意这里: 展示的时候是9*9, 但是接受的时候是11*11;

void Setmine(char board[ROWS][COLS], int row, int col);

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值