当前位置:网站首页>[minesweeping of two-dimensional array application] | [simple version] [detailed steps + code]
[minesweeping of two-dimensional array application] | [simple version] [detailed steps + code]
2022-07-03 05:38:00 【Sobtemesa】
Catalog
1. Create two checkerboard arrays , One to show , One is used to put thunder
2. Initialize and print the checkerboard array
5. Deal with being killed the first time
5. Calculate the number of surrounding mines
6. Expand the function to realize the blank Algorithm
7. Judge whether the minesweeping is successful
Preface
Usually, the project development needs to be prepared in the form of sub documents , Respectively :
- test.c As an entry point for code testing
- game.c The function function realization involved in the preparation of sanziqi
- game.h This header file is usually used to complete the function declaration 、 References to header files 、 Macro definition variables .
First complete the code :
One 、test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"game.h"
menu()
{
printf("****************************\n");
printf("*****1.play game ***********\n");
printf("*****0. exit ***********\n");
printf("****************************\n");
}
game()
{
char mineboard[ROWS][COLS] = { 0 };
char showboard[ROWS][COLS] = { 0 };
Initboard(mineboard, ROWS, COLS, '0');
Initboard(showboard, ROWS, COLS, '*');
Setmine(mineboard, ROWS, COLS);
Display(mineboard, ROWS, COLS);
Palyermove(mineboard, showboard, ROWS, COLS);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf(" Please enter :\n");
scanf("%d", &input);
switch(input)
{
case 1:
game();
break;
case 0:
printf(" Quit the game \n");
break;
}
} while (input);
return 0;
}Two 、game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void Initboard(char board[ROWS][COLS], int rows, int cols, char ch)
{
int i = 0;
for (i = 0; i < rows; i++)
{
for (int j = 0; j< cols; j++)
{
board[i][j] = ch;
}
}
}
void Display(char board[ROWS][COLS], int rows, int cols)
{
int i = 0;
printf("-------------------------------\n");
for (int j = 0; j <= COL; j++)
{
printf("%d ", j);
}
printf("\n");
for (i = 1; i <=ROW; i++)
{
printf("%d ", i);
int j = 0;
for (j = 1; j <= COL; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("-------------------------------\n");
}
void Setmine(char board[ROWS][COLS], int rows, int cols)
{
int x = 0;
int y = 0;
int count = EASY_COUNT;
while (count)
{
x = rand() % ROW + 1;
y = rand() % COL + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
void Setnewmine(char board[ROWS][COLS], int x, int y)
{
int a = 0;
int b = 0;
while (1)
{
a = rand() % ROW + 1;
b = rand() % COL + 1;
if (a != x&&b != y&&board[a][b] == '0')
{
board[a][b] = '1';
break;
}
}
}
Iswin(char showboard[ROWS][COLS], int row, int col)
{
int i = 0;
int count = 0;
for (i = 1; i <= row; i++)
{
int j = 0;
for (j = 1; j <= col; j++)
{
if (showboard[i][j] == "*")
{
count++;
}
}
}
return count;
}
void Palyermove(char mineboard[ROWS][COLS], char showboard[ROWS][COLS], int rows, int cols)
{
int x = 0;
int y = 0;
while (1)
{
int flag = 0;
printf(" Please enter your coordinates :");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= ROW&&y >= 1 && y <= COL)
{
if (mineboard[x][y] == '1')
{
flag = 1;
mineboard[x][y] = '0';
Setnewmine(mineboard, x,y);
openmine(mineboard, showboard[ROWS][COLS], x, y);
}
if (flag == 1 && mineboard[x][y] == '1')
{
printf(" I'm sorry you were killed \n");
Display(mineboard, ROWS, COLS);
break;
}
if (mineboard[x][y] == '0')
{
flag = 1;
openmine(mineboard, showboard,x,y);
Display(showboard, ROWS, COLS);
}
}
else
{
printf(" The coordinates you entered are illegal , Please re-enter \n");
}
if (Iswin(showboard, ROW, COL) == EASY_COUNT)
{
printf(" Congratulations on winning \n");
break;
}
}
}
int Getcount(char mineboard[ROWS][COLS], int x, int y)
{
return (mineboard[x - 1][y - 1] +
mineboard[x - 1][y] +
mineboard[x - 1][y + 1] +
mineboard[x][y - 1] +
mineboard[x][y + 1] +
mineboard[x + 1][y - 1] +
mineboard[x + 1][y] +
mineboard[x + 1][y + 1]) - 8 * '0';
}
void openmine(char mineboard[ROWS][COLS], char showboard[ROWS][COLS], int x, int y)
{
if (Getcount(mineboard, x, y) == 0)
{
showboard[x][y] = ' ';
if ((x - 1) > 0 && (y - 1) > 0 && (showboard[x - 1][y - 1] == '*'))
{
openmine(mineboard, showboard, x - 1, y - 1);
}
if ((x - 1) > 0 && (y ) > 0 && (showboard[x - 1][y] == '*'))
{
openmine(mineboard, showboard, x - 1, y );
}
if ((x - 1) > 0 && (y +1) > 0 && (showboard[x - 1][y+1] == '*'))
{
openmine(mineboard, showboard, x - 1, y+ 1);
}
if ((x ) > 0 && (y - 1) > 0 && (showboard[x][y - 1] == '*'))
{
openmine(mineboard, showboard, x, y - 1);
}
if ((x ) > 0 && (y+1 ) > 0 && (showboard[x ][y+1] == '*'))
{
openmine(mineboard, showboard, x, y + 1);
}
if ((x+1) > 0 && (y - 1) > 0 && (showboard[x +1][y - 1] == '*'))
{
openmine(mineboard, showboard, x +1, y - 1);
}
if ((x + 1) > 0 && (y ) > 0 && (showboard[x+ 1][y ] == '*'))
{
openmine(mineboard, showboard, x +1, y );
}
if ((x + 1) > 0 && (y+1) > 0 && (showboard[x+1][y +1] == '*'))
{
openmine(mineboard, showboard, x +1, y +1);
}
}
else
{
showboard[x][y] = Getcount(mineboard, x, y) + '0';
}
}3、 ... and 、game.h
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS ROW+2
#define EASY_COUNT 10
void Initboard(char board[ROWS][COLS], int rows,int cols, char ch);
void Display(char board[ROWS][COLS], int rows, int cols);
void Setmine(char board[ROWS][COLS], int rows, int cols);
void Palyermove(char mineboard[ROWS][COLS], char showboard[ROWS][COLS], int rows, int cols);
void Setnewmine(char board[ROWS][COLS], int x, int y);
void openmine(char mineboard[ROWS][COLS], char showboard[ROWS][COLS],int x,int y);
int Getcount(char mineboard[ROWS][COLS], int x, int y);
Iswin(char showboard[ROWS][COLS], int row, int col);
Complete thinking :
1. Create two checkerboard arrays , One to show , One is used to put thunder ;
The main body of the minesweeping game is a two-dimensional array of two character types . One is mineboard[][] It's made up of '0' and ‘1', among '0' It means no thunder ,'1' It means thunder . One is showboard[][] It's made up of '*' and ' Numbers '. An asterisk indicates an unopened place , The number indicates the number of mines around . What we should pay attention to here is :mineboard and showboard The actual size of is 11x11, But the effect of the display is 9x9.

2. Initialize and print the checkerboard array
It's easy to operate , I won't repeat

3. Buried mine
This is a buried function . Burying mines requires the use of random numbers , Use a and b To store random numbers .while The termination condition of the cycle is the number of Mines count Reach the preset thunder number EASY_COUNT .rand()%row+1 To produce 1~9 The random number .if The statement ensures that the mined area is not repeated .

4. Mine clearance
// Mine clearance
void Palyermove(char mineboard[ROWS][COLS], char showboard[ROWS][COLS], int rows, int cols)
{
int x = 0;
int y = 0;
while (1)
{
int flag = 0;
printf(" Please enter your coordinates :");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= ROW&&y >= 1 && y <= COL)
{
if (mineboard[x][y] == '1')// Step on Thunder for the first time
{
flag = 1;
mineboard[x][y] = '0';
Setnewmine(mineboard, x,y);// Relocating mines
openmine(mineboard, showboard[ROWS][COLS], x, y);
}
if (flag == 1 && mineboard[x][y] == '1')// It's not the first time to step on thunder , Game over
{
printf(" I'm sorry you were killed \n");
Display(mineboard, ROWS, COLS);
break;
}
if (mineboard[x][y] == '0')// Not stepping on thunder
{
flag = 1;
openmine(mineboard, showboard,x,y);// Expansion function
Display(showboard, ROWS, COLS);
}
}
else
{
printf(" The coordinates you entered are illegal , Please re-enter \n");
}
if (Iswin(showboard, ROW, COL) == EASY_COUNT)
{
printf(" Congratulations on winning \n");
break;
}
}
}This is a minesweeping function .,flag Is a flag variable .
first while The function of dead loop is to ensure that the correct coordinate information is input . Make sure you enter x and y After the data is correct . Start processing data , first if sentence , If you step on Thunder for the first time , Then move this mine to another place . the second if In the sentence , If it's not the first time to step on thunder , Then it will explode, end the dead cycle and end the game , the second if In the sentence , Will perform openmine Expansion function of , The result is the number of surrounding mines .
5. Deal with being killed the first time
This function quietly moves the thunder to other places without thunder in the Jiugong lattice .

5. Calculate the number of surrounding mines
because '0'+ Numbers = ‘ Numbers ', Is equivalent to char Type character is converted to int type , Indicates the number of thunder
int Getcount(char mineboard[ROWS][COLS], int x, int y)
{
return (mineboard[x - 1][y - 1] +
mineboard[x - 1][y] +
mineboard[x - 1][y + 1] +
mineboard[x][y - 1] +
mineboard[x][y + 1] +
mineboard[x + 1][y - 1] +
mineboard[x + 1][y] +
mineboard[x + 1][y + 1]) - 8 * '0';
}6. Expand the function to realize the blank Algorithm
The effect is as shown in the picture :

// Expansion function , Use recursion to sweep a piece
void openmine(char mineboard[ROWS][COLS], char showboard[ROWS][COLS], int x, int y)
{
if (Getcount(mineboard, x, y) == 0)
{
showboard[x][y] = ' ';
{
}
if ((x - 1) > 0 && (y - 1) > 0 && (showboard[x - 1][y - 1] == '*'))
{
openmine(mineboard, showboard, x - 1, y - 1);
}
if ((x - 1) > 0 && y && (showboard[x - 1][y] == '*'))
{
openmine(mineboard, showboard, x - 1, y );
}
if ((x - 1) > 0 && (y +1) <=COL && (showboard[x - 1][y+1] == '*'))
{
openmine(mineboard, showboard, x - 1, y+ 1);
}
if ((x ) > 0 && (y - 1) > 0 && (showboard[x][y - 1] == '*'))
{
openmine(mineboard, showboard, x, y - 1);
}
if ((x) > 0 && (y + 1) <= COL && (showboard[x][y + 1] == '*'))
{
openmine(mineboard, showboard, x, y + 1);
}
if ((x+1)<=ROW && (y - 1) > 0 && (showboard[x +1][y - 1] == '*'))
{
openmine(mineboard, showboard, x +1, y - 1);
}
if ((x + 1) <=ROW && y && (showboard[x+ 1][y ] == '*'))
{
openmine(mineboard, showboard, x +1, y );
}
if ((x + 1) <=ROW && (y+1) <=COL && (showboard[x+1][y +1] == '*'))
{
openmine(mineboard, showboard, x +1, y +1);
}
}
else
{
showboard[x][y] = Getcount(mineboard, x, y) + '0';
}

if Judge the condition , Avoid touching the bottom or the top .
Then use recursion to expand from the center to all directions , Until it reaches the top or bottom .
7. Judge whether the minesweeping is successful
Iswin(char showboard[ROWS][COLS], int row, int col)
{
int i = 0;
int count = 0;
for (i = 1; i <= row; i++)
{
int j = 0;
for (j = 1; j <= col; j++)
{
if (showboard[i][j] == "*")
{
count++;
}
}
}
return count;
}
边栏推荐
- Final review Day8
- 穀歌 | 蛋白序列的深度嵌入和比對
- 32GB Jetson Orin SOM 不能刷机问题排查
- Altaro o365 total backup subscription plan
- chromedriver对应版本下载
- ES7 easy mistakes in index creation
- Simpleitk learning notes
- Analysis of the example of network subnet division in secondary vocational school
- 【无标题】
- Common interview questions of microservice
猜你喜欢

Common interview questions of microservice

Mapbox tasting value cloud animation

2022.DAY592

PHP笔记超详细!!!

中职网络子网划分例题解析

redis 无法远程连接问题。

期末复习(Day5)

Primary school campus IP network broadcasting - Design of primary school IP digital broadcasting system based on campus LAN

Deep embedding and alignment of Google | protein sequences

Apache+php+mysql environment construction is super detailed!!!
随机推荐
Gan network thought
获取并监控远程服务器日志
聊聊如何利用p6spy进行sql监控
联想R7000显卡的拆卸与安装
Training method of grasping angle in grasping detection
Jetson AGX Orin 平台移植ar0233-gw5200-max9295相机驱动
Classification and discussion of plane grab detection methods based on learning
2022.7.2 模拟赛
"C and pointer" - Chapter 13 function of function pointer 1 - callback function 1
Ensemble, série shuishu] jour 9
Basic introduction of redis and explanation of eight types and transactions
[untitled]
求质数的方法
Webrtc M96 release notes (SDP abolishes Plan B and supports opus red redundant coding)
Troubleshooting of 32GB Jetson Orin SOM failure to brush
穀歌 | 蛋白序列的深度嵌入和比對
Error 1045 (28000) occurs when Linux logs in MySQL: access denied for user 'root' @ 'localhost' (using password: yes)
Sophomore dilemma (resumption)
Explanation of several points needing attention in final (tested by the author)
Shanghai daoning, together with American /n software, will provide you with more powerful Internet enterprise communication and security component services