【C】C Language Kingdom Minesweeper Game

①Foreword

Minesweeper is a game suitable for all ages.
The gameplay is to randomly arrange a certain number of mines in a 9×9 (beginner), 16×16 (intermediate), 16×30 (advanced) or custom-sized square matrix (beginner). (10 for intermediate, 40 for intermediate, and 99 for advanced). Players then turn over the blocks one by one to find all the mines as the final game goal. If the player turns over a block that contains a mine, the game is over.
After understanding how it is played, let us think about how to implement it in C language? (Take 9*9 as an example)

Insert image description here

② Game implementation steps

1. Create a menu menu
2. Define two char type arrays mine[ ] and show[ ]. The
mine array is used to store mine position information, and the show array is used to store information about the number of mines around non-mine positions.
3. Right Initialize the mine and show arrays.
The mine array is all initialized to character 0 , and the show array is all initialized to character *.
4. Arrange the mine and store the information in the mine array. The position of the mine is changed from character 0 to character 1.
5. Mine removal
6. Print array

1. Make menu menu

The menu interface is like the menu handed to you by the waiter in a restaurant. You can choose according to the contents of the menu.
Select 1 to start the game; select 0 to end the game.

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

2. Realize multi-row and multi-column mine clearance

First, we will create two char type arrays mine[ ] and show[ ]. The
mine array is used to store the location information of mines, and the show array is used to store information about the number of mines around non-mine locations.

    char mine[9][9] = {
    
     0 };
	char show[9][9] = {
    
     0 };

But since we want to achieve multi-row and multi-column mine clearance, and for the convenience of modifying the number of rows, columns and mines in the future, we might as well
use the #define macro definition

#define ROW 9//行
#define COL 9//列
#define LANDMINE 10//雷的个数

And for the convenience of later searching for information around the non-mine position (for example, the outermost two rows and two columns of the 9*9 array have less than 8 positions around it, so while searching for information around the non-mine position later, it is also necessary to determine the surrounding positions. Whether it is out of bounds), you might as well add two rows and two columns to the original array , and they are all initialized to characters 0 in the mine array (so you don’t have to judge whether the surrounding positions of a certain position are out of bounds)

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

Effect:
Insert image description here

3.Initialization

After defining the two arrays, initialize them.
The mine array is all initialized to characters 0, the show array is all initialized to characters *, and set is used to receive characters 0 and *.

    Initiate_board(mine, ROWS, COLS, '0');
	Initiate_board(show, ROWS, COLS, '*');
void Initiate_board(char mine[ROWS][COLS], int rows, int cols, char set)
{
    
    
	int i, j;
	for (i = 0; i < rows; i++)
		for (j = 0; j < cols; j++)
			mine[i][j] = set;
}

4. Lay out mines

	Set_mine(mine, ROW, COL);

1. Note that layout mines are only used in the light blue 9*9 area in the picture above, and are not used in the dark blue area. Therefore, when passing parameters, the original rows and columns row and col are passed instead of adding two rows. Two columns of rows and cols

2. Arranging n mines means generating n different random coordinates that meet the requirements, so the rand() function must be used, and the premise of use is to use the srand() function, so in order to generate n random coordinates, then You need to call srand((unsigned int)time(NULL)) once in the main function ;

void Set_mine(char mine[ROWS][COLS], int row, int col)
{
    
    
	int i = 0;
	while (i < LANDMINE)
	{
    
    
		int x = rand() % row + 1;//使得x的范围在1--row
		int y = rand() % col + 1;//使得y的范围在1--col
		if (mine[x][y] == '0')
		{
    
    
			mine[x][y] = '1';
			i++;
		}
	}
}

5. Print array

	Display_board(mine, ROW, COL);

When printing an array , only the light blue area in the above figure is printed, and the dark blue area does not need to be printed. Therefore, when passing parameters , the original rows and columns row and col are passed , instead of adding two rows and two columns rows and cols.

In order for us to quickly know which row and column a certain position is, we can use the following code

void Display_board(char mine[ROWS][COLS], int row, int col)
{
    
    
	int i, j;
	printf("\n----------扫雷----------\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 ", mine[i][j]);//打印数组
		printf("\n");
	}
	printf("----------扫雷----------\n");
	printf("\n");
}

Rendering:
Insert image description here

6. Start demining

First enter a coordinate, and determine whether the position is a mine in the mine array (that is, whether the position is character 1). If so, it will prompt "Unfortunately, you were killed"; if not, find how many surrounding positions there are. mine, and pass the number of mines to the corresponding position of the show array

	Search_mine(mine, show, ROW, COL);
int Search_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
    
    
	int x, y;
	int i = 0;
	while (i < ROW * COL - LANDMINE)//ROW * COL - LANDMINE表示非雷的个数
	{
    
    
		printf("请输入想要查找的位置:");
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
    
    
			if (mine[x][y] == '1')//踩到炸弹后,直接退出循环
			{
    
    
				printf("\n很遗憾,您被炸死了\n");
				Display_board(mine, ROW, COL);
				break;
			}
			else if (show[x][y] != '*')
			{
    
    
				printf("该位置已排雷,请重新输入:\n");
			}
			else
			{
    
    
				int count = landmine_count(mine, x, y);//landmine_count()函数在下面介绍
				show[x][y] = count + '0';//使得该位置的数字几变成字符几
				Display_board(show, ROW, COL);
				i++;
			}
		}
		else
		{
    
    
			printf("输入错误,请重新输入\n");
		}
	}
	if (i == ROW * COL - LANDMINE)//当数组中的非雷位置全都排出,即为排雷成功
	{
    
    
		printf("\n恭喜你,扫雷成功\n");
		Display_board(mine, ROW, COL);
	}
}

7. landmine_count() function

Because the mine array is all characters 0 or 1, and character n minus character 0 = number n, so the number of mines is the sum of the characters in the 8 positions around the position minus 8*character 0

int landmine_count(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';
}

③Modular code implementation

1.text.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

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

void game()
{
    
    
	char mine[ROWS][COLS] = {
    
     0 };
	char show[ROWS][COLS] = {
    
     0 };
	//初始化
	Initiate_board(mine, ROWS, COLS, '0');
	Initiate_board(show, ROWS, COLS, '*');
	//Display_board(mine, ROW, COL);
	Display_board(show, ROW, COL);
	//布置雷
	Set_mine(mine, ROW, COL);
	Display_board(mine, ROW, COL);
	//扫雷
	Search_mine(mine, show, ROW, COL);
}

int main()
{
    
    
	int input = 0;
	srand((unsigned int)time(NULL));
	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.game.h

#pragma once

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

#define LANDMINE 10

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

//初始化
void Initiate_board(char mine[ROWS][COLS], int rows, int cols, char set);
//打印
void Display_board(char mine[ROWS][COLS], int row, int col);
//布置雷
void Set_mine(char mine[ROWS][COLS], int row, int col);
//扫雷
int Search_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);


3.game.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

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

void Display_board(char mine[ROWS][COLS], int row, int col)
{
    
    
	int i, j;
	printf("\n----------扫雷----------\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 ", mine[i][j]);
		printf("\n");
	}
	printf("----------扫雷----------\n");
	printf("\n");
}

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

int landmine_count(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';
}

int Search_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
    
    
	int x, y;
	int i = 0;
	while (i < ROW * COL - LANDMINE)
	{
    
    
		printf("请输入想要查找的位置:");
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
    
    
			if (mine[x][y] == '1')
			{
    
    
				printf("\n很遗憾,您被炸死了\n");
				Display_board(mine, ROW, COL);
				break;
			}
			else if (show[x][y] != '*')
			{
    
    
				printf("该位置已排雷,请重新输入:\n");
			}
			else
			{
    
    
				int count = landmine_count(mine, x, y);
				show[x][y] = count + '0';
				Display_board(show, ROW, COL);
				i++;
			}
		}
		else
		{
    
    
			printf("输入错误,请重新输入\n");
		}
	}
	if (i == ROW * COL - LANDMINE)
	{
    
    
		printf("\n恭喜你,扫雷成功\n");
		Display_board(mine, ROW, COL);
	}
}

Guess you like

Origin blog.csdn.net/qq_75000174/article/details/132045526