How to write a minesweeper game with code

To implement a minesweeping game with code also requires writing more than two hundred lines of code, which is not a small amount of engineering, so in order to make the logic clearer, we still create three files to write the code like writing a three-middle chess game.
1 header file (game.h), 2 .c files (game.c and test.c).
The game.h file mainly writes some preprocessing information, some header files of existing functions that need to be used, and declarations of custom functions.

#define _CRT_SECURE_NO_WARNINGS 1
//地雷的总数
#define COUNT 10

#define ROW 9//扫雷的行数
#define COL 9//扫雷的列数

#define ROWS ROW+2//需要创建的二维数组的行数
#define COLS COL+2//需要创建的二维数组的列数

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

//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col);
//布置雷
void SetMine(char mine[ROWS][COLS], int row, int col, int count);
//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

Use #define to define the number of rows and columns of minesweeper and the total number of mines, so that when we want to modify the data in the future, we only need to modify the data behind #define, instead of changing the code all over.

The game.c file also mainly writes the specific definition of the custom function in game.h.

#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 DisplayBoard(char board[ROWS][COLS], int row, int col)
{
    
    
	int i = 0;
	int j = 0;
	printf("--------------扫雷游戏----------------\n");
	for (i = 0; i <= row; 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 SetMine(char mine[ROWS][COLS], int row, int col,int count)
{
    
    
	while (count)
	{
    
    
		int x = rand() % ROW + 1;
		int y = rand() % COL + 1;
		if (mine[x][y] == '0')
		{
    
    
			mine[x][y] = '1';
			count--;
		}
	}
}
//计算该坐标周围8个坐标存在的总雷数
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
    
    
	int i = 0;
	int sum = 0;
	for (i = x - 1; i <= x + 1; i++)
	{
    
    
		int j = 0;
		for (j = y - 1; j <= y + 1; j++)
		{
    
    
			sum += mine[i][j];
		}
	}
	return sum - 9 * '0';
}
//判断周围雷的数量为0的坐标周围雷的存在情况,实现扫雷中的展开一片现象
void JudgeAround(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y)
{
    
    
	int i = 0;
	int j = 0;
	for (i = x - 1; i <= x + 1; i++)
	{
    
    
		for (j = y - 1; j <= y + 1; j++)
		{
    
    
			if (show[i][j] != ' '&&i!=0&&i!=ROWS-1&&j!=0&&j!=COLS-1)
			{
    
    
				int count = GetMineCount(mine, i, j);
				show[i][j] = count + '0';
				if (show[i][j] == '0')
				{
    
    
					show[i][j] = ' ';
					JudgeAround(mine, show, i, j);
				}
			}
		}
	}
}
//计算剩下未知坐标的数量,当其等于雷的总数时表示扫雷成功
int Remain(char show[ROWS][COLS], int row, int col)
{
    
    
	int i = 0;
	int j = 0;
	int count = 0;
	for (i = 1; i <= row; i++)
	{
    
    
		for (j = 1; j <= col; j++)
		{
    
    
			if (show[i][j] == '*')
				count++;
		}
	}
	return count;
}
//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
    
    
	int ret = 0;
	while (ret!=COUNT)
	{
    
    
		printf("请输入要排查的坐标:>");
		int x = 0;
		int y = 0;
		scanf("%d%d", &x, &y);
		//判断输入坐标合法性
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
    
    
			//判断该坐标处是不是雷
			if (mine[x][y] == '1')
			{
    
    
				printf("很遗憾,你被炸死了!\n");
				DisplayBoard(mine, ROW, COL);
				break;
			}
			else
			{
    
    
				//不是雷,统计该坐标周围雷的个数
				int count = GetMineCount(mine, x, y);
				show[x][y] = count + '0';//存放的是数字字符
				if (show[x][y] == '0')//如果该坐标周围雷的数量为0
				{
    
    
					show[x][y] = ' ';
					JudgeAround(mine, show, x, y);
				}
				system("cls");
				DisplayBoard(show, ROW, COL);
			}
		}
		else
		{
    
    
			printf("坐标非法,请重新输入!\n");
		}
		ret = Remain(show, ROW, COL);
	}
	if (ret==COUNT)
	{
    
    
		printf("恭喜你,排雷成功!\n");
		DisplayBoard(mine, ROW, COL);
	}
}

Print out the horizontal and vertical coordinates when printing the chessboard, so that players can better know the coordinates of the mines they want to clear. Among them, I think it is more difficult to write a function that imitates the expansion of a piece of minesweeper game (when there are no mines around the coordinate), it is more difficult to write. Function recursion is used, and boundary issues need to be judged. It is worthy of our consideration.

The main code of the minesweeper game is mainly written in the test.c file.

#include"game.h"
//菜单
void menu()
{
    
    
	printf("|----------------------|\n");
	printf("|        1.play        |\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, '*');
	//打印棋盘
	//DisplayBoard(mine, ROW, COL);
	DisplayBoard(show, ROW, COL);
	//布置雷
	SetMine(mine, ROW, COL, COUNT);
	//DisplayBoard(mine, ROW, COL);
	//排查雷
	FindMine(mine, show, ROW, COL);
}

int main()
{
    
    
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
    
    
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
    
    
		case 1:
			system("cls");
			game();//扫雷游戏的实现
			break;
		case 0:
			printf("退出游戏!\n");
			break;
		default:
			printf("选择错误,请重新选择!\n");
			break;
		}
	} while (input);
	return 0;
}

It is worth noting that the two-dimensional array that we use to store the board information needs to be a two-dimensional array of char type, because when there is no demining at the beginning, what we show to the player should be a chessboard with all *, not an int type two Dimensional array storage.

I think the minesweeper game written by this code is no different from the one on the computer. There are no bugs. Interested bloggers can copy it to their compiler to play.

Guess you like

Origin blog.csdn.net/chenlong_cxy/article/details/113125481