扫雷游戏实现

目录

【前言】

1.游戏的功能说明

2.游戏的分析设计

3.扫雷游戏的代码实现

【前言】

本次我们将就前面所学知识编写一个小游戏:扫雷。下面是项目具体实施。

1.游戏的功能说明

扫雷游戏的功能主要包括以下几个方面:

游戏可以通过菜单实现继续玩或者退出游戏

  1. 扫雷的棋盘是9*9的格子
  2. 随机布置雷
  3. 可以排查雷
  4. 展示雷的坐标
  5. 位置不是雷,就显示周围有几个雷,并展开周围的一片
  6. 是雷,游戏结束

2.游戏的分析设计

扫雷游戏中,雷的布置以及以及排查雷的信息我们都需要存储,此时我们可以设计一个9*9的二维数组用来存储雷的信息以及排查雷的信息。

空格处如果布置雷则存放1,否则为0

当我们排查(3,2)这个坐标时,改位置不是雷,则需要将周围一圈八个位置雷的信息统计下来。

若排查(9,5)这个坐标时,这时候在统计周围八个坐标就会变为数组越界,所以我们需要将数组周围扩大一圈变成11*11的二维数组。

但雷的信息也是1,若周围统计的雷的个数也为1的话,就会导致含义混淆,所以我们可以将储存雷和非雷的换成字符。

但是这样做棋盘上有雷和非雷的信息,还有排查出的雷的个数信息,就比较混杂,不够方便。我们专门给⼀个棋盘(对应⼀个数组mine)存放布置好的雷的信息。给另外⼀个棋盘(对应另外⼀个数组show)存放排查出的雷的信息。这样就互不⼲扰了,把雷布置到 mine数组,在mine数组中排查雷,排查出的数据存放在show数组,并且打印show数组的信息给后期 排查参考。

我们可以设计三个文件

test.c //⽂件中写游戏的测试逻辑 
game.c //⽂件中写游戏中函数的实现等
game.h //⽂件中写游戏需要的数据类型和函数声明等

3.扫雷游戏的代码实现

test.c

#include "game.h"


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

}


void test()
{
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };

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

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

	//生成雷
	Setmine(mine, ROW, COL);
	PrintBoard(mine, ROW, COL);

	//排查雷
	Findmine(mine, show, ROW, COL);
}


int main()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		menu();
		printf("请输入:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			test();
			break;
		case 0:
			printf("退出游戏!\n");
			break;
		default:
			printf("输入有误,请重新输入!\n");
			break;
		}
	} while (input);
	return 0;
}

game.h

#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 arr[ROWS][COLS], int rows, int cols, char ret);

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

//生成雷
void Setmine(char arr[ROWS][COLS], int row, int col);

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

//展开雷
char Around(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);

game.c

#include "game.h"

//初始化棋盘
void InitBoard(char arr[ROWS][COLS], int rows, int cols,char ret)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			arr[i][j] = ret;
		}
	}
}

//打印棋盘
void PrintBoard(char arr[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("----------扫雷游戏-----------\n");
	for (j = 0; j <= col; j++)
	{
		printf("%d ", j);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", arr[i][j]);
		}
		printf("\n");
	}
}

//生成雷
void Setmine(char arr[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (arr[x][y] == '0')
		{
			arr[x][y] = '1';
			count--;
		}
	}
}


//排查雷
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)
		{
			if (show[x][y] == '*')
			{
				//该位置是雷
				if (mine[x][y] == '1')
				{
					printf("很遗憾,你被炸死了\n");
					PrintBoard(mine, ROW, COL);
					break;
				}
				else
				{
					//展开雷
					Around(mine, show, x, y);
					PrintBoard(show, ROW, COL);
					win++;
				}
			}
			else
			{
				printf("输入不合法,请重新输入\n");
			}
		}
		else
		{
			printf("输入不合法\n");
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("排雷成功\n");
		PrintBoard(mine, ROW, COL);
	}
}


//统计雷
int Getmine(char arr[ROWS][COLS], int x, int y)
{
	int i = 0;
	int j = 0;
	int count = 0;
	for (i = x - 1; i <= x + 1; i++)
	{
		for (j = y - 1; j <= y + 1; j++)
		{
			if (arr[i][j] == '1')
			{
				count++;
			}
		}
	}
	return count;
}

//展开雷
char Around(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
	int i = 0;
	int j = 0;
	if (x <= ROW && x >= 1 && y <= COL && y >= 1 && show[x][y] == '*' && mine[x][y] != '1')
	{
		int sum = Getmine(mine, x, y);
		if (sum == 0)//若周围无雷,则将该位置赋值为‘ ’
		{
			show[x][y] = ' ';
			for (i = (x - 1 > 1 ? x - 1 : 1); i <= (x + 1 > ROW ? ROW : x + 1); i++)
			{
				for (j = (y - 1 > 1 ? y - 1 : 1); j <= (y + 1 > COL ? COL : y + 1); j++)
				{
					Around(mine, show, i, j);
				}
			}
		}
		else//若有雷,则将该位置赋值为几,代表该点周围有几个雷
		{
			show[x][y] = sum + '0';
		}
	}
}

游戏试玩

这里为了具体看游戏有无问题,我们将雷的个数设计为80个

制作不易,有问题请指正!!!

  • 11
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ljhohhh

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

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

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

打赏作者

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

抵扣说明:

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

余额充值