当前位置:网站首页>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!
边栏推荐
- Exceptions and exception handling mechanisms
- Only lazy and hungry. You still don't understand the singleton pattern!
- 云原生-VMware虚拟机安装Kubesphere实战(一)
- networkmanager无法打开
- C#-委托的详细用法
- Difference Between Image Recognition and Semantic Segmentation
- 【考虫 六级英语】语法课笔记
- 数据分析面试手册《SQL篇》
- 二维数组实战项目--------《扫雷游戏》
- Pico 4更多参数曝光:Pancake+彩色透视,还有Pro版本
猜你喜欢
I caught a 10-year-old Ali test developer, and after talking about it, I made a lot of money...
Which foreign language journals and conferences can be submitted for software engineering/system software/programming language?
百战RHCE(第四十八战:运维工程师必会技-Ansible学习3-构建Ansible清单)
Lens filter---about day and night dual-pass filter
【爬虫】scrapy创建运行爬虫、解析页面(嵌套url)、自定义中间件(设置UserAgent和代理IP)、自定义管道(保存到mysql)
分布式.性能优化
还在用 Xshell?你 out 了,推荐一个更现代的终端连接工具,好用到爆!
"NIO Cup" 2022 Nioke Summer Multi-School Training Camp 2 DGHJKL Problem Solution
91.(cesium之家)cesium火箭发射模拟
[数据可视化] 图表设计原则
随机推荐
【爬虫】scrapy创建运行爬虫、解析页面(嵌套url)、自定义中间件(设置UserAgent和代理IP)、自定义管道(保存到mysql)
Mysql. Slow Sql
【js】获取当前时间的前后n天或前后n个月(时分秒年月日都可)
小程序onPageNotFound的坑
数据分析面试手册《SQL篇》
[Excel知识技能] 将数值格式数字转换为文本格式
input输入框超出部分用省略号表示以及判断内容是否有超出(PC端)
electron -autoUpdater 更新
C# JObject解析JSON数据
ArcGIS Pro 创建tpk
WebView2 通过 PuppeteerSharp 实现RPA获取壁纸 (案例版)
J9 Digital Theory: DAO governance is more like an ecological process: governance is native to the network and continues to evolve
【pypdf2】合并PDF、旋转、缩放、裁剪、加密解密、添加水印
复制带随机指针的链表——LeetCode
成功解决raise TypeError(‘Unexpected feature_names type‘)TypeError: Unexpected feature_names type
2022.8.10-----leetcode.640
Why do programming languages have the concept of variable types?
学习Apache ShardingSphere解析器源码(一)
线上突然查询变慢怎么核查
百战RHCE(第四十八战:运维工程师必会技-Ansible学习3-构建Ansible清单)