Explanation and implementation of mine clearance code in C language

1. Function description of Minesweeper game

Classic Minesweeper gameplay using a console

The game can be continued or exited through the menu

The minesweeper chessboard is a 9*9 grid.

By default, 10 mines are randomly arranged

Can detect mines

If the location is not mine, it will show how many mines are around.

If the location is mine, it will blow up and the game will end.

Find all mines except 10 mines. If the mines are cleared successfully, the game ends.

2 Game analysis and design

Data structure analysis

During the mine clearing process, the information about the mines placed and the mines discharged needs to be stored, so we need a certain data structure to store this information.

0 1 2 3 4 5 6 7 8
0
1
2
3
4
5
6
7
8

Then if mines are arranged at this position, we will store 1, and if there are no mines, we will store 0.

0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
2 0 0 1 0 0 1 0 0 0
3 0 0 0 0 0 0 1 0 0
4 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 1 0 0
6 1 0 1 0 1 0 0 0 0
7 0 1 0 0 1 0 1 0 0
8 0 0 0 0 0 0 0 0 0

test.c–Complete testing of the Minesweeper game

game.c–game module

game.h–game module

//Use menu() to make a menu

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

//Enter the code implementation of the game

int main()
{
int input = 0;
do
{
menu();
printf("Please enter:");
scanf("%d", & amp;input);
switch(input)
{
case 1:
game();
break;
case 0:
printf("Exit the game\
");
break;
default:
printf("Wrong selection, select again!\
");
break;
}

} while(input);
return 0;
}

//There are too many types of data stored, which can easily lead to ambiguity.

//Use two arrays, one to store mine information and one to store the detected mine information.

char show[11][11];//Initialized to *’
char mine[11][11]; // Mine and non-mine
Thunder--1’
Not thunder--0’

//When counting the number of mines around a coordinate, it may cross the boundary.

//Then add a circle of chessboard around it to become 11*11

Change rows and columns to symbols to make subsequent changes easier

 char mine[ROWS][COLS];
char show[ROWS][COLS];
#define ROW 9
#define COL 9

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

Initialize the chessboard

 Initboard(mine, ROWS, COLS,'0');//'0'
Initboard(show, ROWS, COLS,'*');//'*'

Don’t forget to declare the function here

void Initboard(char arr[ROWS][COLS], int rows, int cols, char set);

Code implementation for initializing the chessboard

void Initboard(char arr[ROWS][COLS], int rows, int cols,char set)
{
int i = 0;
\t//OK
for (i = 0; i < rows; i + + )
{
int j = 0;
for (j = 0; j < cols; j + + )
{
arr[i][j] = set;
}
}
}

The following is the code for printing the chessboard

void DisplayBoard(char arr[ROWS][COLS], int row, int col);
DisplayBoard(mine,ROW,COL);//Display the location of mine
DisplayBoard(show, ROW, COL);
void DisplayBoard(char arr[ROWS][COLS], int row, int col)
{
int i = 1;
printf("************Mine Sweeper**********\
");
//Enter the column number first
for (i = 0; i <= row; i + + )
{
printf("%d ", i);
}
printf("\
");
for (i = 1; i <= row; i + + )
{
int j = 0;
printf("%d ", i);
for (j = 0; j < col; j + + )
{
printf("%c ", arr[i][j]);
}
printf("\
");
}
}

//Deploy mines

SetMine(mine, ROW, COL);
void SetMine(char arr[ROWS][COLS], int row, int col);
void SetMine(char arr[ROWS][COLS], int row, int col)
{
//Deploy ten mines
int count = 10;
while(count)
{
//Deploy mines
int x = rand()%row + 1;//1~9
int y = rand()%col + 1;//1~9

//A mine is successfully deployed, count--
if (arr[x][y] == '0')
{
arr[x][y] = '1';
count--;
}
}
}

Check mines

FindMine(mine, show, ROW, COL);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;

int win = 0;
while (win < row * col - 10)
{
printf("Please enter the coordinates to be checked:");
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("Blasted to death\
");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
int n = GetMineCount(mine, x, y);
show[x][y] = n + '0';
DisplayBoard(show, ROW, COL);
win++;
}

}
else
{
printf("The coordinates are illegal, please re-enter\
");
}
}
if (win == row * col - 10)
{
printf("Congratulations, mine clearance is successful\
");
DisplayBoard(show, ROW, COL);
}
}

Summarize

The following is game.h

#pragma once
#include"game.h"
#include<stdio.h>
#include<stdlib.h>
#include<time.h>


#define ROW 9
#define COL 9

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


//Initialize the chessboard
void Initboard(char arr[ROWS][COLS], int rows, int cols, char set);

//Print the chessboard
void DisplayBoard(char arr[ROWS][COLS], int row, int col);

//Deploy mines
void SetMine(char arr[ROWS][COLS], int row, int col);

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

game.c

#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void Initboard(char arr[ROWS][COLS], int rows, int cols,char set)
{
int i = 0;
\t//OK
for (i = 0; i < rows; i + + )
{
int j = 0;
for (j = 0; j < cols; j + + )
{
arr[i][j] = set;
}
}
}

void DisplayBoard(char arr[ROWS][COLS], int row, int col)
{
int i = 1;
printf("************Mine Sweeper**********\
");
//First enter the column number
for (i = 0; i <= row; i + + )
{
printf("%d ", i);
}
printf("\
");
for (i = 1; i <= row; i + + )
{
int j = 0;
printf("%d ", i);
for (j = 0; j < col; j + + )
{
printf("%c ", arr[i][j]);
}
printf("\
");
}
}

void SetMine(char arr[ROWS][COLS], int row, int col)
{
//Deploy ten mines
int count = 10;
while(count)
{
//Deploy mines
int x = rand()%row + 1;//1~9
int y = rand()%col + 1;//1~9

//A mine is successfully deployed, count--
if (arr[x][y] == '0')
{
arr[x][y] = '1';
count--;
}
}
}

static int GetMineCount(char mine[ROWS][COLS],int x,int y)
{
return mine[x - 1][y] + mine[x - 1][y - 1] +
mine[x][y - 1] + mine[x + 1][y - 1] +
mine[x + 1][y] + mine[x + 1][y + 1] +
mine[x][y + 1] + mine[x - 1][y + 1] - 8 * '0';
}

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;

int win = 0;
while (win < row * col - 10)
{
printf("Please enter the coordinates to be checked:");
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("Blasted to death\
");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
int n = GetMineCount(mine, x, y);
show[x][y] = n + '0';
DisplayBoard(show, ROW, COL);
win++;
}

}
else
{
printf("The coordinates are illegal, please re-enter\
");
}
}
if (win == row * col - 10)
{
printf("Congratulations, mine clearance is successful\
");
DisplayBoard(show, ROW, COL);
}
}

test.c

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include"game.h"
void menu()
{
printf("************************\
");
printf("**** 1.play ****\
");
printf("**** 0.exit ****\
");
printf("************************\
");
}

void game()
{
char mine[ROWS][COLS];
char show[ROWS][COLS];
//Initialize the chessboard
Initboard(mine, ROWS, COLS,'0');//'0'
Initboard(show, ROWS, COLS,'*');//'*'
\t
//Print the chessboard
//DisplayBoard(mine,ROW,COL);//Display the location of mine
//DisplayBoard(show, ROW, COL);

//Deploy mines
SetMine(mine, ROW, COL);
DisplayBoard(mine, ROW, COL);//Display the location of mine

//Check mines
FindMine(mine, show, ROW, COL);
\t
}

int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("Please enter:");
scanf("%d", & amp;input);
switch(input)
{
case 1:
game();
break;
case 0:
printf("Exit the game\
");
break;
default:
printf("Wrong selection, select again!\
");
break;
}

} while(input);
return 0;
}

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. C Skill Tree Home Page Overview 194357 people are learning the system