Explore the classic game: Minesweeper

The last time we made a game of backgammon, this time we have to increase the difficulty a little bit to complete the mini game of minesweeping (link of backgammon: game of backgammon (you can change the size of the board)_in short, it is very good Blog - CSDN blog )

Minesweeper is a classic single-player puzzle game designed to demonstrate the player's logical thinking and reasoning skills by revealing blocks and avoiding mines. This article will introduce the rules and gameplay of the minesweeper game in detail, and provide a simple implementation written in C language with relatively basic functions, so that you can personally experience the process of making a small game 

The source code part is too long and does not take up the length of the article. You can move to my gitee website:  Nerowlp - Gitee.com

Or github website: Nerosts/just-a-try: The process of learning c language, true (github.com) 

Table of contents

 1. Rules and gameplay

2. Game program file description

3. Realization of important functions of Minesweeper game

1.main() main function:

2.menu() function: usually come to print the menu

 3. Come to the game() function according to case 1:

3.1intiBoard () function:

 3.2setMine() function:

3.3 displayBoard () function:

3.4 findMine () function:

4. getMine() function:

5. openBlank () function:

 Four. Summary



 1. Rules and gameplay

The goal of the minesweeper game is to reveal all non-mine squares in a square grid without triggering any mines. Each block can be in one of three states: Unrevealed, Revealed and Marked. Unrevealed squares can be empty squares or mine squares, revealed squares will show the number of mines around.

If the player reveals a mine cube, the game ends and the player loses. The game is won if the player successfully reveals all non-mine squares.


2. Game program file description

  • If the function and constant are declared in game.h, the source file can directly include the header file.
  • The implementation of various functions in game.c: such as initializing the minefield, setting the position of the mine, opening the operation of mine clearance, etc.
  • body.c is the place where various functions are spliced ​​and assembled, and the file where the overall logic of the game is implemented 

 List of constants used


3. Realization of important functions of Minesweeper game

1.main() main function:

The main function is mainly to control the main process of the game - the start of the game and the exit of the game. A while loop is used to meet the needs of the continuous game. When the input is 0, the game is exited and the loop is exited. At the same time, srand() is called function to prepare for later use of the rand() function to generate mine coordinates.

int main()//主函数主要是进行游戏的主体流程的控制——游戏的开始与游戏退出,用一个while的死循环来满足持续进行游戏的需求,input为0时退出游戏并退出循环
{
	srand((unsigned int)time(NULL));
	int input = 1;
	while (input)
	{
		menu();
		printf("请进行选择:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏");
			break;
		default:
			printf("输入有误,请重新输入");
			break;
		}
	}
	return 0;
}

2.menu() function: usually come to print the menu

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

 3. Come to the game() function according to case 1:

  • The intiBoard function is used for initialization, that is, the mine mine array is all initialized to 0, and the show array is all set to # to cover up
  • setMine function to generate and place mines
  • displayBoard function to print the chessboard
  • The findMine function is used for mine clearance operations

It should be noted that although the size of the game is 9*9, the size of our array is 11*11. The extra row and column are for the convenience of subsequent counting of the number of mines in the surrounding 8 grids.

void game()
{
	char mine[ROWS][COLS] = { 0 };//底层雷的数组
	char show[ROWS][COLS] = { 0 };//表面上展示的
	intiBoard(mine, ROWS, COLS,'0');//初始化棋盘的函数
	intiBoard(show, ROWS, COLS, '#');

	
	setMine(mine, ROW, COL);
	displayBoard(show, ROW, COL);
    
	//排除雷
	findMine(mine, show, ROW, COL);
	
}

3.1intiBoard () function:

Perform normal assignment operations, and set one more character parameter for versatility to realize different initialization functions for multiple arrays

void intiBoard(char mine[ROWS][COLS], int rows, int cols,char set)//多设置一个字符参数来实现对多个数组各自不同的初始化的功能
{
	for (int i = 0; i < ROWS; i++)
	{
		for (int j = 0; j < COLS; j++)
		{
			mine[i][j] = set;
		}
	}
}

 3.2setMine() function:

count to control the number of generated mines, rand()%9=1 is to generate random numbers between 1 and 9, because the game area is within the range of 1 to 9 with subscripts of the two-dimensional array

void setMine(char board[ROWS][COLS], int row, int col)
{
	int count = mineNum;
	while (count)
	{
		int x = rand() % 9 + 1;
		int y = rand() % 9 + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}

3.3 displayBoard () function:

Just use the traversal of the two-dimensional array to print, and attach the numerical coordinates of each row and column 

void displayBoard(char board[ROWS][COLS], int rows, int cols)
{
	printf("-------------扫雷------------\n");
	for (int i = 0; i <= ROW; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (int i = 1; i <= ROW; i++)
	{
		printf("%d ", i);
		for (int j = 1; j <= COL; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("-------------扫雷------------\n");
}

 It is convenient for players to input coordinates

3.4 findMine () function:

We entered the coordinates to play, and the Lei game failed directly.

The number of mines in the surrounding 8 grids will be displayed if no mines are discharged. We use the getMine function to calculate this number. If we want to win the game, we have to check all the non-mine parts completely, which is too many times, so the openBlank function is designed to help the expansion: if there are no mines around, expand the 3*3 area.

void findMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x, y;
	int win = 0;
	while (win<row*col-mineNum)
	{
		printf("请输入排查雷的坐标:\n");
		scanf("%d %d", &x, &y);
		
			if (mine[x][y] == '1')
			{
				printf("很遗憾,排到了雷,被炸了。雷的位置如下(数字1代表雷)\n");
				displayBoard(mine,ROW, COL);
				break;
			}
			else
			{
				int c = getMine(mine, x, y);
				show[x][y] = (char)c;
				openBlank(mine,show, x, y);
				displayBoard(show, ROW, COL);
				win++;
			}
	}
	if (win == row * col - mineNum)
	{
		printf("恭喜你,赢了");
		displayBoard(mine, ROW, COL);
	}
}

4. getMine() function:

Because we can directly calculate with only 8 grids 

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

5. openBlank () function:

Use traversal: "expand" 

void openBlank(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
	if (mine[x][y] == '0')
	{
		for (int i = x - 1; i <= x + 1; i++)
		{
			for (int j = y - 1; j <= y + 1; j++)
			{
				show[i][j] = ' ';
				
			}
		}
		mine[x][y] = ' ';
	}
}

 The effect is shown in the figure


 Four. Summary

In the implementation of this minesweeper game, we use multiple functions to complete different functions. We first initialize the game board, then randomly place mines and count the number of mines around each square. Next, we can perform game operations by revealing blocks or marking blocks. We also implement functions to determine if the game is over and win, and print the game board when the game is over.

Compared with the last three-game mini-game, the implementation of this minesweeper game is more complicated. It involves more logic and functionality, such as mine placement and counting the number of surrounding mines. At the same time, the game board of the minesweeper game is also more complex, including multiple blocks and different states of the blocks.

However, both games have something in common. They all use functions to complete different functions, and both need to judge whether the game is over and whether it is a victory. Additionally, they both can display the state of the game by printing the game board.

In general, these two games are small games based on logic and judgment. By implementing different functions, you can complete various operations of the game, and perform game judgment and printing. The realization of these small games can help us better understand and practice the basic concepts and skills of programming.

Finally, thank you for your support! ! !

Guess you like

Origin blog.csdn.net/qq_74415153/article/details/131980227