Mini Game Series - Minesweeper

Table of contents

1. Game introduction

2. Finished product preview

3. Design ideas

4. Game Production

1), initialize the board

2), the computer randomly arranges mines

3) Display the chessboard

4) Get the number of surrounding mines

5) Expand a piece

6), demining && judging winning or losing

V. Summary


Click my code cloud extraction code

1. Game introduction

"Minesweeper" is a popular puzzle game released in 1992. The goal of the game is to find out all non-thunder grids according to the numbers that appear on the click grid in the shortest time, and at the same time avoid stepping on mines. If you step on a thunder, you will lose everything.

2. Finished product preview

Select the menu to enter the game

Enter coordinates to clear mines

It's not Ray, continue to line up

If you step on mine, the game is over, print the information of mine

3. Design ideas

  • board initialization
  •  The computer randomly places mines
  • display board
  • demining
  • Show the number of mines around
  • spread out
  • game win or lose
  • Improve the game interface, print menu

4. Game Production

  • Design two arrays

                 Myboard                                       Showborad          

The Showboard array is used to display player actions.

The Myboard array is used to store Lei's information.

  • The specification is 11*11

It is convenient to avoid the array out of bounds and count the number of mines around.

1), initialize the board

memset function (string.h)

 Quickly set array elements

void InitBoard(char board[ROWS][COLS], int rows, int cols,char set)
{
	memset(board, set, sizeof(char) * rows * cols);
}

2), the computer randomly arranges mines

Mark '0' as non mine, '1' as mine

The location of mine, random number generation.

rand generates random numbers, call the rand function, first define srand

Notice:

Arrange mines randomly, to be arranged in '0'

In order to modify the number of mines conveniently, it is recommended to use a macro to define the number of mines

void SetBoard(char board[ROWS][COLS], int row, int col)
{
	int count = EASY;
	while (count)
	{
		int x = rand() % 9 + 1;
		int y = rand() % 9 + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}

3) Display the chessboard

For easy identification, on the 9*9 chessboard, print row and column information.

void ShowBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("**********扫雷游戏*********\n");
	//Count(board, ROW, COL);
	for (i = 0; i <= row; i++)
	{
		printf("%d", i);
		for (j = 1; j <= col; j++)
		{
			if (i == 0)
				printf(" %d ", j);
			if(i>0)
			printf(" %c ", board[i][j]);
		}
		printf("\n");
	}
}

4) Get the number of surrounding mines

Count the sum of characters in the surrounding 8 grids,

Subtract 8 characters '0' to get the number of thunder

When creating an array, store characters

For example, there are 3 '1's and 5 zeros around a grid of Myboard, count 3*'1'--8*' 0 ', and get the integer 3.

//计算周围八个格子雷数
int GetCount(char board[ROWS][COLS], int x, int y)
{
	return board[x - 1][y - 1] + board[x][y - 1] + board[x + 1][y - 1]
		+ board[x - 1][y] + board[x + 1][y] +
		board[x - 1][y + 1] + board[x][y + 1] + board[x + 1][y + 1] 
		- 8 * '0';
}

5) Expand a piece

Every time the player clears mines (inputs the coordinates once), the number of mines in 8 grids around Myboard will be detected. If the number of mines is not 0, the number of mines will be displayed at the coordinate point in the Showboard.

If it is 0 mines, it will be displayed as a blank, and at the same time, each coordinate is recursively surrounded by 8 grids, and if it is 0, it will be displayed as a blank until it is a number. just finished unfolding

void Open(char board[ROWS][COLS], char sboard[ROWS][COLS], int x, int y)
{
	//检查合法性
	if (x == 0 || y == 0 || x == ROWS - 1 || y == COLS - 1)
	{
		return;
	}
	if (sboard[x][y] != '*')
	{
		return;
	}

	//显示周围雷数
	int count = GetCount(board, x, y);
	if (count > 0)
	{
		sboard[x][y] = count + '0';
		return;
	}

	//递归清除一片
	else if (count == 0)
	{
		sboard[x][y] = ' ';
		Open(board, sboard, x - 1, y - 1);
		Open(board, sboard, x - 1, y);
		Open(board, sboard, x - 1, y +1);
		Open(board, sboard, x , y - 1);
		Open(board, sboard, x , y + 1);
		Open(board, sboard, x + 1, y - 1);
		Open(board, sboard, x + 1, y );
		Open(board, sboard, x + 1, y + 1);
	}
}

6), demining && judging winning or losing

Judge win or lose:

If you hit thunder, you lose

Finish all the non-mines to win

The cycle is completed, each time a non-thunder point is arranged, the loop condition win flag ++; until win==non-thunder point

void Find(char board[ROWS][COLS],char sboard[ROWS][COLS] , int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	while (win < row * col - EASY)
	{
		printf("输入坐标\n");
		scanf("%d%d", &x, &y);
		if (1 <= x && 9 >= x && 1 <= y && 9 >= y)//检查坐标合法性
		{
			if (sboard[x][y] == '*')//检查是否被排过
			{
				if (board[x][y] == '1')
				{
					printf("排到雷了,游戏结束\n");
					ShowBoard(board, ROW, COL);
					break;
				}
				Open(board, sboard, x, y);//展开一片
				win++;
				ShowBoard(sboard, row, col);
			}
			else
				printf("坐标被排查过\n");
		}
		else
			printf("坐标非法\n");
		
	}
	if (win == row * col - EASY)//排雷胜利条件
	{
		printf("排雷成功!\n");
		ShowBoard(board, ROW, COL);
	}
}

7), menu, modular

print a simple menu

Assemble each module

V. Summary

When you write it once, you won't feel so difficult.

A more ingenious idea is to set two 11*11 arrays.

In the implementation of the demining function, it takes a little thought to expand recursively.

A simple game to deepen the understanding of arrays.

——— There are no shortcuts in programming.

Guess you like

Origin blog.csdn.net/m0_73299809/article/details/128810156