当前位置:网站首页>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.hWith 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!留个赞再走吧~
(如有错误,望指出)
边栏推荐
猜你喜欢
随机推荐
Leicester Weekly 304 6135. The longest ring in the picture Inward base ring tree
leetcode-6134:找到离给定两个节点最近的节点
HoloView -- Tabular Datasets
Shell:条件测试操作
杨辉三角(c语言实现)
ACmix 论文精读,并解析其模型结构
sql server, FULL mode, dbcc shrinkfile(2,1) can not shrink the transaction log, or the original size, why?
C语言中编译时出现警告C4013(C语言不加函数原型产生的潜在错误)
node 格式化时间的传统做法与高级做法(moment)
基于tika实现对文件类型进行判断
Change Servlet project to SSM project
GBase 8c中怎么查询数据库配置参数,例如datestyle
最新的Cesium和Three的整合方法(附完整代码)
实验。。。。
YOLOv7-Pose尝鲜,基于YOLOv7的关键点模型测评
179. 最大数
【数据集】各类绝缘子、鸟巢及防震锤数据集汇总
笔记。。。。
【无标题】
量化日常工作指标









