Minesweeper–contains detailed content of blank space expansion

Table of Contents

1. Analysis and ideas
2. Header file description
3. Main function
4. Various functions of branch functions
5. All codes

1. Analysis and ideas:

Clear mines through the Windows mini-game By playing this mini-game, it is not difficult for us to discover this It is a chessboard composed of two-dimensional arrays and some functional functions, so we have to start with the chessboard first, and then write multiple functional functions,
Then the first step is to construct the chessboard. Through observation, we can get that there are two two-dimensional arrays. The first two-dimensional array is the blank square on the first layer of the chessboard, and the second two-dimensional array is the thunder and lightning on the second layer. For the safety grid, the second step is various functional functions. The order of priority is to arrange mines, enter the coordinates to find mines, and the number of mines around the grid that are not mines (in order to get closer to the prototype, add a recursion here to expand the blank grid) function), in order to facilitate subsequent viewing, the code is written in the form of multiple files

2. Header file description:

#include<stdio.h>
//This is the header file of the input and output functions in C language

#include<stdlib.h>
//This header file is the header file for rand in generating random numbers, and the header file for the randomly generated seed by srand
//The seeds generated by srand are not truly random, so time (NULL) must be used to achieve randomness.

#include<time.h>
//Call the function time() to generate a timestamp to achieve the randomization of srand generated seeds.

#include<Windows.h>
//Call the system() function to clear the screen
#define ROW 9 //Define the chessboard to be 9 rows wide
#define COL 9 //Define the chessboard to be 9 columns long
#define ROWS ROW + 2 //Define the rows of the two-dimensional array
#define COLS COL + 2 //Define the columns of the two-dimensional array

void InitBoard(char arr[ROWS][COLS], int rows, int cols,char set);
//In the header file, declare the created chessboard function
void Display(char arr[ROWS][COLS],int row,int col);
//In the header file, declare the function that displays the chessboard
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col);
//In the header file, declare the function to find mines
void SetMine(char arr[ROWS][COLS], int row, int col);
//In the header file, declare the function for deploying mines
int Xianshi(char mine[ROWS][COLS], int x, int y);
//In the header file, declare a function to determine the number of mines around the blank grid.
void DiGui(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int row, int col);
//In the header file, declare the function to expand blank spaces

This is the header file created in the game.h file, and the corresponding meaning and function of each header file. Of course, if you want to achieve more functions, you can call more functions and define global variables later. The row and column length of the array, and the functional functions used later by the main function are declared once in the header file so that they do not need to be declared again when called later.

3. Main function

The main function of this minesweeper is relatively simple and easy to understand. Let’s look at the code first.

#include"mine.h"

void menu() //Sets an initial menu function
{
printf("************************\
");
printf("****** 1. play *****\
");
printf("****** 2. exit *****\
");
printf("************************\
");
} //Print the required opening options and menus

void game() //The most important game function of Minesweeper, which also contains the functional functions of this game
{
char mine[ROWS][ROWS]; //The first two-dimensional array is defined, which is the chessboard for background mines
char show[COLS][COLS]; //Define the second two-dimensional array, which is the blank space on the display page
InitBoard(mine, ROWS, COLS, '0');//Initialize the thunderboard
InitBoard(show, ROWS, COLS, '*');//Initialize the display board
Display(show, ROW, COL);//Display the chessboard
SetMine(mine, ROW, COL);//Mine
FindMine(mine, show, ROW, COL);//Find mine
}

int main()
{
srand((unsigned int)time(NULL));
//To generate random positions for arranging mines, the time() function is called and the null pointer is used to realize the position of random mines generated by srand.
int a = 0;
do
{
menu(); //Call menu function
printf("Enter your choice:");
scanf("%d", & amp;a); //Enter the selected option
switch (a) //Determine subsequent operations based on the input options
{
case 1:
game(); //Call the game function and officially enter the game
break;
case 2:
printf("Exit game");
break;
default:
printf("Input error and restart\
");
break;
}
} while (a != 2);//If you don’t choose to exit, you will keep looping
return 0;
}

This is the main function of Minesweeper. Various functional functions have been called. Next is the introduction of each functional function.

This is the printed menu:

4. Functional functions:

1. Create a chessboard and use relatively simple loop nesting to complete the creation of a two-dimensional array

void InitBoard(char arr[ROWS][COLS], int rows, int cols,char set)
{
int i;
for (i = 0; i < rows; i + + )
{
int j = 0;
for (j = 0; j < cols; j + + )
{
arr[i][j] = set;
           //Two for loops define the chessboard. Set a parameter set in the function to facilitate changing the characters on the chessboard later.
}
}
}

2. Print the chessboard and mark the corresponding printed serial numbers on the rows of the chessboard. The code is relatively easy to understand

void Display(char arr[ROWS][COLS], int row, int col)
{
int i;
printf("************Mine Sweeper************\
");
for (i = 0; i <= row; i + + )
{
printf("%d ", i); // Mark the rows of the chessboard with serial numbers to facilitate input of coordinates
}
printf("\
");

for (i = 1; i <= row; i + + )
{
int j = 0;
printf("%d ", i); // Mark the columns of the chessboard with serial numbers to facilitate input of coordinates
for (j = 1; j <= col; j + + )
{
printf("%c ", arr[i][j]); //Print the chessboard of the two-dimensional array
}
printf("\
");
}
}

This is the chessboard display printed when the code is run

3. To arrange the thunder function, rand is used, and the main function calls time() to achieve true randomness

void SetMine(char arr[ROWS][COLS], int row, int col)//Function to arrange mines
{
int m = 10; //Define m as mine and initialize it to ten
while (m) //When all ten mines are arranged, end the loop
{
int x = rand() % row + 1;//Generate random numbers through rand to determine the horizontal coordinate of thunder
int y = rand() % col + 1;//Generate random numbers through rand to determine the abscissa of mine
if (arr[x][y] == '0')
{
arr[x][y] = '1'; //Determine whether mines have been deployed at this location
m--; //Successfully arrange an m self-decrement
}
}
}

4. Enter the coordinates to find the function of mine:

Using the nesting of functions, the Xianshi() and Digui functions are re-called in the function of searching for thunder

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int num = 71;
while (num!=0) //The 71 spaces after eliminating mines have been checked and the loop ends.
{
printf("Please enter the coordinates of the mine to be cleared");
scanf("%d %d", & amp;x, & amp;y); //Input coordinates
if (x >= 1 & amp; & amp; x <= row & amp; & amp; y >= 1 & amp; & amp; y <= col)//If the input coordinates are on the chessboard
{
if (mine[x][y] == '1') //If the input coordinate is '1', it is mine
{
printf("I'm sorry you were killed\
");
Display(mine, ROW, COL); //Display mine's chessboard, which is the answer
break;
}
else //If the coordinates you are looking for are not mine
{
int n;
\t\t\t\t
n = Xianshi(mine, x, y); //Call a function that calculates how many mines are around
show[x][y] = n + '0'; //If there are several mines around the coordinates being searched, the number will be displayed.
if (n== 0)
{
DiGui(mine, show, x, y, ROW, COL); //Call this recursive function to expand multiple blanks
}
show[x][y] = n + '0';
Display(show, ROW, COL);//Display the chessboard
num--;
\t\t\t\t
}
}
else
printf("Input error, re-enter:");

}
if (num == 0) //If all spaces are checked, you will win.
{
printf("Congratulations on your win\
");
Display(mine, ROW, COL);
}
\t
}

5. Complete a function that calculates how many mines there are around the investigation, and display it at the coordinates

int Xianshi(char mine[ROWS][COLS],int x,int y)
{
int count;
return count = mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x - 1][y] + mine[x + 1][y] + mine[x][y + 1] + mine[x - 1][y + 1] + mine[x + 1][y + 1] + 8 * '0';
 //An integer count is defined to add up the numbers in the eight surrounding coordinates. Because there are characters in the coordinates, the ASSIC code minus 0 becomes an integer.

}

5. Complete a recursive function to expand most of the blank areas to zero

void DiGui(char mine[ROWS][COLS],char show[ROWS][COLS],int x,int y,int row,int col)
{
int count=Xianshi(mine,x,y);
      show[x][y]=count + '0'; //Call the Xianshi() function to determine whether the coordinates being checked are zero.
 
if (count!=0) //If it is not zero, jump out of this function
{
          return;
}
    int i;
    int xx[]={1,1,1,-1,-1,-1,0,0};
     int yy[]={1,0,-1,0,1,-1,1,-1};//The horizontal and vertical coordinates of the eight surrounding grids
      for(i=0;i<8;i + + )//Loop eight times to determine whether there are non-zero numbers around
      {
     int dx =x + xx[i];
    int dy = y + yy[i];
      if(dx >= 1 & amp; & amp; dx <= row & amp; & amp; dy >= 1 & amp; & amp; dy <= col & amp; & amp; mine[dx][dy] = = '0' & amp; & amp; show[dx][dy]=='*')
      {
        DiGui(mine,show,dx,dy,ROW,COL);//If it is zero, return to the Digui function again and perform recursion
      }
 
       }
}

5. All codes

mine.h file

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>
#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS COL + 2
void InitBoard(char arr[ROWS][COLS], int rows, int cols,char set);
void Display(char arr[ROWS][COLS],int row,int col);
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col);
void SetMine(char arr[ROWS][COLS], int row, int col);
int Xianshi(char mine[ROWS][COLS], int x, int y);
void DiGui(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int row, int col);

main.c file

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"mine.h"

void menu()
{
printf("**********************\
");
printf("****** 1. play *****\
");
printf("****** 2. exit *****\
");
printf("************************\
");

}
void game()
{
char mine[ROWS][ROWS];
char show[COLS][COLS];
InitBoard(mine, ROWS, COLS, '0');//Initialize the chessboard
InitBoard(show, ROWS, COLS, '*');
Display(show, ROW, COL);//Display the chessboard
SetMine(mine, ROW, COL);//Mine
FindMine(mine, show, ROW, COL);//Find mine


}

int main()
{
srand((unsigned int)time(NULL));
int a = 0;
do
{
menu();
printf("Enter your choice:");
scanf("%d", & amp;a);
switch (a)
{
case 1:
game();
break;
case 2:
printf("Exit game");
break;
default:
printf("Input error and restart\
");
break;
}
} while (a != 2);
return 0;
}

game.c function

#define _CRT_SECURE_NO_WARNINGS 1
#include"mine.h"
void InitBoard(char arr[ROWS][COLS], int rows, int cols,char set)
{
int i;
for (i = 0; i < rows; i + + )
{
int j = 0;
for (j = 0; j < cols; j + + )
{
arr[i][j] = set;
}
}
}
void Display(char arr[ROWS][COLS], int row, int col)
{
int i;
printf("************Mine Sweeper**********\
");
for (i = 0; i <= row; i + + )
{
printf("%d ", i);
}
printf("\
");

for (i = 1; i <= row; i + + )
{
int j = 0;
printf("%d ", i);
for (j = 1; j <= col; j + + )
{
printf("%c ", arr[i][j]);
}
printf("\
");
}
}
void SetMine(char arr[ROWS][COLS], int row, int col)
{
int m = 10;
while (m)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (arr[x][y] == '0')
{
arr[x][y] = '1';
m--;
}
}
}
int Xianshi(char mine[ROWS][COLS],int x,int y)
{
int count;
return count = mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x - 1][y] + mine[x + 1][y]
+ mine[x][y + 1] + mine[x - 1][y + 1] + mine[x + 1][y + 1] + 8 * '0';

}
void DiGui(char mine[ROWS][COLS],char show[ROWS][COLS],int x,int y,int row,int col)
{
int count=Xianshi(mine,x,y);
      show[x][y]=count + '0';
 
if (count!=0)
{
          return;
}
    int i;
    int xx[]={1,1,1,-1,-1,-1,0,0};
     int yy[]={1,0,-1,0,1,-1,1,-1};
      for(i=0;i<8;i + + )
      {
     int dx =x + xx[i];
    int dy = y + yy[i];
      if(dx >= 1 & amp; & amp; dx <= row & amp; & amp; dy >= 1 & amp; & amp; dy <= col & amp; & amp; mine[dx][dy] = = '0' & amp; & amp; show[dx][dy]=='*')
      {
        DiGui(mine,show,dx,dy,ROW,COL);
      }
 
       }
}

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int num = 71;
while (num!=0)
{
printf("Please enter the coordinates of the mine to be cleared");
scanf("%d %d", & amp;x, & amp;y);
if (x >= 1 & amp; & amp; x <= row & amp; & amp; y >= 1 & amp; & amp; y <= col)
{
if (mine[x][y] == '1')
{
printf("I'm sorry you were killed\
");
Display(mine, ROW, COL);
break;
}
else
{
int n;
\t\t\t\t
n = Xianshi(mine, x, y);
show[x][y] = n + '0';
if (n== 0)
{
DiGui(mine, show, x, y, ROW, COL);
}
show[x][y] = n + '0';
Display(show, ROW, COL);//Display the chessboard
num--;
\t\t\t\t
}
}
else
printf("Input error, re-enter:");

}
if(num == 0)
{
printf("Congratulations on your win\
");
Display(mine, ROW, COL);
}
\t
}
6. Output results: The results are quite cumbersome, so only part of the results are intercepted. Interested friends can go and play with them

Thanks for browsing, friends. If you like it, you can like it three times! ! !