当前位置:网站首页>C语言实现扫雷游戏
C语言实现扫雷游戏
2022-08-02 00:13:00 【7昂7.】
文章目录
一、游戏介绍
扫雷游戏是在一个指定的二维空间里,随机布置雷,把不是雷的位置都找出来,在你点一个位置的时候它会显示它周围全部雷的个数,根据这个线索去找 ,会更容易赢。
二、实现模块
文件名 作用
clear_mine.h 三子棋的函数声明,头文件声明等
clear_mine.c 三子棋函数接口的实现
main.c 三子棋函数测试功能
三、实现原理
1、用两个2维数组保存扫雷信息,一个用来设置雷,另一个用来展示看不见的雷,并把周围的雷的个数统计出来展示。
2、也就是当在排查雷输入坐标的时候要使用这个两个数组,这个坐标位置在设置雷的数组里面如果不是雷,就统计它周围雷个数放在另一个数组里面进行显示。直到我们排查排查 ROW乘COL-雷个数次,即可胜利
3、这里面最重要的怎样在方格位置上是显示个数,用字符是最方便的,不是雷的初始化为字符0,是雷放字符置为1。当一个位置上不是雷把他周围8个的字符加起来-去8*字符0就是有多少个雷的个数。再把雷的个数转化为字符放在可视化数字里面显示,只需要个数加上字符0即可。
四、实现逻辑
(一)、创建menu菜单函数
在这里插入代码片void menu()
{
printf("########### 1、游戏开始 ##########\n");
printf("########### 2、退出 ##############\n");
}
(二)、用switch语句去创建game游戏开始函数和退出功能
void game()
{
//用2个棋盘即两个2维数组,一个进行放雷另一个进行展示棋盘信息
char mine[ROWS][COLS] = {
0 };
char show[ROWS][COLS] = {
0 };
Init(mine,ROWS,COLS,'0');//先把放雷的数组初始化为字符0
Init(show,ROWS, COLS, '*');//再把棋盘信息初始化为字符*
set_mine(mine, ROW, COL );//放置雷
show1(show, ROW, COL);//展示棋盘信息
clear_mine(mine, show, ROW, COL);//排查雷
}
int main()
{
int insert = 0;
srand((unsigned int)time(NULL));
do{
menu();
printf("请选择:");
scanf("%d", &insert);
switch (insert)
{
case 1:
game();
break;
case 2:
printf("退出游戏");
break;
default:
printf("请重新输入");
break;
}
} while (insert);
}
(三)、在void game()函数里面创建上层调用框架
(1)、初始化棋盘并可视化棋盘
1、创建两个数组一个mine数组,一个是show数组并初始化,把它们分别置为字符0和字符*。然后创建一个打印棋盘函数以便呈现我们眼前。我们先调用Init和show1函数来看一下打印效果。
2、需要注意的是我们用二维数组打印两个棋盘,我们就是要打印9x9的棋盘?答案不是,因为我们在设计算法时需要统计坐标周围8个方位的个数,假如要统计边界周围雷的个数,那么就会有数组越界的问题,那么我们就需要在9×9的基础上加2,这些元素我们不打印,心里有数就行。
void Init(char a[][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++)
{
a[i][j] = set;
}
}
}
void show1(char a[][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (j = 0; j <col+1; j++)
{
printf("%d ", j);
}
printf("\n");
for (i = 1; i < row+1; i++)
{
printf("%d ", i);
for (j= 1; j <col+1; j++)
{
printf("%c ", a[i][j]);
}
printf("\n");
}
;
}
(2)、设置雷
这一步我门要去设置雷,就在刚刚我们初始化的mine数组里面去放随机雷,这个雷我们可以指定大小 我们定义宏,以便可以方便改。当然我们要现在测试函数main里面设置一个随机数生成器。然后再set_mine函数设置rand函数生成随机坐标。我们雷置为字符1,以便我更好计算。如图代码:
void set_mine(char mine[][COLS], int row, int col)
{
int i = 0;
int j = 0;
int x = 0;
int y = 0;
int count = mine_count;//雷的个数
while (count)
{
x = rand() % row + 1;//生成一到0的数
y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
(3)、展示棋盘信息
展示棋盘信息是刚才我们初始化的show函数里面 放的都是字符*,在这里就调用一次来开始玩游戏,进行第一次扫雷。
如图效果:
(4)、排查雷
1、这一步是游戏的核心,进行排查雷,要输入坐标就要先考虑它的合法性。
2、要要考虑他是否被排查过 也就是去重性。
3、在进行判断这个坐标在mine数组里面是否是字符1,如果是游戏结束,反之则把他周围的雷数统计出来给show数组,再调用show1来显示。
4、最一步判断排雷是否成功,只要排了ROW*COL-雷个数次,说明排雷成功。
void clear_mine(char mine[][COLS], char show[][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = 0;
while (count = row*col - mine_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')
{
int count1 = 0;
count1 = round_mineCount(mine, x, y);
show[x][y] = count1 + '0';
show1(show, row, col);
count--;
}
else
{
printf("踩到雷,游戏结束\n");
show1(mine, row, col);
break;
}
}
else
{
printf("已经被排查\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
if (count == 0)
{
printf("排雷成功");
show1(mine, row, col);
}
}
}
//t统计雷的个数,八个方向
int round_mineCount(char mine[][COLS],int x,int y)
{
return ( mine[x - 1][y] + mine[x + 1][y] +
mine[x][y - 1] + mine[x][y + 1] +
mine[x - 1][y - 1] + mine[x - 1][y + 1]
+ mine[x + 1][y - 1] + mine[x + 1][y + 1]-8*'0');
}
五、全部代码
main.c
在这里插入代码片#include"clear_mine.h"
void menu()
{
printf("########### 1、游戏开始 ##########\n");
printf("########### 2、退出 ##############\n");
}
void game()
{
//用2个棋盘即两个2维数组,一个进行放雷另一个进行展示棋盘信息
char mine[ROWS][COLS] = {
0 };
char show[ROWS][COLS] = {
0 };
Init(mine,ROWS,COLS,'0');//先把放雷的数组初始化为字符0
Init(show,ROWS, COLS, '*');//再把棋盘信息初始化为字符*
set_mine(mine, ROW, COL );//放置雷
show1(show, ROW, COL);//展示棋盘信息
clear_mine(mine, show, ROW, COL);//排查雷
}
int main()
{
int insert = 0;
srand((unsigned int)time(NULL));
do{
menu();
printf("请选择:");
scanf("%d", &insert);
switch (insert)
{
case 1:
game();
break;
case 2:
printf("退出游戏");
break;
default:
printf("请重新输入");
break;
}
} while (insert);
}
clear_mine.h
在这里插入代码片#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define mine_count 10//设置雷的个数
#define count2 row*col-Count
void Init(char a[][COLS], int rows,int cols, char set);//初始化棋盘
void set_mine(char mine[][COLS], int row, int col);//设置雷
void show1(char a[][COLS], int row, int col);//展示棋盘信息
void clear_mine(char mine[][COLS], char show[][COLS], int row, int col);// 排查雷
int round_mineCount(char mine[][COLS], int row, int col);//统计一个位置周围的雷的个数
clear_mine.c
#include"clear_mine.h"
void Init(char a[][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++)
{
a[i][j] = set;
}
}
}
void show1(char a[][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (j = 0; j <col+1; j++)
{
printf("%d ", j);
}
printf("\n");
for (i = 1; i < row+1; i++)
{
printf("%d ", i);
for (j= 1; j <col+1; j++)
{
printf("%c ", a[i][j]);
}
printf("\n");
}
;
}
void set_mine(char mine[][COLS], int row, int col)
{
int i = 0;
int j = 0;
int x = 0;
int y = 0;
int count = mine_count;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
void clear_mine(char mine[][COLS], char show[][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = 0;
while (count = row*col - mine_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')
{
int count1 = 0;
count1 = round_mineCount(mine, x, y);
show[x][y] = count1 + '0';
show1(show, row, col);
count--;
}
else
{
printf("踩到雷,游戏结束\n");
show1(mine, row, col);
break;
}
}
else
{
printf("已经被排查\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
if (count == 0)
{
printf("排雷成功");
show1(mine, row, col);
}
}
}
int round_mineCount(char mine[][COLS],int x,int y)
{
return ( mine[x - 1][y] + mine[x + 1][y] +
mine[x][y - 1] + mine[x][y + 1] +
mine[x - 1][y - 1] + mine[x - 1][y + 1]
+ mine[x + 1][y - 1] + mine[x + 1][y + 1]-8*'0');
}
六、运行结果
边栏推荐
- An interesting project--Folder comparison tool (1)
- uni-app project summary
- els block deformation judgment.
- TCL: Pin Constraints Using the tcl Scripting Language in Quartus
- What is the function of the JSP Taglib directive?
- 07-SDRAM: FIFO control module
- els strip deformation
- CRS management and maintenance
- Short video SEO search operation customer acquisition system function introduction
- Double queue implementation stack?Dual stack implementation queue?
猜你喜欢
Redis-消息发布订阅
什么是低代码(Low-Code)?低代码适用于哪些场景?
Knowing the inorder traversal of the array and the preorder traversal of the array, return the postorder history array
微软电脑管家V2.1公测版正式发布
Graphical LeetCode - 1161. Maximum Sum of In-Layer Elements (Difficulty: Moderate)
测试点等同于测试用例吗
uni-app project summary
Are test points the same as test cases?
An interesting project--Folder comparison tool (1)
短视频SEO搜索运营获客系统功能介绍
随机推荐
uni-app项目总结
The Statement update Statement execution
ICML 2022 || 局部增强图神经网络GNN,在 GCN 和 GAT基础上 平均提高了 3.4% 和 1.6%
String splitting function strtok exercise
els 方块边界变形处理
22. The support vector machine (SVM), gaussian kernel function
不要用jOOQ串联字符串
JSP如何使用request获取当前访问者的真实IP呢?
Redis的集群模式
What is the function of the JSP Taglib directive?
微软电脑管家V2.1公测版正式发布
go语言标准库fmt包怎么使用
ROS dynamic parameters
nodeJs--mime module
MYSQL(基本篇)——一篇文章带你走进MYSQL的奇妙世界
已知中序遍历数组和先序遍历数组,返回后序遗历数组
Play NFT summer: this collection of tools is worth collecting
工业信息物理系统攻击检测增强模型
MLX90640 红外热成像仪测温传感器模块开发笔记(十) 成果展示-红眼睛相机
Industrial control network intrusion detection based on automatic optimization of hyperparameters