当前位置:网站首页>C language minesweeping latest recursive expansion super detailed explanation (with source code)
C language minesweeping latest recursive expansion super detailed explanation (with source code)
2022-07-27 06:22:00 【Big thunder pressing enter】
List of articles
One 、 Preface
what ! Write C Language minesweeping game ? It only needs 100 Line code 、 It only needs C Language foundation ? you 're right ! Super friendly to novices 、 Here comes the super detailed tutorial , Through the practice of this small project, your programming ability can be further improved . Too simple or not enough fun ?
C Language writing Tetris here :https://blog.csdn.net/qq_41796226/article/details/125818224
Game Description
1. Enter the corresponding horizontal and vertical coordinates x y ( Including input validity check )
2. Coordinate for (x , y) The dot of shows the number of mines around
3. If the number of surrounding mines is 0, The program automatically opens 0 Points around the location ( A recursive algorithm )
4. The game interface shows the number of remaining uncertain positions , And the number of known mines
5. You can customize the size of the interface and the number of Mines
6. Go and expand more functions
Game effect

Two 、 Implementation steps
1、 The header file 、 Macro definition 、 Global variables and function declarations
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define HEIGHT 10 // That's ok
#define WIDTH 10 // Column
#define BOOM_NUM 10
int count_last = HEIGHT*HEIGHT;
char user_map[HEIGHT][WIDTH]; // Player interface
char mine_map[HEIGHT][WIDTH]; // Designer interface
void initial_map();
void put_map(char (*p)[WIDTH]);
void set_bom();
void sweep(int x,int y);
int check();
int count_here(int m,int n);
2、 Initialize the game interface

Global variables :
char user_map[HEIGHT][WIDTH];
char mine_map[HEIGHT][WIDTH];
/* function : Assign initial values to two two-dimensional arrays of global variables */
void initial_map()
{
int i,j;
for(i=0;i<HEIGHT;i++)
{
for(j=0;j<WIDTH;j++)
{
user_map[i][j] = '*';// Point of player interface
mine_map[i][j] = '-';// Developer interface points
}
}
}
/* function : Print the game interface Parameters : Pass in mine_map perhaps user_map */
void put_map(char (*p)[WIDTH]) // Those who don't know much about array pointers can be divided into put_minemap and put_usermap Two functions write
{
check(); // Calculation in function user_map The remaining '*' The number of count_last
printf(" Remain: %2d Boom: %2d\n",count_last,BOOM_NUM);
int i,j;
for(i=0;i<2;i++) // Print auxiliary marks in the first two lines of the interface
{
printf(" ");
for(j=0;j<WIDTH;j++)
{
if(i==0)
printf("%2d",j);
else
printf(" v");
}
printf("\n");
}
for(i=0;i<HEIGHT;i++)
{
printf("%2d >",i); // Print auxiliary marks in the first two columns of the interface
for(j=0;j<WIDTH;j++)
{
printf(" %c",p[i][j]);
}
printf("\n");
}
}
3、 Lay out mines
#include<stdlib.h>
#include<time.h>
#define BOOM_NUM 10
/* function : to mine_map Randomly arranged in the coordinate system BOM_NUM A mine */
void set_bom()
{
srand(time(0)); // Random time seed
int x,y;
int bom_num = BOOM_NUM;
while(bom_num)
{
x = rand()%HEIGHT; // Choose a number at random 0~HEIGHT-1
y = rand()%WIDTH; // Choose a number at random 0~WIDTH-1
if(mine_map[x][y]=='@') // Avoid repeating mine laying in one position
continue;
mine_map[x][y] = '@';
bom_num--; // Each cloth makes a thunder , Subtract once
}
}
4、 Mine clearance
/* function : Calculation user_map[y][x] Number of mines around Parameters : The abscissa entered by the player x, Ordinate y Return value :(x,y) The number of mines around */
int count_here(int x,int y)
{
int count = 0;
int i,j;
for(i=(y==0?0:y-1);i<=(y==HEIGHT-1?HEIGHT-1:y+1);i++)
{
for(j=(x==0?0:x-1);j<=(x==WIDTH-1?WIDTH:x+1);j++)// Consider cross-border issues , The following figure explains in detail
{
if(mine_map[i][j]=='@') // adopt mine_map[i][j] To map user_map[i][j]
count++;
}
}
return count;
}

/* function : to user_map[y][x] Assigned to the number of mines around the point , if 0, Call itself to 0 Assign values to surrounding points , Recursion in turn Parameters : The abscissa entered by the player x, Ordinate y */
void sweep(int x,int y)
{
if(user_map[y][x]!='*')
return;
int count = count_here(x,y);
user_map[y][x] = count+'0'; // hold int Type conversion to char type
system("clear"); // Clear the screen ( Not linux Terminal comment this line of code )
fflush(stdout); // Clean up the terminal cache ( Not linux Terminal comment this line of code )
put_map(user_map);
usleep(1000*50); // Microsecond delay ( Not linux Terminal comment this line of code )
if(count!=0) // if count by 0, Recursively call
return;
int i,j;
for(i=(y==0?0:y-1);i<=(y==HEIGHT-1?HEIGHT-1:y+1);i++)// Consider cross-border issues , Detailed explanation of the above figure
{
for(j=(x==0?0:x-1);j<=(x==WIDTH-1?WIDTH-1:x+1);j++)
{
sweep(j,i);
}
}
return;
}
5、 Minesweeping completion judgment

Global variables
int count_last = HEIGHT*HEIGHT;
/* function : By calculation user_map[y][x] In the rest of the '*' The number of , Compare the number of Mines , Realize the judgment of the end of the game Return value : After minesweeping, return 1, Unfinished return 0 */
int check()
{
count_last = 0;
int i,j;
for(i=0;i<HEIGHT;i++)
{
for(j=0;j<WIDTH;j++)
{
if(user_map[i][j]=='*')
count_last++;
}
}
if(count_last==BOOM_NUM)
return 1;
else
return 0;
}
6、 The main function
int main()
{
system("clear"); // Clear the screen ( Not linux Terminal comment this line of code )
initial_map();
set_bom();
put_map(mine_map);
usleep(1000*2000); // Microsecond delay ( Not linux Terminal comment this line of code )
system("clear"); // Clear the screen ( Not linux Terminal comment this line of code )
put_map(user_map);
while(1)
{
if(check()) // Minesweeping completion judgment ( Game end conditions 1)
{
printf("Congratulations! You Win!\n");
put_map(mine_map);
break;
}
int x,y;
printf("Input coordinates: x y\n");
while(1) // Check the validity of the input
{
scanf("%d%d",&x,&y);
if(x<0||x>WIDTH||y<0||y>HEIGHT)
printf("Coordinates nagative,input again:\n");
else
break;
}
if(mine_map[y][x]=='@') // Judge by stepping on thunder ( Game end conditions 2)
{
printf("Oh no! You are on the Boom!\n");
put_map(mine_map);
break;
}
sweep(x,y);
}
return 0;
}
3、 ... and 、 End of the flower
1. I'm sorry if I mislead you because of the level limit
2. Welcome to leave a message below to discuss , Thank you for your support
3. Come on “ Huaqing vision ” http://www.hqyj.com/ Learn more about programming 
Four 、 Complete source code
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define HEIGHT 10 // That's ok
#define WIDTH 10 // Column
#define BOOM_NUM 10
int count_last = HEIGHT*HEIGHT;
char user_map[HEIGHT][WIDTH]; // Player interface
char mine_map[HEIGHT][WIDTH]; // Designer interface
void initial_map();
void put_map(char (*p)[WIDTH]);
void set_bom();
void sweep(int x,int y);
int check();
int count_here(int m,int n);
int main()
{
system("clear"); // Clear the screen ( Not linux Terminal comment this line of code )
initial_map();
set_bom();
put_map(mine_map);
putchar('\n');
usleep(1000*2000); // Microsecond delay ( Not linux Terminal comment this line of code )
system("clear");
put_map(user_map);
while(1)
{
if(check()) // Minesweeping completion judgment ( Game end conditions 1)
{
printf("Congratulations! You Win!\n");
put_map(mine_map);
break;
}
int x,y;
printf("Input coordinates: x y\n");
while(1) // Check the validity of the input
{
scanf("%d%d",&x,&y);
if(x<0||x>WIDTH||y<0||y>HEIGHT)
printf("Coordinates nagative,input again:\n");
else
break;
}
if(mine_map[y][x]=='@') // Judge by stepping on thunder ( Game end conditions 2)
{
printf("Oh no! You are on the Boom!\n");
put_map(mine_map);
break;
}
sweep(x,y);
}
return 0;
}
/* function : Assign initial values to two two-dimensional arrays of global variables */
void initial_map()
{
int i,j;
for(i=0;i<HEIGHT;i++)
{
for(j=0;j<WIDTH;j++)
{
user_map[i][j] = '*';// Point of player interface
mine_map[i][j] = '-';// Developer interface points
}
}
}
/* function : Print the game interface Parameters : Pass in mine_map perhaps user_map */
void put_map(char (*p)[WIDTH]) // Those who don't know much about array pointers can be divided into put_minemap and put_usermap Two functions write
{
check(); // Calculation in function user_map The remaining '*' The number of count_last
printf(" Remain: %2d Boom: %2d\n",count_last,BOOM_NUM);
int i,j;
for(i=0;i<2;i++) // Print auxiliary marks in the first two lines of the interface
{
printf(" ");
for(j=0;j<WIDTH;j++)
{
if(i==0)
printf("%2d",j);
else
printf(" v");
}
printf("\n");
}
for(i=0;i<HEIGHT;i++)
{
printf("%2d >",i); // Print auxiliary marks in the first two columns of the interface
for(j=0;j<WIDTH;j++)
{
printf(" %c",p[i][j]);
}
printf("\n");
}
}
/* function : to mine_map[y][x] Randomly arrange BOM_NUM A mine */
void set_bom()
{
srand(time(0));
int x,y;
int bom_num = BOOM_NUM;
while(bom_num)
{
x = rand()%HEIGHT;
y = rand()%WIDTH;
if(mine_map[x][y]=='@')
continue;
mine_map[x][y] = '@';
bom_num--;
}
}
/* function : Calculation user_map[y][x] Number of mines around Parameters : The abscissa entered by the player x, Ordinate y Return value :(x,y) The number of mines around */
int count_here(int x,int y)
{
int count = 0;
int i,j;
for(i=(y==0?0:y-1);i<=(y==HEIGHT-1?HEIGHT-1:y+1);i++)
{
for(j=(x==0?0:x-1);j<=(x==WIDTH-1?WIDTH:x+1);j++)// Consider cross-border issues
{
if(mine_map[i][j]=='@')
count++;
}
}
return count;
}
/* function : to user_map[y][x] Assigned to the number of mines around the point , if 0, Call itself to 0 Assign values to surrounding points , Recursion in turn Parameters : The abscissa entered by the player x, Ordinate y */
void sweep(int x,int y)
{
if(user_map[y][x]!='*')
return;
int count = count_here(x,y);
user_map[y][x] = count+'0'; // hold int Type conversion to char type
system("clear"); // Clear the screen ( Not linux Terminal comment this line of code )
fflush(stdout); // Clean up the terminal cache ( Not linux Terminal comment this line of code )
put_map(user_map);
usleep(1000*50); // microsecond ( Not linux Terminal comment this line of code )
if(count!=0) // if count by 0, Recursively call
return;
int i,j;
for(i=(y==0?0:y-1);i<=(y==HEIGHT-1?HEIGHT-1:y+1);i++)
{
for(j=(x==0?0:x-1);j<=(x==WIDTH-1?WIDTH-1:x+1);j++)
{
sweep(j,i);
}
}
return;
}
/* function : By calculation user_map[y][x] In the rest of the '*' The number of , Compare the number of Mines , Realize the judgment of the end of the game Return value : After minesweeping, return 1, Unfinished return 0 */
int check()
{
count_last = 0;
int i,j;
for(i=0;i<HEIGHT;i++)
{
for(j=0;j<WIDTH;j++)
{
if(user_map[i][j]=='*')
count_last++;
}
}
if(count_last==BOOM_NUM)
return 1;
else
return 0;
}
边栏推荐
猜你喜欢

论文报告-Linear Regression for face recognition

Ram of IP core

文件内容的读写——数据流

wireshark图形界面介绍

线程安全问题详解

多线程CAS、synchronized锁原理 、JUC以及死锁

Advanced ROS communication mechanism

5g's past and present life -- a brief introduction to the development of mobile communication

Multi coordinate transformation

TF coordinate transformation
随机推荐
学习软件测试时需要配备的运行环境需求搭建
数据库的联合查询
IP核小结
Brief introduction to unity menu interface
Solve binary tree (6)
数据库的索引和事务(重点)
Wireshark graphical interface capture
Basic concepts of software testing
Multi threaded CAS, synchronized lock principle, JUC and deadlock
多坐标变换
遥感影像识别-制作数据集
The problem that tqdm cannot display in a single line
Multi coordinate transformation
What is the difference between single line and three line when renting servers in Hong Kong?
Ram of IP core
论文写作(收获)
Pycharm installation and import project considerations
正确安装wireshark
Calculation of Huffman tree, code implementation and proof, graphic interpretation
切线空间以及TBN矩阵