Using C Language to Realize Minesweeper

This article is suitable for C language beginners, mainly involving the use of functions, arrays, and branch loops.

Directory

Design thinking:

Total code (improved):

Running result display:

Distribution introduction:

statement:

The main part of the code:

Function module implementation:

Initialize the module:

print module:

Mines module:

Judgment module:

Summarize:


Design thinking:

First of all, there must be a Game Menu, input 1 means start, 0 means end, other numbers will prompt input error, please re-enter; secondly, there must be Leipan, Leipan is represented by a two-dimensional array, at the beginning There must be a initialization module to initialize the two-dimensional array, there must be a mining module to place mines, there must be a judgment win or loss and return result module, there must be Print module shows the mines to the player. In order to realize these modules more easily, we choose two two-dimensional arrays to represent the mines, one of which lays mines, and one of them shows the player. In order to facilitate the calculation of the number of mines around, the space is represented by ‘0’, and the mine is represented by ‘1’.

Total code (improved):

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROW 9
#define COL 9

#define ROWS ROW + 2
#define COLS COL + 2

#define Easy_Mine 10

void InitBorad(char borad[ROWS][COLS], int rows, int cols, char ch);
void DisplayBorad(char borad[ROWS][COLS], int row, int col);
void SetMine(char borad[ROWS][COLS], int row, int col);
void FindMine(char mine_borad[ROWS][COLS], char show_board[ROWS][COLS], int row, int col);

//initialization
void InitBorad(char borad[ROWS][COLS], int rows, int cols, char ch)
{
int i = 0;

for (i = 0; i < rows; i ++ )
{
int j = 0;

for (j = 0; j < cols; j ++ )
{
boardad[i][j] = ch;
}
}
}

// print extension
void DisplayBorad(char borad[ROWS][COLS], int row, int col)
{
int i = 0;
//column label
for (i = 0; i <= col; i ++ )
{
printf(" %d |", i);
}
printf("\\
");
for (i = 0; i <= col; i ++ )
{
printf("---|", i);
}
printf("\\
");
  
for (i = 1; i <= row; i ++ )
{
//row mark
printf(" %d |", i);
int j = 0;
for (j = 1; j <= col; j ++ )
{
printf(" %c |",borad[i][j]);
}
printf("\\
");
\t\t
for (j = 0; j <= col; j ++ )
{
printf("---|", i);
}
printf("\\
");
}
printf("\\
");
}

// place mine
void SetMine(char borad[ROWS][COLS], int row, int col)
{
int count = Easy_Mine;

while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;

if (borad[x][y] == '0')
{
boardad[x][y] = '1';
count--;
}
}

}

//Improve the judgment method

//Recursively realize the expansion of minesweeper
void GetMineCount(char mine_borad[ROWS][COLS], char show_borad[ROWS][COLS], int x, int y, int* count)
{
if (x >= 1 & amp; & amp; x <= ROW & amp; & amp; y >= 1 & amp; & amp; y <= COL & amp; & amp; show_borad[x][y] = = '*')
{
int i = x - 1;
int j = y - 1;
int sum = 0;
// Calculate how many mines are around
for (i = x - 1; i <= x + 1; i + + )
{
for (j = y - 1; j <= y + 1; j + + )
{
sum = sum + (mine_borad[i][j] - '0');
}
}
//If there are no mines around, set this coordinate as a space, recursively find out if there are mines around...
if (sum == 0)
{
show_borad[x][y] = ' ';
for (i = x - 1; i <= x + 1; i + + )
{
for (j = y - 1; j <= y + 1; j + + )
{
//Correct the number of loops
(*count)--;
GetMineCount(mine_borad, show_borad, i, j, count);
}
}
}
//If there are mines around, this coordinate character should be the character corresponding to the number of mines
else
{
//Correct the number of loops
(*count)--;
show_borad[x][y] = sum + '0';
}
}
}

void FindMine(char mine_borad[ROWS][COLS], char show_board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = row * col - Easy_Mine;
// implement the main body
while (count)
{
printf("Please enter the coordinates:>");
scanf("%d %d", &x, &y);
if (x >= 1 & amp; & amp; x <= row & amp; & amp; y >= 1 & amp; & amp; y <= col)
{
if (mine_borad[x][y] == '1')
{
printf("Boom! You were killed!\\
");
DisplayBorad(mine_borad, ROW, COL);
break;
}
else if (mine_borad[x][y] == '0' & amp; & amp; show_board[x][y] == '*');
{
GetMineCount(mine_borad, show_board, x, y, &count);
//count = count_number(show_board, row, col);
//DisplayBorad(mine_borad, ROW, COL);
DisplayBorad(show_board, ROW, COL);
}
}
else
{
printf("Input coordinates are illegal, please re-enter!\\
");
}
}
if (count == 0)
{
printf("Congratulations, you have won!\\
");
DisplayBorad(mine_borad, row, col);

}
}

void menu()
{
printf("******************************\\
");
printf("******** 1.paly ***********\\
");
printf("******** 0.exit ***********\\
");
printf("******************************\\
");
}

void game()
{
//This two-dimensional array is used to place mines
char mine[ROWS][COLS] = { 0 };
//This two-dimensional array is used to display to the player
char show[ROWS][COLS] = { 0 };

//Initialize two 2D arrays
InitBorad(mine, ROWS, COLS, '0');
//DisplayBorad(mine, ROW, COL);
InitBorad(show, ROWS, COLS, '*');

// place mine
SetMine(mine, ROW, COL);

// print the chessboard
DisplayBorad(show, ROW, COL);

//DisplayBorad(mine, ROW, COL);
//The main body of the game
FindMine(mine, show, ROW, COL);
}

//main function
int main()
{
int input = 0;
srand((unsigned int)time(NULL));

do
{
menu();
printf("Please choose:>");
scanf("%d", &input);

switch (input)
{
case 1:
game();
break;
case 0:
printf("Exit the game!\\
");
break;
default:
printf("Choose wrong, please choose again!\\
");
break;
}
} while (input);
return 0;
}

Run result display:

Distribution introduction:

Statement:

If you want to change the size of the mine disk and the number of mines to place, you only need to modify ROW, COL, and Easy_Mine accordingly.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//Identifier defines row and column
#define ROW 9
#define COL 9
//The actual size of the mine disk
#define ROWS ROW + 2
#define COLS COL + 2
//The number of mines
#define Easy_Mine 10
//main function declaration
void InitBorad(char borad[ROWS][COLS], int rows, int cols, char ch);
void DisplayBorad(char borad[ROWS][COLS], int row, int col);
void SetMine(char borad[ROWS][COLS], int row, int col);
void FindMine(char mine_borad[ROWS][COLS], char show_board[ROWS][COLS], int row, int col);

Code body:

As for the 9 * 9 mine disk, why choose the two-dimensional array representation of 11 * 11, it is to prevent out-of-bounds access, and to more easily realize the judgment and return result after minesweeping. Use character 0 to initialize the two-dimensional array where mines will be placed, use character * to initialize the two-dimensional array displayed to the player, and use character 1 to represent mines.

//game menu
void menu()
{
printf("******************************\\
");
printf("******** 1.paly ***********\\
");
printf("******** 0.exit ***********\\
");
printf("******************************\\
");
}
// implement the main body
void game()
{
//This two-dimensional array is used to place mines
char mine[ROWS][COLS] = { 0 };
//This two-dimensional array is used to display to the player
char show[ROWS][COLS] = { 0 };

//Initialize two 2D arrays
InitBorad(mine, ROWS, COLS, '0');
//DisplayBorad(mine, ROW, COL);
InitBorad(show, ROWS, COLS, '*');

// place mine
SetMine(mine, ROW, COL);

// print the chessboard
DisplayBorad(show, ROW, COL);

//DisplayBorad(mine, ROW, COL);
//The main body of the game
FindMine(mine, show, ROW, COL);
}
//main function
int main()
{
int input = 0;
    //Generate the starting point of random numbers to serve the mine-burying module
srand((unsigned int)time(NULL));

do
{
menu();
printf("Please choose:>");
scanf("%d", &input);

switch (input)
{
case 1:
game();
break;
case 0:
printf("Exit the game!\\
");
break;
default:
printf("Choose wrong, please choose again!\\
");
break;
}
} while (input);
return 0;
}

Function module implementation:

Initialization module:

Initialize the passed array with the passed characters through nested for loops.

//initialization
void InitBorad(char borad[ROWS][COLS], int rows, int cols, char ch)
{
int i = 0;

for (i = 0; i < rows; i ++ )
{
int j = 0;

for (j = 0; j < cols; j ++ )
{
boardad[i][j] = ch;
}
}
}

Print module:

Original print module:

First use the for loop to print the column label, and then use the nested for loop to print the row label and the two-dimensional array. The code and mine disk are displayed as follows:

//print
void DisplayBorad(char borad[ROWS][COLS], int row, int col)
{
int i = 0;
    //column label
for (i = 0; i <= col; i ++ )
{
printf("%d ", i);
}
printf("\\
");
for (i = 1; i <= row; i ++ )
{
        //row mark
printf("%d ", i);
        // traverse the two-dimensional array
int j = 0;
for (j = 1; j <= col; j ++ )
{
printf("%c ", boardad[i][j]);
}
printf("\\
");
}
printf("\\
");
}

Thunder Disk:

Improve printing module:

The principle is as above, with modification, the code and mine disk are shown as follows:

//print expansion
void DisplayBorad(char borad[ROWS][COLS], int row, int col)
{
int i = 0;
//column label
for (i = 0; i <= col; i ++ )
{
printf(" %d |", i);
}
printf("\\
");
for (i = 0; i <= col; i ++ )
{
printf("---|", i);
}
printf("\\
");
  
for (i = 1; i <= row; i ++ )
{
//row mark
printf(" %d |", i);
int j = 0;
for (j = 1; j <= col; j ++ )
{
printf(" %c |",borad[i][j]);
}
printf("\\
");
\t\t
for (j = 0; j <= col; j ++ )
{
printf("---|", i);
}
printf("\\
");
}
printf("\\
");
}

Thunder Disk:

Mine-burying module:

To achieve random placement of mines on the mine disk, the srand() and time() library functions are needed to determine the starting point of the random number generation period, and the rand() library function is used to generate random numbers, and the size of the obtained random numbers is controlled between 1 and 9 time (the logical size of the mine disk), after obtaining the coordinates of the mine to be placed, find the corresponding two-dimensional array element, and set it as character 1. (The character 0 means it is not mine, the character 1 means mine) The module code and the two-dimensional array display of placing mines:

//place mine
void SetMine(char borad[ROWS][COLS], int row, int col)
{
    //Number of mines to place
int count = Easy_Mine;

while (count)
{
        //Get the coordinates of the mine to be placed
int x = rand() % row + 1;
int y = rand() % col + 1;
        //If mine is not placed at this coordinate, place mine, otherwise regain coordinates
if (borad[x][y] == '0')
{
boardad[x][y] = '1';
count--;
}
}
}

A two-dimensional array for placing mines:

Judgement module:

Original judgment module;

This module needs to pass both the two-dimensional arrays of real mines and fake mines. The player enters the coordinates to be mineswept. If the coordinates are illegal, it will prompt that the input is wrong and re-enter. If the coordinates are legal, compare them with the corresponding coordinates of the two-dimensional array where mines are placed. If the coordinates are mines, it will output that the player failed to clear mines, and show the two-dimensional array of mines to the player. The game is over. If this coordinate is not mine, judge whether there is mine around this coordinate, if there is no mine, return 0, if there is mine, return the number of mine, and show the corresponding coordinates of the two-dimensional array to the player Change to the number of mines, correct the loop control condition, and continue the game. When the loop control condition is no longer satisfied (and all coordinates that are not mines have been swept out), the game wins and the game ends.

//Original judgment method

//Return the number of mines around this coordinate
int GetMineCount(char borad[ROWS][COLS], int x, int y)
{
//The character 1~9 minus a character 0 can get the integer number 1~9, which is why the characters 0 and 1 are used to indicate whether there is thunder
return (borad[x - 1][y] + borad[x - 1][y - 1] + borad[x][y - 1] + borad[x + 1][y - 1]
+ borad[x + 1][y] + borad[x + 1][y + 1] + borad[x][y + 1] + borad[x - 1][y + 1] - 8 * '0 ');
}

// judge win or lose
void FindMine(char mine_borad[ROWS][COLS], char show_board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
//loop control condition
int count = row * col - Easy_Mine;
while (count)
{
printf("Please enter the coordinates:>");
scanf("%d %d", &x, &y);
/ / Determine whether the input coordinates are legal
if (x >= 1 & amp; & amp; x <= row & amp; & amp; y >= 1 & amp; & amp; y <= col)
{
//This coordinate is mine
if (mine_borad[x][y] == '1')
{
printf("Boom! You were killed!\\
");
//Print the two-dimensional array where mines are placed
DisplayBorad(mine_borad, ROW, COL);
break;
}
//This coordinate is not mine
else if (mine_borad[x][y] == '0' & amp; & amp; show_board[x][y] != ' ');
{
//The function call returns the number of mines
int ret = GetMineCount(mine_borad, x, y);
//Change the corresponding coordinates of the two-dimensional array displayed to the player to the number of mines (adding a character 0 to the integer number 1~9 can be converted into characters 1~9)
show_board[x][y] = ret + '0';
\t\t\t\t//Print
DisplayBorad(show_board, ROW, COL);
//Correct the loop control condition
count--;
}
}
else
{
printf("Input coordinates are illegal, please re-enter!\\
");
}
}
//Game victory determination
if (count == 0)
{
printf("Congratulations, you have won!\\
");
DisplayBorad(mine_borad, row, col);

}
}

Run Showcase:

Improved Judgment Module:

The above judgment module is still somewhat different from the minesweeper game. In the minesweeper game, when there are no mines around a coordinate, it will be empty, and then judge the coordinates around it… Therefore, it is necessary to To improve it, the overall idea remains the same, only need to modify the submodule for judging the number of thunders in a circle around the coordinates and the loop control conditions. When there are no mines around the input coordinates, set the corresponding coordinates of the two-dimensional array displayed to the player as space, change the loop control condition, and judge whether there are mines around the coordinates.. …So on and so forth. This function is realized here by circulation and recursion.

//Improved judgment method

//Recursively realize the expansion of minesweeper
void GetMineCount(char mine_borad[ROWS][COLS], char show_borad[ROWS][COLS], int x, int y, int* count)
{
// recursive constraints
if (x >= 1 & amp; & amp; x <= ROW & amp; & amp; y >= 1 & amp; & amp; y <= COL & amp; & amp; show_borad[x][y] = = '*')
{
int i = x - 1;
int j = y - 1;
//This variable represents the number of mines
int sum = 0;
// Calculate how many mines are around
for (i = x - 1; i <= x + 1; i + + )
{
for (j = y - 1; j <= y + 1; j + + )
{
sum = sum + (mine_borad[i][j] - '0');
}
}
//If there are no mines around, set this coordinate as a space, recursively find out if there are mines around...
if (sum == 0)
{
show_borad[x][y] = ' ';
for (i = x - 1; i <= x + 1; i + + )
{
for (j = y - 1; j <= y + 1; j + + )
{
//Correct the number of loops
(*count)--;
// recursion
GetMineCount(mine_borad, show_borad, i, j, count);
}
}
}
//If there are mines around, change the coordinate character to the character corresponding to the number of mines
else
{
//Correct the number of loops
(*count)--;
show_borad[x][y] = sum + '0';
}
}
}

void FindMine(char mine_borad[ROWS][COLS], char show_board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = row * col - Easy_Mine;
// implement the main body
while (count)
{
printf("Please enter the coordinates:>");
scanf("%d %d", &x, &y);
if (x >= 1 & amp; & amp; x <= row & amp; & amp; y >= 1 & amp; & amp; y <= col)
{
if (mine_borad[x][y] == '1')
{
printf("Boom! You were killed!\\
");
DisplayBorad(mine_borad, ROW, COL);
break;
}
else if (mine_borad[x][y] == '0' & amp; & amp; show_board[x][y] == '*');
{
GetMineCount(mine_borad, show_board, x, y, &count);
//count = count_number(show_board, row, col);
//DisplayBorad(mine_borad, ROW, COL);
DisplayBorad(show_board, ROW, COL);
}
}
else
{
printf("Input coordinates are illegal, please re-enter!\\
");
}
}
if (count == 0)
{
printf("Congratulations, you have won!\\
");
DisplayBorad(mine_borad, row, col);

}
}

Run Showcase:

Summary:

The basic modules of the minesweeper game have been implemented, and the game can start! Of course, this simple minesweeper game can still be optimized, such as adding marking functions, etc. The realization of these modules needs to be explored by everyone, so I won’t elaborate too much. That’s all for this issue, if it helps you, please give it a one-click triple link.