[C Language Foundation 7 - Array (3) Minesweeper]


foreword

This article then reviews the knowledge learned earlier, taking the minesweeper game as an example.


1. What is mine sweeping?

Baidu Encyclopedia : "Minesweeper" is a popular puzzle game, released in 1992. The goal of the game is to find out all the non-mine grids in the shortest time according to the numbers that appear in the clicked grids, and at the same time avoid stepping on the thunder, stepping on a thunder will lose the whole game.
insert image description here

2. Program framework

The overall framework of the program can be adapted from the one in the previous section, and this framework can also be used as a general form.

2.1 Main function

int main()
{
    
    
	int input = 0;
	srand((unsigned int)time(NULL));//产生随机数
	do
	{
    
    
		menu();//菜单提示
		printf("请输入 ==> ");//输入1或0,
		scanf("%d", &input);
		switch (input)//根据输入选择是否玩游戏
		{
    
    
		case 1:
			game();//玩游戏的具体实现
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新选择!\n");
			break;
		}
	} while (input);

	return 0;
}

2.2 Function menu

Output the prompt menu to remind the player, 1 is to play the game, 0 is to quit the game

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

2.3 Function game

array mine , initialized with the character '0'

  • The mine array is followed by 10 mines, the position with mines is represented by the character '1', and the position without mines is still the character '0'
  • The locations of 10 mines are randomly generated

Array show , initialized with characters '*'

  • The character '*' is to block the position where the mine is generated, so that the player cannot see it
  • The show array puts information about mines around specific coordinates into the chessboard
  • If there are mines around the coordinates, the number of mines will be counted and displayed on this coordinate
void game()
{
    
    
	printf("开始玩游戏!\n");
	//扫雷游戏的实现
	//mine数组是用来存放布置好的雷的信息
	//就10个雷在什么位置
	char mine[ROWS][COLS] = {
    
     0 };//'0'
	//show数组是用来存放排查出的雷的信息
	//坐标周围有几个雷
	char show[ROWS][COLS] = {
    
     0 };//'*'

	//初始化棋盘
	init_board(mine, ROWS, COLS, '0');
	init_board(show, ROWS, COLS, '*');
	//打印棋盘
	//show_board(mine, ROW, COL);//全是字符'0'
	//show_board(mine, ROW, COL);//全是'*'
	//布置雷
	set_mine(mine, ROW, COL);//雷的数组
	//show_board(mine, ROW, COL);这是显示10个雷在哪里
	show_board(show, ROW, COL);//输出*暂时掩盖雷在哪里
	//排查雷
	find_mine(mine, show, ROW, COL);

	
}

2.3.2 Function init_board

The init_board initialization parameter is to initialize the chessboard so that the entire chessboard displays the characters '0' and '*'

//初始化棋盘    参数:行数 列数  行数 列数 字符0或* 
void init_board(char arr[ROWS][COLS], int rows, int cols, char set)
{
    
    //set表示初始化传进来的字符是0 还是 *
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
    
    
		for (j = 0; j < cols; j++)
		{
    
    
			arr[i][j] = set;
		}
	}
}

2.3.3 Function show_board

show_board is to display the chessboard, you can see the information of the mines in the chessboard, as well as the specific state of the chessboard during subsequent mine sweeping

//展示棋盘
void show_board(char arr[ROWS][COLS], int row, int col)
{
    
    
	int i = 0;
	int j = 0;
	printf("------------扫雷------------\n");
	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 ", arr[i][j]);//打印棋盘每个元素
		}
		printf("\n");
	}
	printf("------------扫雷------------\n");
}

2.3.4 Function set_mine

The function set_mine arranges mines, which will randomly generate mines at 10 coordinate positions in the chessboard.

//布置雷
void set_mine(char mine[ROWS][COLS], int row, int col)
{
    
    
	int count = EASY_COUNT;//布置10个雷
	int x = 0;//行坐标
	int y = 0;//列坐标
	while (count)//直到10个雷布置完成,退出循环
	{
    
    
		x = rand() % row + 1;//取模是0-8,加1就是1-9
		y = rand() % col + 1;
		if (mine[x][y] == '0')//是空的,就放雷,否则重新随机产生坐标位置
		{
    
    
			mine[x][y] = '1';//布置雷
			count--;
		}
	}
}

2.3.5 Function find_mine

The function find_mine is to check for mines. Every time the player clears a mine, he first enters a coordinate, and then judges whether the character on the coordinate is 1:

  • 1 is mine, the game is over
  • Not 1, count the number of mines in 8 positions around the coordinates, and display them on the coordinates in the form of characters
//排查雷
void find_mine(char mine[ROWS][COLS], char show[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 && x <= row && y >= 1 && y <= col)//在1-9的坐标范围内
		{
    
    
			if (mine[x][y] == '1')//确定坐标为字符'1',就是雷
			{
    
    
				printf("很遗憾,被炸死了\n");
				show_board(mine, ROW, COL);//显示所有雷的位置
				break;
			}
			else//不是字符1,坐标就不是雷,显示坐标周围有雷的个数
			{
    
    
				int count = get_mine_count(mine, x, y);//函数计算类的个数
				show[x][y] = count + '0';//周围有雷的个数+'0'就转换成字符了
				show_board(show, ROW, COL);//打印出来,每次扫雷后的棋盘
				win++;//扫了一次雷就++
			}
		}
		else//超过坐标范围
		{
    
    
			printf("坐标非法,重新输入\n");
		}
	}
	if (win == row * col - EASY_COUNT)//扫雷次数==9*9-10 71次就结束
	{
    
    
		printf("恭喜你,排雷成功\n");
		show_board(mine, ROW, COL);//显示雷的信息
	}
}

2.3.6 Function get_mine_count

The function get_mine_count counts the number of mines:

  • The character '1' means there is a thunder, the character '0' means no, and '1'-'0' is the number 1, representing 1 thunder
  • Add the characters on the 8 coordinates around the coordinates - 8 * '0', the result is the number of mines, which is an integer
//统计坐标周围有雷的个数
int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
    
    //坐标周围的8个地方减去'0',再相加的个数就是类的个数
	return mine[x - 1][y] +
		mine[x - 1][y - 1] +
		mine[x][y - 1] +
		mine[x + 1][y - 1] +
		mine[x + 1][y] +
		mine[x + 1][y + 1] +
		mine[x][y + 1] +
		mine[x - 1][y + 1] - 8 * '0';
}

3. Header file.h

#include <stdio.h>
#include <stdlib.h>//库函数
#include <time.h>//与系统时间相关

#define ROW 9//棋盘真实的行数
#define COL 9
#define ROWS ROW+2 //棋盘放大范围,便于棋盘边的位置遍历
#define COLS COL+2
#define EASY_COUNT 10 //10个雷的个数
//初始化
void init_board(char arr[ROWS][COLS], int rows, int cols, char set);
//打印
void show_board(char arr[ROWS][COLS], int row, int col);
//布置雷
void set_mine(char mine[ROWS][COLS], int row, int col);

4. Play the game

The running results are shown in the figure below, which basically satisfies the game functions.
insert image description here

The complete code is in gitee:

C Language Foundation 7 - Array (3) Complete Code of Minesweeper


Summarize

This article only covers the more basic minesweeping games. After learning more complex knowledge later, the minesweeping games can be improved. The overall writing idea of ​​the minesweeper game can be realized with reference to the three-piece game.

The knowledge related to arrays is basically over, and the next article starts to learn the knowledge related to operators.

Guess you like

Origin blog.csdn.net/taibudong1991/article/details/123906619