# catalogue

Game analysis

Game module

2. Create two checkerboards (character arrays)

3. Initialize chessboard (character array)

4. Print checkerboard and row / column serial number

5. Lay thunder

6. Demining

Complete code

summary

I believe many people have played the game of mine sweeping, but few friends should study it deeply. Most people play this game just to pass the time, but when you really study this game, you will find that this game is actually very interesting.

Friends interested in this game or beginners interested in programming must also want to use their own knowledge to realize this game.

Enter the theme ---- use C language to simply realize the game

# Game analysis

Before implementing the game, analyze the layout of the game.

The primary minesweeping is a 9 * 9 layout (hereinafter referred to as the chessboard), which uses a two-dimensional array to create a chessboard.

Simple description of minesweeping game playing method

After clicking on a grid, if there is no thunder, a number or a blank grid will be displayed. If a number 3 is displayed, it means that there are 3 mines in the 8 grids around this number. Based on this information, the approximate location of the mine can be analyzed.

Due to my limited strength, I can't use mouse click to mine. Only the input coordinates can be realized. • Create two character arrays, one for storing the information of arranged mines and the other for storing the information of troubleshooting mines.
• The character '0' indicates that it is not mine, the character '1' indicates that it is mine, and the character '*' indicates that it is not mine.
• When entering coordinates for demining, if there are mines nearby, the number of nearby mines will be displayed at the entered coordinate position.
• In order to maintain the sense of mystery and hide Ray's information, the chessboard only displays the character '*'.
• Enter the coordinates. If the location is not a mine, the information of the surrounding mine will be displayed at the location. If there is no mine around the coordinates, a space will be displayed. If there is thunder in this position, it will output that you have been killed and display the checkerboard of thunder information. The game is over. If all thunder are checked out, you win and the game is over.
• When checking the mines around the chessboard, counting the number of mines will cause the array to cross the boundary. Therefore, expand the two arrays by one circle, count the 9 * 9 chessboard, and set the two arrays to 11 * 11, so that the array will not cross the boundary when calculating the thunder around the chessboard.
• When inputting coordinates, the position of the coordinates in the chessboard cannot be quickly determined, so the row and column serial numbers of the chessboard are printed around the chessboard. Because the size of the array is set to 11 * 11 and the size of the chessboard is only 9 * 9, the row column serial number can be printed at the extra position of the array when printing the chessboard. Code Planning: create three source files

game.h   : Stored are library function references, macro definitions, function declarations and other code

game.c: store the code of function implementation

main.c: Code for storing function calls

# Game module

```#include"game.h"

{
printf("*********************************\n");
printf("**********   1. play   **********\n");
printf("**********   0. exit   **********\n");
printf("*********************************\n");

}

void game()
{
printf("The Minesweeper game\n");
}

void test()
{
int input = 0;
do {
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("Exit the game\n");
break;
default:
break;
}
} while (input);
}

int main()
{
test();
return 0;
}
```

test result ## 2. Create two checkerboards (character arrays)

• Mine: store the information of the arranged mine
• show: store the information of mine detection

Use the macro definition to set the size of the array

```//Array representing actual operation 9 * 9
#define ROW 9
#define COL 9

//Represents the array to be created 11 * 11
#define ROWS ROW+2
#define COLS COL+2```

Use the macro definition to set the size of the array to facilitate the later change of the size of the array. If you want to change the size of the actual operation in the chessboard to 16 * 16, you can directly modify the parameters defined by the macro.

## 3. Initialize chessboard (character array)

The chessboard storing the information of mine arrangement is initialized as' 0 ', and the chessboard storing the information of mine detection is initialized as' *'

Create InitBoard function to initialize checkerboard (array).

```//Initialize chessboard
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}

```

## 4. Print checkerboard and row / column serial number

Create DisplayBoard function to print chessboard. Because the area for playing the game is 9 * 9, you only need to print the 9 * 9 chessboard.

```//Print the chessboard. Because the array size passed by the argument is rows and cols, the size of the received array parameter should be consistent with it
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
//Print column number
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
//1~9
for (i = 1; i <= row; i++)
{
//Print line number
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}```

test result Essentially, the show array should be printed, because all you see when playing the game should be '*',

## 5. Lay thunder

Ten random numbers of 1 ~ 9 are generated by rand function and arranged in a 9 * 9 chessboard. Assign the position where the random number is generated to '1'.

```//Lay thunder
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = 10;

while (count)
{
int x = rand() % row + 1;//Random numbers from 1 to 9
int y = rand() % col + 1;//Random numbers from 1 to 9
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}```

Test results (the figure here is the optimized chessboard, followed by all the codes) ## 6. Demining

Mine clearance is to find mines in the chessboard where mine information is arranged, and then display the mine information in the chessboard for mine screening. The process of demining involves two arrays, and the operation is a 9 * 9 grid.

Demining (method I)

```//mine clearance
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- COUNT)
{
printf("Please enter the coordinates to check:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("You stepped on thunder, the game is over!\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
//Calculate the number of mines around X and Y coordinates
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';//Convert the calculated number of mines into characters
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("The coordinates you entered are illegal and cannot be cleared!\n");
}
}
if (win == row * col - COUNT)
{
DisplayBoard(mine, ROW, COL);
}
}```

This way of writing can not realize one coordinate. Now there is no effect of direct expansion of thunder in the surrounding 8 grids.

Supplement: the number plus character 0 ('0 ') can be converted to the corresponding number

For example: 2 + '0' ='2 ';

Reason: the ASCII code of '0' is 48. 2 + '0' is actually the ASCII code value of 2 and character 0 - > 48 + 2 = 50, while the ASCII code value of character '2' is 50.

Demining (II)

Recursion is used to realize the effect of direct expansion if there is no thunder in the surrounding 8 grids

Recursive conditions:

(1) This coordinate is not mine

(2) There are no mines around this coordinate

(if the coordinate is not a mine and there is no mine around the coordinate, set the coordinate as a space.)

(3) This coordinate has not been checked

If these three conditions are met, use recursion to expand the troubleshooting.

```//Realize the effect of directly expanding if there is no thunder in the surrounding 8 grids
void blank(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
int i = 0;
int j = 0;
//Count the number of Mines
int count = get_mine_count(mine, x, y);
if (count == 0)
{
show[x][y] = ' ';

for (i = x-1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (i>0 && i<ROWS && j>0 && j<COLS && mine[i][j]!='1' && show[i][j]=='*')
{
blank(mine, show, i, j);
}
}
}
}
else
{
show[x][y]=count+'0';
}
}
```

If recursive expansion is used, there will also be some changes in the function of Lei

```void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int i = 0;
int j = 0;
while (1)
{
printf("\n Please enter the coordinates to check:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("You stepped on thunder, the game is over!\n");
printf("\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
//Calculate the number of mines around X and Y coordinates
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';//Convert the calculated number of mines into characters
blank(mine, show, x, y);
DisplayBoard(show, ROW, COL);//Display the number of thunder on the show chessboard
}
}
else
{
printf("The coordinates you entered are illegal and cannot be cleared!\n");
}
//Count the number of Mines ('*')
int win = 0;
for (i = 1; i <= ROW; i++)
{
for (j = 1; j <= COL; j++)
{
if (show[i][j] == '*')
{
win++;
}
}
}
if (win == COUNT)
{
break;
}
}
}
```

test result Some basic functions of minesweeping game have been realized, but this code can be improved.

For example, make sure that what is found during the first demining is not mine.

# Complete code

game.h

```#pragma once

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

//Array representing actual operation 9 * 9
#define ROW 9
#define COL 9

//Represents the array to be created 11 * 11
#define ROWS ROW+2
#define COLS COL+2

//Number of thunder
#define COUNT 10

//Declare initialization checkerboard function
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);

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

//Lay thunder
void SetMine(char mine[ROWS][COLS], int row, int col);

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

//Recursive implementation to expand the blank lattice
void blank(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
```

game.c

```#include"game.h"

//Initialize chessboard
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}

//Print the chessboard. Because the array size passed by the argument is rows and cols, the size of the received array parameter should be consistent with it
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
//Print column number
for (i = 0; i <= col; i++)
{
printf("%-4d", i);
}
printf("\n");

//Print the border line on the chessboard
printf("  ");
for (j = 1; j <= col; j++)
{
printf("|---");
}
printf("|\n");

//Print chessboard (1 ~ 9 -- > 9 * 9)
for (i = 1; i <= row; i++)
{
//Print line number
printf("%d", i);
for (j = 1; j <= col; j++)
{
printf(" |");//Print vertical lines in the chessboard
printf(" %c", board[i][j]);
}
printf(" |");//Print the vertical line of the last line
printf("\n");

//Print horizontal lines in the chessboard
printf("  ");
for (j = 1; j <= col; j++)
{
printf("|---");
}
printf("|\n");
}
}

//Lay thunder
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = COUNT;

while (count)
{
int x = rand() % row + 1;//Random numbers from 1 to 9
int y = rand() % col + 1;//Random numbers from 1 to 9
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}

//Count the number of Mines
int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
return
mine[x - 1][y - 1] +
mine[x - 1][y] +
mine[x - 1][y + 1] +
mine[x][y - 1] +
mine[x][y + 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] - 8 * '0';
}

//mine clearance
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int i = 0;
int j = 0;
while (1)
{
printf("\n Please enter the coordinates to check:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("You stepped on thunder, the game is over!\n");
printf("\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
//Calculate the number of mines around X and Y coordinates
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';//Convert the calculated number of mines into characters
blank(mine, show, x, y);
//system("cls");// Clear screen
DisplayBoard(show, ROW, COL);//Display the number of thunder on the show chessboard
}
}
else
{
printf("The coordinates you entered are illegal and cannot be cleared!\n");
}
//Count the number of Mines ('*')
int win = 0;
for (i = 1; i <= ROW; i++)
{
for (j = 1; j <= COL; j++)
{
if (show[i][j] == '*')
{
win++;
}
}
}
if (win == COUNT)
{
break;
}
}
}

//Realize the effect of directly expanding if there is no thunder in the surrounding 8 grids
void blank(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
int i = 0;
int j = 0;
int count = get_mine_count(mine, x, y);
if (count == 0)
{
show[x][y] = ' ';

for (i = x-1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (i>0 && i<ROWS && j>0 && j<COLS && mine[i][j]!='1' && show[i][j]=='*')
{
blank(mine, show, i, j);
}
}
}
}
else
{
show[x][y]=count+'0';
}
}
```

main.c

```#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

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

void game()
{
//Create array
char mine[ROWS][COLS];//Store the information of the arranged mine
char show[ROWS][COLS];//Storage of mine detection information

//Initialize mine array to full character '0'
InitBoard(mine,ROWS,COLS,'0');
//Initialize the show array to the full character '*'
InitBoard(show,ROWS,COLS,'*');

//Lay thunder
SetMine(mine,ROW, COL);
//Print chessboard
DisplayBoard(show, ROW, COL);

//After arranging the thunder, you can see the position of the thunder
//Chessboard for printing mine information
//DisplayBoard(mine, ROW, COL);

//mine clearance
FindMine(mine,show, ROW, COL);

}

void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do {
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("Exit the game\n");
break;
default:
break;
}
} while (input);
}

int main()
{
test();
return 0;
}
``` # summary

The game can change the size of macro definition parameters in the header file to change the size of the chessboard or modify the number of thunder, making the game more challenging. You can also print out the checkerboard of mine information and play a cracked version of mine sweeping.

Using C language to realize minesweeping game, mainly uses the knowledge of array, function, selection statement, loop and so on in C language. Tags: C Game Development

Posted on Fri, 12 Nov 2021 19:14:36 -0500 by alexcmm