当前位置:网站首页>[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.7.2 模拟赛
- Redis encountered noauth authentication required
- (perfect solution) how to set the position of Matplotlib legend freely
- Principles of BTC cryptography
- Disassembly and installation of Lenovo r7000 graphics card
- 期末复习(day3)
- How do I migrate my altaro VM backup configuration to another machine?
- [set theory] relational closure (relational closure related theorem)
- Hotel public broadcasting background music - Design of hotel IP network broadcasting system based on Internet +
- Altaro o365 total backup subscription plan
猜你喜欢
Altaro o365 total backup subscription plan
Introduction to redis using Lua script
谷歌 | 蛋白序列的深度嵌入和比对
中职网络子网划分例题解析
Together, Shangshui Shuo series] day 9
Hotel public broadcasting background music - Design of hotel IP network broadcasting system based on Internet +
Latest version of source insight
2022.DAY592
3dslam with 16 line lidar and octomap
Export the altaro event log to a text file
随机推荐
[untitled]
Primary school campus IP network broadcasting - Design of primary school IP digital broadcasting system based on campus LAN
Apache+php+mysql environment construction is super detailed!!!
2022.DAY592
配置xml文件的dtd
Configure DTD of XML file
Simpleitk learning notes
Xaml gradient issue in uwp for some devices
"C and pointer" - Chapter 13 advanced pointer int * (* (* (*f) () [6]) ()
Redis使用Lua脚本简介
Introduction to redis and explanation of data types
Transferring images using flask
XML Configuration File
Go practice -- generate and read QR codes in golang (skip2 / go QRcode and boombuilder / barcode)
NG Textarea-auto-resize
Go practice -- closures in golang (anonymous functions, closures)
Win10 install pytullet and test
Export the altaro event log to a text file
redis 遇到 NOAUTH Authentication required
Niuke JS separator