Realization of Minesweeper Game in C Language

It is divided into four steps in general, and each part is divided into many steps

1. Game menu

2. Game content

3. Game implementation

4. The game is "online"

---------------------------------------------------------------------------------------------------------------------------------

1. First of all, let's start with the game menu, just create a function to use printf to output, the code is as follows:

void menu()//游戏菜单
{
	printf("**************************************\n");
	printf("****** 1、进入游戏 *******************\n");
	printf("****** 0、退出游戏 *******************\n");
	printf("**************************************\n");
}

2. (1) Game content, this part requires us to think carefully and build a game() function for preliminary planning of the game. Before proceeding, I need to put it into a loop so that users can keep playing. The initial plan is:

int main()
{
	int input = 0;
	do
	{
		menu();//菜单
		printf("请作出选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();//扫雷游戏
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default :
			printf("你输入有误请重新输入\n");
			break;
		}
	} while (input);
	return 0;
}

 (2) Now to implement the game content, first we need to define two character array components to store mines and check mines,

	char mine[ROWS][COLS] = { 0 };//存放雷
	char show[ROWS][COLS] = { 0 };//排查雷

(3) Then it is the initialization of the array, defining a function initialize()

void initialize(char arr[ROWS][COLS], int rows, int cols, char net)//对棋盘初始化
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			arr[i][j] = net;
		}
	}
}

When no mines are released, the chessboard initialized by mine() is all '0', and the chessboard initialized by the show() function is all '*'. Because the characters of the two initializations are different, they are conveyed by parameters.

(4) The next step is to arrange mines

We use Lei to represent '1', or use random numbers, the code is as follows:

void Setmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	
	int i = minenumble;
	while ( i )
	{
    int x = rand() % row + 1;
	int y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			i--;
		}
	}
}

(5) The next step is to print the chessboard, we still use the function we created

The code to implement this function is as follows:

void DisCopy(char arr[ROWS][COLS], int row, int col)//打印棋盘
{
	int i = 0;
	int j = 0;
	printf("-----------扫雷游戏----------\n");
	for (j = 0; j <= col; j++)
	{
		printf("%d ", j);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", arr[i][j]);
		}
		printf("\n");
	}
}

(6) The last step is mine detection. There are two major cases and two small cases. The first and second cases are that the input conforms to the range and the input does not conform to the range. Our investigation of mines is around that coordinate, that is, the eight coordinates surrounding it. The range is x-1 to x+1, y-1 to y+1. The two small situations are mine clearance success and mine clearance failure. The mine clearance failure is well designed, but the difficulty is that the mine clearance is successful, and we have to return the functions determined around it. At this time, we need to create a Computer() function to design this function. At this point, the game function is basically completed, the code is as follows:

void Findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int count = 0;
	while (count < row * col - minenumble)
	{
		printf("请输入你排查雷的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾,你被雷炸死了\n");
				DisCopy(mine, ROW, COL);
				break;
			}
			else
			{
				
				show[x][y] = Computer(mine, x, y) + '0';
				DisCopy(show, ROW, COL);
				count++;
			}
		}
		else
		{
			printf("输入有误,请重新输入\n");
		}
	}
	if (count == row * col - minenumble)
	{
		printf("恭喜你排雷成功\n");
		DisCopy(mine, ROW, COL);
	}
}

Summary: The general functions of the game are basically completed.

3. The implementation of the game, the code is as follows:

void game()
{
	char mine[ROWS][COLS] = { 0 };//存放雷
	char show[ROWS][COLS] = { 0 };//排查雷

	initialize(mine, ROWS, COLS, '0');//对棋盘初始化,当没有雷时为‘1’
	initialize(show, ROWS, COLS, '*');//对棋盘初始化,全是‘*’

    Setmine(mine,show, ROW, COL);//设置雷

	DisCopy(mine, ROW, COL);//打印棋盘,这个显示只留给测试人员看
	DisCopy(show, ROW, COL);//打印棋盘

	Findmine(mine, show, ROW, COL);

}

4. The overall code of the game:

#include<stdio.h>

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

#define minenumble 5

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



void menu()//游戏菜单
{
	printf("**************************************\n");
	printf("****** 1、进入游戏 *******************\n");
	printf("****** 0、退出游戏 *******************\n");
	printf("**************************************\n");
}

void initialize(char arr[ROWS][COLS], int rows, int cols, char net)//对棋盘初始化
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			arr[i][j] = net;
		}
	}
}

void DisCopy(char arr[ROWS][COLS], int row, int col)//打印棋盘
{
	int i = 0;
	int j = 0;
	printf("-----------扫雷游戏----------\n");
	for (j = 0; j <= col; j++)
	{
		printf("%d ", j);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", arr[i][j]);
		}
		printf("\n");
	}
}

void Setmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	
	int i = minenumble;
	while ( i )
	{
    int x = rand() % row + 1;
	int y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			i--;
		}
	}
}

int Computer(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 Expend(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)
{
	if (x >= 1 && x <= row && y >= 1 && y <= col)
	{
		
		
			int count = Computer(mine, x, y);
			if (count == 0)
			{
				show[x][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] == '*')
							Expend(mine,show,row,col,i,j);
					}
				}
			}
			else
			{
				mine[x][y] =  count + '0';
			}
		
	}
}


void Findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int count = 0;
	while (count < row * col - minenumble)
	{
		printf("请输入你排查雷的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾,你被雷炸死了\n");
				DisCopy(mine, ROW, COL);
				break;
			}
			else
			{
				Expend(mine, show, ROW, COL, x, y);
				//show[x][y] = Computer(mine, x, y) + '0';
				DisCopy(show, ROW, COL);
				count++;
			}
		}
		else
		{
			printf("输入有误,请重新输入\n");
		}
	}
	if (count == row * col - minenumble)
	{
		printf("恭喜你排雷成功\n");
		DisCopy(mine, ROW, COL);
	}
}

void game()
{
	char mine[ROWS][COLS] = { 0 };//存放雷
	char show[ROWS][COLS] = { 0 };//排查雷

	initialize(mine, ROWS, COLS, '0');//对棋盘初始化,当没有雷时为‘1’
	initialize(show, ROWS, COLS, '*');//对棋盘初始化,全是‘*’

    Setmine(mine,show, ROW, COL);//设置雷

	DisCopy(mine, ROW, COL);//打印棋盘,这个显示只留给测试人员看
	DisCopy(show, ROW, COL);//打印棋盘

	Findmine(mine, show, ROW, COL);

}



int main()
{
	srand((unsigned int)time(NULL));//产生随机数的函数,头文件名为#include<stdlib.h>,time()函数头文件名为#include<time.h>
	int input = 0;
	do
	{
		menu();//菜单
		printf("请作出选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();//扫雷游戏
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default :
			printf("你输入有误请重新输入\n");
			break;
		}
	} while (input);
	return 0;
}

Summary: Due to my own level, I may not be so good in some places. Please forgive me. I will learn slowly in the future and strive to write more clearly and understandably. Thank you!

Guess you like

Origin blog.csdn.net/weixin_74967884/article/details/131405632