[C language] Realize minesweeper game (expand safe area)

The minesweeper game is that the player enters a coordinate to determine whether the coordinate is a mine. If it is not a mine, it traverses the surrounding 8 coordinates. If there are several mines, it displays the number. If there are no mines in the 8 coordinates, then expand them all until until the coordinates become numbers.

1. Game Design Ideas

  1. Print menu for players to choose;
  2. Use two two-dimensional arrays to store the arranged mines and the detected mines;
  3. Use the function InitBoardo() function to initialize two arrays;
  4. Use the DispalyBoard() function to print the board;
  5. Use the SetMine() function to arrange mines;
  6. Use the FindMine() function to check for mines.

Second, the specific implementation

1. Using multiple files

  • test.c file game framework
  • game.c file game implementation
  • game.h file game function declaration

2. Design of test.c file

  1. Design in the main function

The main function is the entry point of the entire program. Here I use it to write the entire framework of the game. The realization of the game is mainly realized by calling related functions in the main function.

int main()
{
    
    
	int input = 0;
	//用来生成随机值的,布置雷的使用使用
	srand((unsigned int)time(NULL));
	do
	{
    
    
		//打印菜单
		meun();
		printf("请进行选择:>");
		scanf("%d", &input);
		switch (input)
		{
    
    
		case 1:
			//printf("扫雷\n");
			game();
			break;
		case 0:
			printf("退出游戏\n");
		default:
			printf("输入错误请重新输入\n");
			break;
		}
	} while (input);
	return 0;
}
  1. The design of the game() function
void game()
{
    
    
	//布置雷的数组
	char mine[ROWS][COLS];
	//排查雷的数组
	char show[ROWS][COLS];
	//初始化数组
	InitBoard(mine, ROWS, COLS,'0');
	InitBoard(show, ROWS, COLS, '*');
	//打印棋盘
	//DispalyBoard(mine, ROW, COL);
	DispalyBoard(show, ROW, COL);
	//布置雷
	SetMine(mine, ROW, COL);
	//DispalyBoard(mine, ROW, COL);
	//排查雷
	FindMine(show, mine, ROW, COL);
}

The game function is mainly used to implement the logic of the game.

  1. Design of menu() function
void meun()
{
    
    
	printf("******************************\n");
	printf("********    1.paly     *******\n");
	printf("********    0.exit     *******\n");
	printf("******************************\n");
}

Mainly used for printing menus.

  1. overall result
#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

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

void game()
{
    
    
	//布置雷的数组
	char mine[ROWS][COLS];
	//排查雷的数组
	char show[ROWS][COLS];
	//初始化数组
	InitBoard(mine, ROWS, COLS,'0');
	InitBoard(show, ROWS, COLS, '*');
	//打印棋盘
	//DispalyBoard(mine, ROW, COL);
	DispalyBoard(show, ROW, COL);
	//布置雷
	SetMine(mine, ROW, COL);
	//DispalyBoard(mine, ROW, COL);
	//排查雷
	FindMine(show, mine, ROW, COL);
}

int main()
{
    
    
	int input = 0;
	//用来生成随机值的,布置雷的使用使用
	srand((unsigned int)time(NULL));
	do
	{
    
    
		//打印菜单
		meun();
		printf("请进行选择:>");
		scanf("%d", &input);
		switch (input)
		{
    
    
		case 1:
			//printf("扫雷\n");
			game();
			break;
		case 0:
			printf("退出游戏\n");
		default:
			printf("输入错误请重新输入\n");
			break;
		}
	} while (input);
	return 0;
}

3. Design of game.h file

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

#define ROW 9
#define COL 9

#define EASY_COUNT 10

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

//初始化数组
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set);

//打印棋盘
void DispalyBoard(char board[ROWS][COLS], int row, int col);

//布置雷
void SetMine(char board[ROWS][COLS], int row, int col);

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

The purpose of this file is to declare functions.

4. Design of game.c file

  1. Initialize the array using the InitBoard function;

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

When passing parameters, pass the characters that need to be initialized into, which can avoid repeated codes.

  1. Use the DispalyBoard function to print the chessboard;
void DispalyBoard(char board[ROWS][COLS], int row, int col)
{
    
    
	printf("*************扫雷游戏***********\n");
	for (int i = 0; i <= col; 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");
	}
}
  1. Use the SetMine function to arrange mines;
void SetMine(char board[ROWS][COLS], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	int count = EASY_COUNT;
	while (count)
	{
    
    
		x = rand() % row + 1;
		y = rand() % col + 1;

		if (board[x][y] == '0')
		{
    
    
			board[x][y] = '1';
			count--;
		}
	}
	
}
  1. Use the GetMineCount function to count how many mines are around the coordinates;
int GetMineCount(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][y + 1]) 
		- (8 * '0'));
}

It is necessary to traverse the surrounding 8 coordinates and count the number of mines. Here I use the character 1 to arrange the mines. When the character 1 is subtracted from the character 0, the number 1 is obtained, so the 8 coordinates can be added and subtracted. 8 characters 0.

  1. Use the expand function to expand the security area;
void expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int* win)
{
    
    
	if (x >= 1 && x <= ROW && y >= 1 && y <= COL)
	{
    
    
		int count = GetMineCount(mine, x, y);
		if (count == 0)
		{
    
    
			//如果(x,y)坐标四周没有雷,将其改为空格,并且向四周扩散
			show[x][y] = ' ';
			int i = 0;
			for (i = x - 1; i <= x + 1; i++)
			{
    
    
				int j = 0;
				for (j = y - 1; j <= y + 1; j++)
				{
    
    
					//只对*进行递归,防止死循环
					if (show[i][j] == '*')
					{
    
    
						expand(mine, show, i, j, win);
					}
				}
			}
		}
		else  //四周有雷显示雷数
		{
    
    
			show[x][y] = count + '0';
		}
		(*win)++;
	}
}

Recursion is used so that the safe area can be expanded. If there are no thunder in the surrounding eight coordinates, the display is a blank space. As shown in the picture:
image.png

  1. Use the FindMine function to clear mines;
  • Determine whether the coordinates are legal;
  • Determine whether the coordinates are equal to character 1;
  • Count the number of thunders around.
void FindMine(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	int win = 0;
	while (win<row*col-EASY_COUNT)
	{
    
    
		printf("请输入坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && y >= 1 && x <= row && y <= col)
		{
    
    
			if (mine[x][y] == '1')
			{
    
    
				printf("很遗憾你被炸死了。\n");
				DispalyBoard(mine, ROW, COL);
				break;
			}
			else
			{
    
    
				//该坐标不是雷,统计周围雷的个数
				//int count = GetMineCount(mine, x, y);
				//show[x][y] = count + '0';
				expand(mine, show, x, y, &win);
				DispalyBoard(show, ROW, COL);
				//win++;
			}
		}
		else
		{
    
    
			printf("请输入合法的坐标。\n");
		}
	}
	if (win==row*col-EASY_COUNT)
	{
    
    
		printf("恭喜你,赢了!\n");
		DispalyBoard(mine, ROW, COL);
	}
	
}
  1. Overall implementation code.
#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

//初始化数组
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
    
    
	int i = 0;
	for (i = 0; i < rows; i++)
	{
    
    
		int j = 0;
		for ( j = 0; j < cols; j++)
		{
    
    
			board[i][j] = set;
		}
	}
}

//打印棋盘
void DispalyBoard(char board[ROWS][COLS], int row, int col)
{
    
    
	printf("*************扫雷游戏***********\n");
	for (int i = 0; i <= col; 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");
	}
}

//布置雷
void SetMine(char board[ROWS][COLS], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	int count = EASY_COUNT;
	while (count)
	{
    
    
		x = rand() % row + 1;
		y = rand() % col + 1;

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

//统计坐标周围有几个雷
int GetMineCount(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][y + 1]) 
		- (8 * '0'));
}
void expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int* win)
{
    
    
	if (x >= 1 && x <= ROW && y >= 1 && y <= COL)
	{
    
    
		int count = GetMineCount(mine, x, y);
		if (count == 0)
		{
    
    
			//如果(x,y)坐标四周没有雷,将其改为空格,并且向四周扩散
			show[x][y] = ' ';
			int i = 0;
			for (i = x - 1; i <= x + 1; i++)
			{
    
    
				int j = 0;
				for (j = y - 1; j <= y + 1; j++)
				{
    
    
					//只对*进行递归,防止死循环
					if (show[i][j] == '*')
					{
    
    
						expand(mine, show, i, j, win);
					}
				}
			}
		}
		else  //四周有雷显示雷数
		{
    
    
			show[x][y] = count + '0';
		}
		(*win)++;
	}
}


//排查雷
void FindMine(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	int win = 0;
	while (win<row*col-EASY_COUNT)
	{
    
    
		printf("请输入坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && y >= 1 && x <= row && y <= col)
		{
    
    
			if (mine[x][y] == '1')
			{
    
    
				printf("很遗憾你被炸死了。\n");
				DispalyBoard(mine, ROW, COL);
				break;
			}
			else
			{
    
    
				//该坐标不是雷,统计周围雷的个数
				//int count = GetMineCount(mine, x, y);
				//show[x][y] = count + '0';
				expand(mine, show, x, y, &win);
				DispalyBoard(show, ROW, COL);
				//win++;
			}
		}
		else
		{
    
    
			printf("请输入合法的坐标。\n");
		}
	}
	if (win==row*col-EASY_COUNT)
	{
    
    
		printf("恭喜你,赢了!\n");
		DispalyBoard(mine, ROW, COL);
	}
	
}

3. The final effect of the game

image.png

Guess you like

Origin blog.csdn.net/weixin_63284756/article/details/130606595