C language to achieve minesweeping [super detailed explanation]

Table of contents

1. The basic idea of ​​minesweeping

Two, the specific steps of code implementation

3. Complete code

1, saolei.h part

2. saolei.c part

3. part of test.c


There are many similarities between minesweeper and backgammon . I believe that after studying the game of backgammon carefully and then studying this blog carefully, you will have a good grasp of relevant knowledge and a mastery of it.


1. The basic idea of ​​minesweeping

1. Create a menu

2. Realize the switch case statement, choose to play the game or exit the game

3. Choose to play the game and realize the code of the game

4. The game part creates two two-dimensional arrays, mine and show arrays . The mine array is an array of bombs , the position with a bomb is 1, and the position without a bomb is 0; the show array is an array for finding bombs , the unknown position is '*', and the number of the found position is the number of bombs around number

5. Initialize the two two-dimensional arrays, the mine array is initialized to '0', and the show array is initialized to '*'

6. Print the chessboard, print the chessboard once every time you input the coordinates, so that you can observe

7. Realize the operation of arranging mines: rand (header file: stdlib.h) and time (header file: time.h) are required as in backgammon , and combined use can make the computer randomly arrange mines

8. Realize the operation of mine detection: the player enters a coordinate, if the mine is stepped on, it will remind the game to end; if the mine is not stepped on, the number of mines in the surrounding circle will be displayed in the corresponding position in the show array

9. After the game is over, continue to print the selected menu, and the player decides whether to continue the game


Two, the specific steps of code implementation

1. Create three sections as shown below:

2. Also refer to the header file in saolei.h , and only need to refer to saolei.h in saolei.c and test.c



3. Create the switch case statement of the main function and the test function in test.c

 

4. Create a simple menu

5. The mine-sweeping standard is 9 rows and 9 columns. However, when calculating the number of mines around the side coordinates, it is necessary not to include the out-of-bounds areas, so expanding the two arrays by a circle will not cause trouble in this regard. Now, it becomes a two-dimensional array with 11 rows and 11 columns. Use the macro definition to define ROW9, COL9, ROWS11, COLS11 , and the number of mines set MINE_COUNT10

6. The implementation of the game function in test.c (still complete the framework first, and then implement it

7. Implement the requirements of the game function in saolei.c ( declare in saolei.h and implement in saolei.c ), the first picture below is the declaration, and the second picture is the realization of the function 

(1), the implementation of the array initialization function


(2), print chessboard

On the top and left of the 9×9 chessboard, print a row and a column of numbers respectively , so that players can read the exact coordinates of which row and column

 (3) Lay out mines

Add in the test function:



(4) Check for mines


In the mine detection function, you need to use the function Find_count to count the number of mines , as follows:


3. Complete code

1, saolei.h part

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

#define ROWS 11
#define COLS 11

#define ROW 9
#define COL 9

#define MINE_COUNT 10


//数组的初始化函数
void Init_board(char arr[ROWS][COLS], int rows, int cols, char set);

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

//布置地雷的函数
void Set_mine(char mine[ROWS][COLS], int row, int col);

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

2. saolei.c part

#include "saolei.h"

//数组初始化函数的实现
void Init_board(char arr[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++)
		{
			arr[i][j] = set;
		}
	}
}

//打印棋盘
void Display_board(char arr[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 ", arr[i][j]);
		}
		printf("\n");
	}
}


//布置地雷函数
void Set_mine(char mine[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int ret = MINE_COUNT;
	while (ret)
	{
		x = rand() % 9 + 1;
		y = rand() % 9 + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			ret--;
		}
	}
}



//统计雷的个数
int Find_count(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 - 1][y] +
		mine[x + 1][y + 1] +
		mine[x][y + 1] +
		mine[x - 1][y + 1] - 8 * '0';
}


//排查地雷
void Find_mine(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 - MINE_COUNT)
	{
		printf("请输入想要排查的坐标:");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾,你被炸死了!\n");
				break;
			}
			else
			{
				count++;
				//Find_count是统计周围雷的个数的函数
				int ret = Find_count(mine, x, y);
				show[x][y] = ret + '0';
				//输入坐标正确后,再打印一次棋盘,方便玩家观察
				Display_board(show, ROW, COL);
			}
		}
		else
		{
			printf("坐标输入错误,请重新输入!\n");
		}
	}
	if (count == row * col - MINE_COUNT)
	{
		printf("恭喜你,排雷成功!\n");
	}
}

3. part of test.c

#include "saolei.h"

void game()
{
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };

	//两个数组的初始化
	//mine数组元素全部初始化为字符0
	//show数组元素全部初始化为字符*
	Init_board(mine, ROWS, COLS, '0');
	Init_board(show, ROWS, COLS, '*');


	//打印棋盘,以便让玩家可以清楚的选择相应的位置对应的坐标
	Display_board(show, ROW, COL);


	//布置地雷
	Set_mine(mine, ROW, COL	);

	//排查地雷
	Find_mine(mine, show, ROW, COL);
	//两个数组都传入是因为排查时两个数组元素都会有相应的改变
	//排查结束,再打印一次地雷棋盘,向玩家展示地雷的位置
	Display_board(mine, ROW, COL);
}


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


void test()
{
	int input = 0;
	do
	{
		menu();
		srand((unsigned int)time(NULL));
		printf("请选择:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出扫雷!\n");
			break;
		default:
			printf("输入错误,请重新输入!\n");
			break;
		}
	} while (input);
}

int main()
{
	test();
	return 0;
}

The above is all the content of minesweeping in C language, see you in the next blog ヾ( ̄▽ ̄)Bye~Bye~

Guess you like

Origin blog.csdn.net/m0_64411530/article/details/125576699