当前位置:网站首页>C language game - minesweeper
C language game - minesweeper
2022-08-01 09:28:00 【it_NunU】
文章目录
前言
“扫雷”I'm sure you guys have played it,In computer class, I secretly play with the computer in the computer room,Only minesweeper,Solitaire and other mini games,Grind and polish the time for class haha.
After studying for a while,Next, let’s talk about the mine-sweeping applet I made!(Additional area expansion effect~)
一、效果图(展示)
Let’s first show the effect diagram of the program running
打印菜单+初始化扫雷界面
Then the minesweeper game is played,输入排查坐标(If there is no lightning in the area near this coordinate(全为0)to expand a nearby slice)
Then continue to enter the coordinates,如果该坐标是雷,则游戏结束,You can choose to continue the game or choose to quit,Press before continuing the game 2 实现清屏,In order to prevent the previous game from interfering with the line of sight
All coordinates will be expanded after the game is over,View mine or non-mine areas
二、The overall structure and function introduction
To clear mine,First of all, there must be enough space to bury mines,When clearing mines, think about how to achieve if there are no mines in the eight squares around the square,The area will be cleared;踩到雷,即游戏结束.
So we sorted out our ideas and started to implement them one by one
1、创建雷盘
2、初始化雷盘
3、打印雷盘
4、Randomly place the number of mines you define
5、展开式排雷
The program file is still the same idea as that of Sanbangtest.c
;game.c
;game.h
With three pieces
需要思考的问题
Thoughts on this last little game of sanbang,Want to complete a big platform,We can use a two-dimensional array to store information about mine.
First of all, the computer needs to realize the random distribution of the positions of each mine、Location information is only visible to the developer and at the end of the game
三、Functional analysis and code implementation
创建雷盘、布置雷盘、初始化、打印
The top and leftmost numbers represent column and row coordinates,'0'
代表没有雷,'1'
It is thunder,'*'
Indicates that the coordinates of this point have not been mine-sweeped yet;
如果9X9 的平台,When the input coordinates are 3,1 时,It is found that there are three positions on the left that are beyond our definition 9X9 array-wide,那么如果解决这个问题呢?
我们可以让9X9The range of rows and columns are extended out one circle,此时9X9就变为 11X11的范围了,Then the outer grid can be initialized as 0Then the peripheral data is not displayed,In this way, the judgment of the follow-up investigation can be avoided.
void init_board(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 display_board(char board[ROWS][COLS], int row, int col)
{
int i = 0, 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 ", board[i][j]);
}
printf("\n");
}
printf("----------扫雷游戏----------\n");
}
void set_mine(char mine[ROWS][COLS], int row, int col)
{
// 布置10个雷
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
Then there's the minesweeper,Here I did not add the function of not being killed by the first mine clearance,This function can be used firstifDetermine whether the coordinates entered for the first time are mines,If it is a mine, you can find a non-mine location on the mine plate and move the mine to another place,You guys can do it yourself~
Minesweeper function is implemented:玩家输入一个坐标,如果是雷,则游戏结束,如果不是雷,And the nearby area is free of mines,expand one,游戏继续,Until the next mine is discharged or all non-mine positions have been checked,则游戏结束
// 统计
int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
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');
}
// 递归实现展开
void Spread(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
int count_number = get_mine_count(mine, x, y);//排查该坐标附近雷的个数
if (count_number == 0)
{
show[x][y] = ' ';
if ((x - 1 > 0 && x - 1 < 10) && (y - 1 > 0 && y - 1 < 10) && show[x - 1][y - 1] == '*')
{
Spread(mine, show, x - 1, y - 1);
}
if ((x - 1 > 0 && x - 1 < 10) && (y > 0 && y < 10) && show[x - 1][y] == '*')
{
Spread(mine, show, x - 1, y);
}
if ((x - 1 > 0 && x - 1 < 10) && (y + 1 > 0 && y + 1 < 10) && show[x - 1][y + 1] == '*')
{
Spread(mine, show, x - 1, y + 1);
}
if ((x > 0 && x < 10) && (y - 1 > 0 && y - 1 < 10) && show[x][y - 1] == '*')
{
Spread(mine, show, x, y - 1);
}
if ((x > 0 && x < 10) && (y + 1 > 0 && y + 1 < 10) && show[x][y + 1] == '*')
{
Spread(mine, show, x, y + 1);
}
if ((x + 1 > 0 && x + 1 < 10) && (y - 1 > 0 && y - 1 < 10) && show[x + 1][y - 1] == '*')
{
Spread(mine, show, x + 1, y - 1);
}
if ((x + 1 > 0 && x + 1 < 10) && (y > 0 && y < 10) && show[x + 1][y] == '*')
{
Spread(mine, show, x + 1, y);
}
if ((x + 1 > 0 && x + 1 < 10) && (y + 1 > 0 && y + 1 < 10) && show[x + 1][y + 1] == '*')
{
Spread(mine, show, x + 1, y + 1);
}
}
else
{
show[x][y] = count_number + '0';
}
}
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
// 1. 输入排查的坐标
// 2. 检查坐标处是不是雷
// (1)是雷 - boom - game over
// (2)不是雷 - 统计坐标周围有几个雷 - 存储排查雷的信息到show数组,游戏继续
int x = 0;
int y = 0;
int win = 0;
while (win < row * col - EASY_COUNT)
{
printf("请输入要排查雷的坐标:");
scanf("%d %d", &x, &y);// x -- (1,9) y -- (1,9)
//判断坐标的合法性
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了!\n");
display_board(mine, ROW, COL);
break;
}
else
{
// 不是雷的情况下,统计x,y坐标周围有几个雷
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
Spread(mine, show, x, y);
//显示排查出的信息
display_board(show, ROW, COL);
win++;
}
}
else
{
printf("该坐标已排查过\n");
}
}
else
{
printf("坐标输入有误,请重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功!\n");
display_board(show, ROW, COL);
}
}
四、完整源码展示
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf(" -------------------\n");
printf("| 1.play |\n");
printf("| 2.clear |\n");
printf("| 0.exit |\n");
printf(" -------------------\n");
}
void game()
{
// 设计2个数组存放信息
char mine[ROWS][COLS] = {
0 };
char show[ROWS][COLS] = {
0 };
// 初始化棋盘
// mine初始化为全'0'
// show初始化为全'*'
init_board(mine, ROWS, COLS, '0');
init_board(show, ROWS, COLS, '*');
// 打印棋盘
//display_board(mine, ROW, COL);
//display_board(show, ROW, COL);
// 布置雷
set_mine(mine,ROW,COL);
// 排雷
//display_board(mine, ROW, COL);
display_board(show, ROW, COL);
find_mine(mine, show, ROW, COL);
}
int main()
{
srand((unsigned)time(NULL));
int input = 0;
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 2:
system("cls"); // 清屏
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择\n");
break;
}
} while (input);
return 0;
}
game.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable : 6031)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
// Default mine
#define EASY_COUNT 20
// 函数声明
// 初始化
void init_board(char board[ROWS][COLS], int rows, int cols,char set);
// 打印棋盘
void display_board(char board[ROWS][COLS], int row, int col);
// 布置雷
void set_mine(char mine[ROWS][COLS], int row, int col);
// 排查雷
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
// 展开
void Spread(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void init_board(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 display_board(char board[ROWS][COLS], int row, int col)
{
int i = 0, 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 ", board[i][j]);
}
printf("\n");
}
printf("----------扫雷游戏----------\n");
}
void set_mine(char mine[ROWS][COLS], int row, int col)
{
// 布置10个雷
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
// 统计
int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
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');
}
// 递归实现展开
void Spread(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
int count_number = get_mine_count(mine, x, y);//排查该坐标附近雷的个数
if (count_number == 0)
{
show[x][y] = ' ';
if ((x - 1 > 0 && x - 1 < 10) && (y - 1 > 0 && y - 1 < 10) && show[x - 1][y - 1] == '*')
{
Spread(mine, show, x - 1, y - 1);
}
if ((x - 1 > 0 && x - 1 < 10) && (y > 0 && y < 10) && show[x - 1][y] == '*')
{
Spread(mine, show, x - 1, y);
}
if ((x - 1 > 0 && x - 1 < 10) && (y + 1 > 0 && y + 1 < 10) && show[x - 1][y + 1] == '*')
{
Spread(mine, show, x - 1, y + 1);
}
if ((x > 0 && x < 10) && (y - 1 > 0 && y - 1 < 10) && show[x][y - 1] == '*')
{
Spread(mine, show, x, y - 1);
}
if ((x > 0 && x < 10) && (y + 1 > 0 && y + 1 < 10) && show[x][y + 1] == '*')
{
Spread(mine, show, x, y + 1);
}
if ((x + 1 > 0 && x + 1 < 10) && (y - 1 > 0 && y - 1 < 10) && show[x + 1][y - 1] == '*')
{
Spread(mine, show, x + 1, y - 1);
}
if ((x + 1 > 0 && x + 1 < 10) && (y > 0 && y < 10) && show[x + 1][y] == '*')
{
Spread(mine, show, x + 1, y);
}
if ((x + 1 > 0 && x + 1 < 10) && (y + 1 > 0 && y + 1 < 10) && show[x + 1][y + 1] == '*')
{
Spread(mine, show, x + 1, y + 1);
}
}
else
{
show[x][y] = count_number + '0';
}
}
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
// 1. 输入排查的坐标
// 2. 检查坐标处是不是雷
// (1)是雷 - boom - game over
// (2)不是雷 - 统计坐标周围有几个雷 - 存储排查雷的信息到show数组,游戏继续
int x = 0;
int y = 0;
int win = 0;
while (win < row * col - EASY_COUNT)
{
printf("请输入要排查雷的坐标:");
scanf("%d %d", &x, &y);// x -- (1,9) y -- (1,9)
//判断坐标的合法性
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了!\n");
display_board(mine, ROW, COL);
break;
}
else
{
// 不是雷的情况下,统计x,y坐标周围有几个雷
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
Spread(mine, show, x, y);
//显示排查出的信息
display_board(show, ROW, COL);
win++;
}
}
else
{
printf("该坐标已排查过\n");
}
}
else
{
printf("坐标输入有误,请重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功!\n");
display_board(show, ROW, COL);
}
}
总结
以上就是CThe language implements the analysis of the Minesweeper game,There are also some functions that are expected to be completed by friends!
Hope this article can be helpful to you guys!留个赞再走吧~
(如有错误,望指出)
边栏推荐
- pytest interface automation testing framework | parametrize source code analysis
- 朴素贝叶斯--学习笔记--基本原理及代码实现
- sql server, FULL模式, dbcc shrinkfile(2,1) 不能收缩事务日志,还是原来的大小,是为什么?
- SkiaSharp 之 WPF 自绘 五环弹动球(案例版)
- HoloView -- Tabular Datasets
- 笔记。。。。
- 获取页面数据的方法
- Intensive reading of ACmix papers, and analysis of its model structure
- leetcode-6133:分组的最大数量
- leetcode-6135:图中的最长环
猜你喜欢
【STM32】入门(二):跑马灯-GPIO端口输出控制
JVM 运行时数据区与JMM 内存模型详解
leetcode-6133: maximum number of groupings
报告:想学AI的学生数量已涨200%,老师都不够用了
Microsoft Azure & NVIDIA IoT 开发者季 I|Azure IoT & NVIDIA Jetson 开发基础
[Beyond programming] When the fig leaf is lifted, when people begin to accept everything
获取页面数据的方法
【应用推荐】常见资源管理器整理,含个人使用体验和产品选型推荐
WLAN networking experiment of AC and thin AP
Redis middleware (from building to refuse pit)
随机推荐
高级驾驶辅助系统ADAS简介
将Servlet项目改为SSM项目
将aof文件转换为命令waoffle安装和使用
rpm和yum
静态Pod、Pod创建流程、容器资源限制
Ogg synchronizes oracle to mysql, there may be characters that need to be escaped in the field, how to configure escape?
Shell:条件测试操作
网络基础学习
指针的介绍及应用
用OpenCV的边缘检测
leetcode-6132: Make all elements in array equal to zero
Delphi MDI appliction documents maximize display, remove buttons such as maximize and minimize
Prime Ring Problem
Graduation thesis writing skills
Opencv creates a window - cv.namedWindow()
Pod环境变量和initContainer
sqlserver怎么查询一张表中同人员的交叉日期
Idea common plugins
Redis middleware (from building to refuse pit)
Parsing MySQL Databases: "SQL Optimization" vs. "Index Optimization"