当前位置:网站首页>C language to achieve mine sweeping game
C language to achieve mine sweeping game
2022-07-24 07:29:00 【Wang Honghua x】
One 、 The game logic
Before we start , We need to clarify the logic of the game .
Scene arrangement
The game first gives us a square matrix , When we click on one of the grids , This grid will show how many mines are around , We can use a two-dimensional array to store mine information , use 0 It doesn't mean ray ,1 It means ray . such , When we click on a grid , We only detect its surroundings 1 The number of , You can show how many mines it has , But then we will encounter 2 A question
1、 Where is the information of the detected mine , For example, a mine was detected , If stored in the two-dimensional array of Mines , When clicking on the grid adjacent to this grid , This is for storage 1 When I couldn't tell the end, there was a thunder , Or is there a thunder around it .
So we need 2 Two dimensional arrays of the same size , A message for storing thunder , A store of information that users know .


2、 When we click on the side grid , Obviously we can't detect its surroundings 8 Lattice , Because it will cross the border , By this time there are 2 Kind of way , firstly , We can set the detection range , second , We can expand the array by one circle . Here we take the second approach .

With a chessboard , We can initialize them , After initialization, use random number generator to arrange Lei , The stage is basically arranged .
Demining
Next, we start demining , First, we need players to give us a coordinate , To check thunder , When checking Lei, let's first see if this position is Lei , If so, the game is over , If not, it shows that there are several mines around ;
The specific code implementation and some details are put below ,game.h Stored in is the function declaration and macro ,test.c It is the logical implementation of the main program ,game.c Is the specific implementation of the game function .
Two 、 Complete code
game.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS COL + 2
#define EASY 10
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void InitializeBoard(char board[ROWS][COLS], int row, int col, char ch);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char board[ROWS][COLS], int row, int col, int count);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
void GetMineCount(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int row, int col, int *pwin);test.c
#include "game.h"
void menu()
{
printf("---------------------------\n");
printf("-----1.play 0.exit-----\n");
printf("---------------------------\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
InitializeBoard(mine, ROWS, COLS, '0');
InitializeBoard(show, ROWS, COLS, '*');
SetMine(mine, ROW, COL, EASY);
DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf(" Please select :");
scanf("%d", &input);
switch (input)
{
case 1:game();
break;
case 0:printf(" Quit the game !\n");
break;
default:printf(" Input error !\n");
break;
}
} while (input);
return 0;
}game.c
#include "game.h"
void InitializeBoard(char board[ROWS][COLS], int row, int col, char ch)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ch;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("----- The Minesweeper game -----\n");
for (i = 0; i <= col; i++)
printf("%d ", i);
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
if (board[i][j] == '0')
printf(" ");
else
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("----- The Minesweeper game -----\n");
}
void SetMine(char board[ROWS][COLS], int row, int col, int count)
{
int i = 0;
int j = 0;
while (count)
{
i = rand() % row + 1;
j = rand() % col + 1;
if (board[i][j] == '0')
{
board[i][j] = '1';
count--;
}
}
}
void GetMineCount(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int row, int col, int *pwin)
{
if (show[x][y] == '*')
{
int i, j, count = 0;
for (i = -1; i <= 1; i++)
{
for (j = -1; j <= 1; j++)
{
if (mine[x + i][y + j] == '1')
count++;
}
}
show[x][y] = count + '0';
(*pwin)++;
if (show[x][y] == '0')
{
for (i = -1; i <= 1; i++)
{
for (j = -1; j <= 1; j++)
{
if (x + i >= 1 && x + i <= row && y + j >= 1 && y + j <= col)
GetMineCount(mine, show, x + i, y + j, row, col, pwin);
}
}
}
}
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x, y;
int win = 0;
while (win < row * col - EASY)
{
printf(" Please enter the coordinates of the mine :");
scanf("%d%d", &x, &y);
system("cls");
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf(" The coordinates you entered are (%d,%d), unfortunately , You're killed in the blast !\n", x, y);
int i, j;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (mine[i][j] == '1')
{
show[i][j] = '!';
}
}
}
DisplayBoard(show, row, col);
break;
}
else
{
if (show[x][y] != '*')
{
printf(" This coordinate has been checked !\n");
}
else
{
GetMineCount(mine, show, x, y, row, col, &win);
DisplayBoard(show, row, col);
}
}
}
else
{
printf(" Illegal coordinate input !\n");
}
if (win == row * col - EASY)
{
printf(" congratulations , Mine clearance is successful !\n");
DisplayBoard(mine, row, col);
}
}
}边栏推荐
- Network security B module windows operating system penetration test of national vocational college skills competition
- File "manage.py", line 14) from exc ^ syntaxerror: cause and solution of invalid syntax error
- Vulnhub DC1
- Jenkins 详细部署
- 深度学习二三事-回顾那些经典卷积神经网络
- Development system selection route
- Wild pointer, null pointer, invalid pointer
- 我的创作纪念日
- JS_实现多行文本根据换行分隔成数组
- Raspberry pie change source
猜你喜欢

Decompress the anchor and enjoy 4000w+ playback, adding a new wind to the Kwai food track?

Influxdb unauthorized access & CouchDB permission bypass
![[steering wheel] the super favorite idea efficiency artifact save actions is uninstalled](/img/b0/54a826287154be5b758b3850e7fa51.png)
[steering wheel] the super favorite idea efficiency artifact save actions is uninstalled

Part II - C language improvement_ 4. Secondary pointer

服务漏洞&FTP&RDP&SSH&rsync

JS_ Realize the separation of multiple lines of text into an array according to the newline

Jenkins detailed deployment

Cloud version upgrade

无法自动装配,未找到“RedisTemplate类型的Bean

QoS服务质量四QoS边界行为之流量监管
随机推荐
23.组件自定义事件
mysql查询当前节点的所有父级
Basic syntax of MySQL DDL and DML and DQL
Deep learning two or three things - review those classical convolutional neural networks
开发系统选择路线
Win10 sound icon has no sound
25. Message subscription and publishing - PubSub JS
Customization or GM, what is the future development trend of SaaS in China?
25.消息订阅与发布——PubSub-js
全国职业院校技能大赛网络安全B模块 缓冲区溢出漏洞
【云原生】MySql索引分析及查询优化
win10声音图标有个没有声音
PHP escape string
【信息系统项目管理师】第七章 复盘成本管理知识架构
Jackson parsing JSON detailed tutorial
Notes on the basics of using parameters in libsvm (1)
Buffer overflow vulnerability of network security module B in national vocational college skills competition
csdn,是时候说再见!
PHP link log scheme
File "manage.py", line 14) from exc ^ syntaxerror: cause and solution of invalid syntax error