"Introduction to C language" minesweeping game c language implementation

catalogue

 

1. Principle introduction

2. Distribution objectives and code implementation

3. Summary

1. Principle introduction

  First of all, we need an empty mine disk, in which ten mines are buried randomly. When we row this mine, if this position is mine, the game fails. If it is not mine, the number of mines in the surrounding eight squares will appear. We use the array to achieve this goal.

      2. Distribution objectives and code implementation

First, we need to print a menu, accept the user's input data and judge whether to play a game, such as Sanzi chess. The specific code is as follows:

void test()
{
	int input = 0;
	srand((unsigned int)time(NULL));

	do
	{
		menu();
		printf("Please select:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			//mine clearance
			game();
			break;
		case 0:
			printf("Exit the game\n");
			break;
		default:
			printf("Selection error\n");
			break;
		}
	} while (input);
}
void menu()
{
	printf("***********************\n");
	printf("*****  1. play     ****\n");
	printf("*****  0. exit     ****\n");
	printf("***********************\n");
}

Then we need to set an empty disk. We need to set a basic class 9 * 9 thunder disk. (note!!! When we check that the thunder lattice is at the edge, if we want to count the number of thunder in the surrounding eight squares, the array will cross the boundary, so we set an 11 * 11 empty disk during initialization, and then set thunder in the 9 * 9 empty disk). It is the same as Sanzi, For the convenience of subsequent code maintenance, we use define to define the size of the chessboard (if you are interested in Sanzi, you can move to another blog of mine. (14 messages) introduction to C language Sanzi C language implementation (detailed version) _m0_60756071 blog - CSDN blog)

We initialize the thunder disk to a unified symbol.

Here are some codes:

#define ROW 9
#define COL 9

#define ROWS ROW+2
#define COLS COL+2
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;
		}
	}
}

Next, we number the thunder disk rows and columns

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	//1~9
	int i = 0;
	int j = 0;

	//Printing of column numbers
	for (i = 0; i <= col; i++)
	{
		printf("%d ", i);
	}
	printf("\n");

	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);//Print line number

		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

So we can print out such a thunder disk

  Next, we need to set 10 mines randomly in the chessboard. Similarly, using srand mentioned in the previous code, we can get the number of 1 ~ 9 by using rand()%9+1, which is just within the range of Mines we set. The specific code is as follows:

void SetMine(char mine[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;

	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;

		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

This is a hidden chessboard. We set the thunder in it. The array element with thunder is set to '1', and the position without thunder is set to '0'. Next, the player performs demining operation. We write a function to return the number of thunder in the eight grids around the player's selected position (that is, the number of characters' 1 'in the array) Here, we skillfully use the special value '0' to achieve this goal. The specific code is as follows:

static int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
	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';
}

At this point, we will take the last step. We will write another function to call the function above to judge whether the user will enter or win or enter the coordinates.

void FindMine(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("Please enter the coordinates to check:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("I'm sorry you were killed\n");
				DisplayBoard(mine, row, col);
				break;
			}
			else
			{
				//How many mines are there around the X and Y coordinates
				int n = get_mine_count(mine, x, y);
				show[x][y] = n + '0';
				DisplayBoard(show, row, col);
				win++;
			}
		}
		else
		{
			printf("The coordinates entered are illegal and cannot be cleared. Please re-enter\n");
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("Congratulations on your success\n");
		DisplayBoard(mine, row, col);
	}

}

Well, at this step, the block code is roughly completed. We can integrate the code together to realize the minesweeping game.

See for complete code c language / 2021.11.21 · Wu Changsheng / code - Code cloud - Open Source China (gitee.com)

3. Summary

In the minesweeping game, we cleverly set up a larger empty disk to avoid the problem of array crossing. We used two minesweeping disks, one to bury the mine, and the other to print and display to the player to let the player clear the mine. When counting the number of surrounding mines, we cleverly used return.

Thank you for reading, welcome your comments and discussions, and thank you for your praise. I wish you all the best!!!

Tags: C

Posted on Sun, 21 Nov 2021 16:50:51 -0500 by Solarpitch