# C language minesweeping games

Keywords: C

This article will use C language to realize a simple minesweeping game. (the full code is at the end of the article)

① Test logic of the game (its code is stored in test.c);

② The implementation logic of the game (its code is stored in game.c);

③ Declaration of game implementation functions (the declaration of each function is stored in game.h);

The overall implementation idea of the game is as follows:

1. Print out the game menu, enter '1' to start playing the game, and enter '0' to exit the game.

2. Create and print out a 9X9 minesweeping game board.

3. The player inputs the chessboard coordinates. If there are mines under the coordinates, he will lose the game; If there are no mines under the input coordinates, the total number of Mines under the eight adjacent coordinates around the input coordinates is calculated. For example, if you enter X and y, the total number of Mines contained in the eight coordinates [x-1][y-1], [x-1][y], [x-1][y+1], [x][y+1], [x+1][y-1], [x+1][y] and [x+1][y+1] will be calculated. If there are only mines left in the 9X9 coordinates, the player will win

First you have to print the menu on the screen. Define a function called menu. Its content shows that when you enter 1, you start playing the game (PLAY), and when you enter 0, you EXIT (EXIT). The code is stored in test.c.

```void menu()
{
printf("*************************************\n");
printf("*****          1. PLAY          *****\n");
printf("*****          0. EXIT          *****\n");
printf("*************************************\n");
}```

The do while loop structure is adopted. After entering the loop, first execute the menu function, that is, print the menu. Put the input number in the input. When you enter 1, the game starts; Enter 0 to exit the game. The code and running results are as follows:

```int main()
{
int input = 0;
do
{
scanf("%d", &input);
switch (input)
{
case 1:
break;
case 2:
break;
}
} while (input);

return 0;
}``` Enter 1 and enter the game. To realize this function, you need to create a game() function to realize the specific game functions. therefore   Next, the code related to the implementation of game functions is stored in game.c.

First, create an Initboard() function to initialize the chessboard. Generally, the minesweeping game we see has only one chessboard, that is, click any position on the chessboard. If there is a bomb at that position, the game ends; On the contrary, the number of bombs contained around will be displayed at the clicked position. However, in order to realize the game conveniently and quickly, two two-dimensional arrays will be created this time, mine and show. Mine array is used to place bombs and calculate the number of bombs around. The show array is used to display whether there are bombs under the coordinates and the number of bombs around when the player enters the coordinates. This time, 9X9 chessboard is used. It should be noted that if the mine and show arrays are defined as 9X9 arrays, when the bombs are arranged on the outermost layer of the chessboard, the process of calculating the number of bombs around will become very complex. Assuming that one of the bombs is placed in mine, referring to the third step in the game implementation step, it is necessary to calculate the number of bombs contained in the eight surrounding positions. At this time, cross-border access will occur. Therefore, in order to avoid these problems, the mine and show arrays are defined as 11X11 arrays, and only 9X9 spaces are used (that is, 81 positions from arr to arr). At the same time, in order to change the size of the array at any time in the future, the following four constants are defined in game.h: ROWS and COLS are the real size of the array ROWS and columns, while ROW and COL are the actual use size of the array.

```#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS COL + 2```
```//Two functions defined in game.c
#include "game.h"
void Initboard(char board[ROWS][COLS], int rows, int cols, char set)//Used to initialize the chessboard
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void Display_board(char board[ROWS][COLS], int row, int col)//For printing checkerboard
{
int i = 0;
int j = 0;
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("\n");
}```
```void game()
{
char mine[ROWS][COLS] = { 0 };//Define an array for bomb placement (no printing)
char show[ROWS][COLS] = { 0 };//Define an array for players to play games
Initboard(mine, ROWS, COLS, '0');//Initialize the mine array and all elements become '0'
Initboard(show, ROWS, COLS, '*');//Initialize the show array, and all elements become '*'
Display_board(show, ROW, COL);//Print the show array (the print result is a 9X9 chessboard)
}```

Result after executing the game() function: When the chessboard is arranged, you need to place a bomb in the mine array and name it set_ Function of mine, where   Mine_count (for the convenience of changing the number of bombs in the future) refers to the number of bombs placed. Defined in game.h, this design is 10. Because the mine array actually only uses 81 spaces from mine to mine, the values of X and Y are 1 to 9. ```void Set_mine(char mine[ROWS][COLS], int row, int col)
{
int i = 0;
int count = Mine_count;
while(count)
{
int x = rand() % row + 1;//x is the abscissa of the input
int y = rand() % col + 1;//Y is the ordinate of the input
if (mine[x][y] == '0')//At this time, the value range of X and Y is 1 ~ 9
{
mine[x][y] = '1';
count--;
}
}
}```

The last step is for the player to start demining and define   Find_ The mine() function. When players enter coordinates on the chessboard of the show array, if the size of the horizontal and vertical coordinates does not belong to 1~row/col, they will be prompted for input error and require re-entry. When the input is correct, if the position in the mine array is' 1 ', the player will be killed; When it is not a bomb (not '1'), for example, if you input (X,Y), you need to calculate the number of bombs around the coordinate, store the number of bombs in show[x][y] and print it on the chessboard. This process needs to define a named get_ mine_ The count function is used to calculate the number of bombs. Until the game is over or the player is killed.

```int get_mine_count(char mine[ROWS][COLS], int x, int y)//It is used to calculate the number of bombs around and print them on the chessboard
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';
}
void Find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)//
{
int x = 0;
int y = 0;
int count = 0;
while (1)
{
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("I'm sorry, you were killed!\n");
break;
}
else
{
show[x][y] = get_mine_count(mine, x, y) + '0';
Display_board(show, ROW, COL);
count++;
if (count == (row * col-Mine_count))
{
printf("Congratulations on winning the game!\n");
break;
}
}
}
else
{
}
}
}```

Note: to test the feasibility of the program more quickly, the result of being blown up is good, but to see the result of successful demining, 71 coordinates need to be entered. In order to make the test process faster, you can set the number of bombs to 80 (Mine_count is defined as 80), print out the mine array chessboard and find the coordinate that is not the bomb. Enter the coordinate. If the demining is successful, the program is feasible.  All codes

```*****************************************game.h******************************************
#pragma once
# define _CRT_SECURE_NO_WARNINGS 1
#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS COL + 2
#define Mine_count 80
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void Initboard(char board[ROWS][COLS], int rows, int cols, char set);
void Display_board(char board[ROWS][COLS], int row, int col);
void Set_mine(char mine[ROWS][COLS], int row, int col);
void Find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

****************************************game.c*******************************************
#include "game.h"
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;
}
}
}
void Display_board(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("\n");
}
void Set_mine(char mine[ROWS][COLS], int row, int col)
{
int i = 0;
int count = Mine_count;
while(count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
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';
}
void Find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = 0;
while (1)
{
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("I'm sorry, you were killed!\n");
break;
}
else
{
show[x][y] = get_mine_count(mine, x, y) + '0';
Display_board(show, ROW, COL);
count++;
if (count == (row * col-Mine_count))
{
printf("Congratulations on winning the game!\n");
break;
}
}
}
else
{
}
}
}

****************************************test.c*******************************************
#include "game.h"
{
printf("*************************************\n");
printf("*****          1. PLAY          *****\n");
printf("*****          0. EXIT          *****\n");
printf("*************************************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
Initboard(mine, ROWS, COLS, '0');
Initboard(show, ROWS, COLS, '*');
Display_board(show, ROW, COL);
Set_mine(mine, ROW, COL);
Display_board(mine, ROW, COL);
Find_mine(mine, show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 2:
break;
}
} while (input);

return 0;
}

```

Posted by canabatz on Mon, 22 Nov 2021 14:13:41 -0800