当前位置:网站首页>[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;
}
边栏推荐
- 2022.6.30DAY591
- [together Shangshui Shuo series] day 7 content +day8
- Webrtc protocol introduction -- an article to understand ice, stun, NAT, turn
- ES 2022 正式发布!有哪些新特性?
- Redis expiration elimination mechanism
- Go practice -- design patterns in golang's singleton
- 获取并监控远程服务器日志
- Redis encountered noauth authentication required
- (perfect solution) how to set the position of Matplotlib legend freely
- Go practice -- factory mode of design patterns in golang (simple factory, factory method, abstract factory)
猜你喜欢
![Together, Shangshui Shuo series] day 9](/img/39/c1ba1bac82b0ed110f36423263ffd0.png)
Together, Shangshui Shuo series] day 9

Disassembly and installation of Lenovo r7000 graphics card

Analysis of the example of network subnet division in secondary vocational school

大学校园IP网络广播-厂家基于校园局域网的大学校园IP广播方案设计指南

穀歌 | 蛋白序列的深度嵌入和比對

6.23星期四库作业

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

大二困局(复盘)

Apache+php+mysql environment construction is super detailed!!!

Configure DTD of XML file
随机推荐
Why should we rewrite hashcode when we rewrite the equals method?
AtCoder Beginner Contest 258(A-D)
大学校园IP网络广播-厂家基于校园局域网的大学校园IP广播方案设计指南
redis 无法远程连接问题。
[untitled]
Installing altaro VM backup
期末复习DAY8
Final review (Day6)
32GB Jetson Orin SOM 不能刷机问题排查
Obtenir et surveiller les journaux du serveur distant
Ensemble, série shuishu] jour 9
Export the altaro event log to a text file
ninja: build stopped: subcommand failed.
Go practice -- generate and read QR codes in golang (skip2 / go QRcode and boombuilder / barcode)
谷歌 | 蛋白序列的深度嵌入和比对
Congratulations to musk and NADELLA on their election as academicians of the American Academy of engineering, and Zhang Hongjiang and Fang daining on their election as foreign academicians
[untitled]
Final review (Day7)
About debugging the assignment of pagenum and PageSize of the formal parameter pageweb < T > (i.e. page encapsulation generic) in the controller
Progressive multi grasp detection using grasp path for rgbd images