当前位置:网站首页>Two-dimensional array combat project -------- "Minesweeper Game"
Two-dimensional array combat project -------- "Minesweeper Game"
2022-08-11 00:40:00 【Xu Fan】
Continue with the game of three-moku introduced in the previous issue,Today I will introduce to you a similar minesweeper game!(源码在文章末尾)
一 . 游戏开发框架
- 建立游戏菜单
- Build a minesweeper chessboard
- 初始化棋盘
- 布置雷
- 排雷(判断是否踩雷)
- 游戏结束
二 . Game development and details
1.建立游戏菜单
首先,我们需要创建一个项目,Add a header file and two source files

![]()
test.c------------------------Main frame design for games and logic testing of minesweeper games
game.h----------------------for the declaration of the corresponding function
game.c----------------------用于游戏函数的实现
A print implementation of the game menu,The three-bang game shared in the last issue has been declared,Just apply the previous code directly,代码块如下:
void menu() {
printf("-------------------------\n");
printf("---------1.paly----------\n");
printf("---------0.exit----------\n");
printf("-------------------------\n");
}
void game() {
printf("开始游戏\n");
}
int main() {
int input = 0;
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;
}
实现如下:

到这里,The game menu printing part of the preparation is complete.
2.Build a minesweeper chessboard
The rules of the game are not introduced here,参考下面的链接:https://jingyan.baidu.com/article/7f766daf9231e84101e1d03d.html

Printing on the chessboard,We need a two-dimensional array to define,But in order to prevent the array out of bounds problem,We appropriately expand the size of the array,It is used where there is thunder when defining the chessboard ‘ 1 ’表示,Use where there is no thunder ‘ 0 ’表示;
Next, we need to consider the details of mine clearance:

创建一个mine数组和一个show数组:
game.h
#pragma once
#include <stdio.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
test.c
//设计2个数组存放信息
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };Two arrays have been created,Next we want to initialize the board and print the board
3.初始化棋盘
mineFor array initialization ‘ 0 ’表示
showFor array initialization ‘ * ’表示
test.c
void game() {
//设计2个数组存放信息
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//初始化棋盘
init_board(mine, ROWS, COLS,'0');
init_board(show, ROWS, COLS,'*');
//打印棋盘
display_board(mine, ROW, COL);
display_board(show, ROW, COL);
}game.c
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;
int j = 0;
for (i = 1; i <= row; i++) {
for (j = 1; j <= col; j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
}game.h
//Initialize the chessboard
void init_board(char board[ROWS][COLS], int rows, int cols,char set);
//打印棋盘
void display_board(char board[ROWS][COLS], int row, int col);The print of the checkerboard is as follows:

由上图可以看到,Printed boards are not very player friendly,We can add row and column labels to the board,In order for the player to enter the coordinates when playing the game,只需改变game.c的代码即可:
void display_board(char board[ROWS][COLS], int row, int col) {
int i = 0;
int j = 0;
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");
}
}
实现如下:

4.布置雷
We need to consider two issues when laying out mines:
1.坐标的合法性
2.Whether the coordinates have been laid out for mines,Mine cannot be repeated
这里我们使用randfunction to realize the randomness of mine placement,关于randHow to use the function can refer to this linkhttps://cplusplus.com/reference/cstdlib/rand/?kw=rand
int x = rand() % row + 1;
int y = rand() % col + 1;x代表横坐标,y代表纵坐标,rand()%row的值在0~8,加1就是1~9,Exactly match the coordinates entered by the player.
test.c
void game() {
//设计2个数组存放信息
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//初始化棋盘
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);
}game.c
void set_mine(char mine[ROWS][COLS], int row, int col) {
//Suppose layout10个雷
int count = 10;
while (count) {
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0') {
mine[x][y] = '1';
count--;
}
}
}game.h
//布置雷
void set_mine(char board[ROWS][COLS], int row, int col);
运行如下:

到这里10A thunder was arranged by us
5.排雷
When rowed to a coordinate,We need to judge the surrounding of the coordinates8Whether there is thunder at a location,也就是字符‘ 1 ’,We need to define a function to add8个位置的字符‘ 1 ’,Add the red lines strung together,求出(x,y)周围雷的个数;
代码如下:
test.c
void game() {
//设计2个数组存放信息
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//初始化棋盘
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);
find_mine(mine, show, ROW, COL);
}game.c
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 find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) {
int x = 0;
int y = 0;
printf("请输入要排查雷的坐标:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col) {
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
display_board(show, ROW, COL);
}
else {
printf("坐标非法,重新输入:");
}
}
geme.h
//排查雷
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS] , int row, int col);
An addition to the above piece of code:

show[x][y] = count + '0';
Complement to this code,Why count the number of mines around+' 0 ' ? ? ?
We know that characters are stored in dataASCII存储的
由上图可知,字符-字符=一个数值,所以show[x][y](数字字符)=count(数值)+‘0’(数字字符);
到这里,Demining worksOK了,下面演示一下:

6.游戏结束(Judge whether to win or step on the thunder)
在9x9的棋盘中,Assume placement10个雷,只有2中情况,kind of put the rest71All coordinates are arranged,游戏胜利,One is stepping on thunder,游戏结束;
This time we just need to be theregame.cYou can add the conditions for judging winning or losing in the file:
void find_mine(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");
display_board(mine, ROW, COL);
break;
}
else
{
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
display_board(show, ROW, COL);
win++;
}
}
else
{
printf("该坐标已经被排查过了\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
display_board(mine, ROW, COL);
}
}在game.hdeclare a definition in
#define EASY_COUNT 10好了,到这里游戏的设计就完成了,让我们看下效果吧:

No more playing here,If you are interested, you can try it yourself
源代码:
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("-----------------------------------\n");
printf("----------------1. play------------\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()
{
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"
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;
int j = 0;
//列号
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");
}
}
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 find_mine(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");
display_board(mine, ROW, COL);
break;
}
else
{
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
display_board(show, ROW, COL);
win++;
}
}
else
{
printf("该坐标已经被排查过了\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
display_board(mine, ROW, COL);
}
}game.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define EASY_COUNT 10
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
//Initialize the chessboard
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 board[ROWS][COLS], int row, int col);
//排查雷
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS] , int row, int col);
三. 总结
for this little game,It is also very difficult for me to learn,It also took a lot of time to research,Figure out the details inside,最重要的是理解,Learn how to design a game idea,Programming is not something that can be done in a day or two,慢慢积累,温故而知新,will get the result we want,The editor is a student who has just come into contact with programming,If you have any comments or errors on the above,还请大佬们斧正,Children who find it helpful,Three Crabs!
边栏推荐
- J9数字论:DAO治理更像一种生态过程:治理原生于网络,不断演变
- Distributed. Performance optimization
- 分布式.性能优化
- How to check if the online query suddenly slows down
- Only lazy and hungry. You still don't understand the singleton pattern!
- Dump file generation, content, and analysis
- 微信小程序获取当前页面的url和参数
- input输入框超出部分用省略号表示以及判断内容是否有超出(PC端)
- How to do patent mining, the key is to find patent points, in fact, it is not too difficult
- [21 Days Learning Challenge] Half Insertion Sort
猜你喜欢

Qt入门(六)——抽奖系统的实现
![[Excel knowledge and skills] Convert numeric format numbers to text format](/img/fb/79d6928456f090d47f0fe7a5074979.png)
[Excel knowledge and skills] Convert numeric format numbers to text format

Mysql.慢Sql

ADC和DAC记录

"NIO Cup" 2022 Nioke Summer Multi-School Training Camp 3 DF Problem Solving

“蔚来杯“2022牛客暑期多校训练营2 DGHJKL题解

微信小程序自定义navigationBar

力扣------使用最小花费爬楼梯

Only lazy and hungry. You still don't understand the singleton pattern!

Single-chip human-computer interaction--matrix key
随机推荐
Analysis of LENS CRA and SENSOR CRA Matching Problems
报错:Client does not support authentication protocol requested by server; consider upgrading MySQL cli
鲲鹏编译调试及原生开发工具基础知识
成功解决TypeError: can‘t multiply sequence by non-int of type ‘float‘
More parameter exposure of Pico 4: Pancake + color perspective, and Pro version
22/8/9 Collection of Greedy Problems
详解JDBC的实现与优化(万字详解)
成功解决raise TypeError(‘Unexpected feature_names type‘)TypeError: Unexpected feature_names type
软件测试证书(1)—— 软件评测师
Shell 文本三剑客 Sed
从0开始设计JVM ,忘记名词跟上思路一次搞懂
【21天学习挑战赛】折半插入排序
Lens filter---about day and night dual-pass filter
Single-chip human-computer interaction--matrix key
Pico 4更多参数曝光:Pancake+彩色透视,还有Pro版本
dump_stack()
力扣------值相等的最小索引
Jvm. Profiling tools (jconsole, jvisualvm, arthas, jprofiler, mat)
关于科研学习中的几个问题:如何看论文?如何评价工作?如何找idea?
构建检测,无规矩不成方圆
