C language entry game (minesweeper), recursive, can be spread around [specific implementation is conducive to eating] ψ (') ψ]

Keywords: C

Catalog

Preface

1. Important points of knowledge to be noticed in the realization of this mini-game

1. The first step is to use arrays and functions

2. We will use recursion to expand

2. Ways to Realize Game

3. Realization of Game Subject

1. Implementation of Game Menu

2. Implementation of Main Function

3.game() function implementation, completed the main body of the game

4. Functional implementation of the game (the most important place)

4. Minesweeper game full code

1.game.h

2.game.c

3.test.c

5. Minesweeper game running

Preface

hello wow, meet you again. This time brings a mini game - Minesweeper, this minesweeper, can't be like what we normally play, just use the mouse point, can't stand flag. After all, we are still a beginner in C. Wow, we need a little bit of learning progress, until that level, then we can also make a really decent Minesweeper game.

The implementation of minesweeping is similar to that of the previous tri-chess, but also uses the idea of modularization. It is recommended that you finish the last tri-chess before you learn this one. At the same time, some of the repetitions of the previous article are briefly covered in this article.

Okay, let's not say more. Now start the Minesweeper Game.

1. Important points of knowledge to be noticed in the realization of this mini-game

1. The first step is to use arrays and functions

The knowledge of arrays and functions has been introduced before, let alone go.

2. We will use recursion to expand

The best way to do this is to use recursion, which is difficult for us as a beginner, but we have to get through it. Recursion is an important point of knowledge. You can see what happened before me (through recursion in Hannotta and Frog Step). To further understand recursion.

2. Ways to Realize Game

1. To play this game more than once, you should first create a menu and put it into the do while loop.

2. To make a minesweeper game possible, we must be familiar with arrays and functions. If we create a board, then when we put the thunder on it, the players can see it, so there is no playability at all, so if we can't create one, we'll create two more boards. One is for our internal implementation, storing some mines, and the other is for the player.

3. After that, some mines should be placed in the internal array.

4. Finally, the most critical step is "minesweeping". Here we should complete the coordinate input, the minesweeping, the different board displayed in front of the player by each row of mines, and the tips and algorithms for stepping on a mine or successful minesweeping.

5. Storage method

game.h stores header files, macro definitions, function declarations

game.c is used to implement functions

test.c is used for the overall implementation of the game and is the test of the game

3. Realization of Game Subject

1. Implementation of Game Menu

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

2. Implementation of Main Function

void test()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("Please select:>");
		scanf("%d", &input);
		switch(input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("Exit Game\n");
			break;
		default:
			printf("Selection error, please re-select\n");
		}
	} while (input);
}

int main()
{
	test();

	return 0;
}

1,2 These two steps are almost the same as a trio.

3.game() function implementation, completed the main body of the game

Macro defines ROW COL

ROWS=ROW+2,COLS=COL+2

void game()
{
	//Create an array of mine information (inside, not for players)
	char mine[ROWS][COLS] = { 0 };
	//Create an array of mine-clearing information (external, for players to see)
	char show[ROWS][COLS] = { 0 };
	//Initialize the board
	InitBoard(mine, ROWS, COLS, '0');//Initialize all internals to 0
	InitBoard(show, ROWS, COLS, '*');//Initialize all exteriors to *
	//DisplayBoard(mine, ROW, COL); (Printing this board allows you to debug it yourself, but not for the player)
	//DisplayBoard(show, ROW, COL);
	//Mine Layout
	SetMine(mine, ROW, COL);
	//Print the board (you want to print the 9*9 board in the middle, but pass the whole array of 11*11)
	DisplayBoard(show, ROW, COL);

	//Mine clearance
	FineMine(mine, show, ROW, COL);
}

4. Functional implementation of the game (the most important place)

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;
		}
	}
}

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	//Print the column number because a line number was previously printed, so make the first column 0, but print one more line.
	//So first i=0, then I < = row
	for (i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		//Print line numbers
		printf("%d ", i);
		//Print board
		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

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--;
		}
	}
}

static int get_mine_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';
}

void open_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
	int count = get_mine_count(mine, x, y);
	if (count == 0)
	{
		show[x][y] = ' ';
		if (show[x - 1][y - 1] == '*')
			open_mine(mine, show, x - 1, y - 1);
		if (show[x - 1][y] == '*')
			open_mine(mine, show, x - 1, y);
		if (show[x - 1][y + 1] == '*')
			open_mine(mine, show, x - 1, y + 1);
		if (show[x][y - 1] == '*')
			open_mine(mine, show, x, y - 1);
		if (show[x][y + 1] == '*')
			open_mine(mine, show, x, y + 1);
		if (show[x + 1][y - 1] == '*')
			open_mine(mine, show, x + 1, y - 1);
		if (show[x + 1][y] == '*')
			open_mine(mine, show, x + 1, y);
		if (show[x + 1][y + 1] == '*')
			open_mine(mine, show, x + 1, y + 1);
	}
	else
		show[x][y] = get_mine_count(mine, x, y) + '0';
}

void FineMine(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 coordinates for mine clearance:>");
		scanf("%d %d",&x, &y);
		//Judging whether coordinates are legal
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			//Thunder Treading
			if (mine[x][y] == '1')
			{
				printf("Unfortunately, you were blown up\n");
				DisplayBoard(mine, row, col);
				break;
			}
			//No thunder
			else
			{
				//int count = get_mine_count(mine, x, y);
				//Show[x][y] = count +'0'; // These two are not needed after using recursion, in open_ These functions are implemented in mine
				open_mine(mine, show, x, y);//If there is no thunder near the input coordinates, spread around until a thunder is encountered
				DisplayBoard(show, ROW, COL);//Print the board again after each mine clearance
				win++;
			}
		}
		else
		{
			printf("Illegal coordinates, please re-enter\n");
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("Congratulations, mine clearance was successful\n");
		DisplayBoard(mine, row, col);
	}
}

The above is after using recursive implementation, if you don't want to expand, you can modify the following code (delete open_mine and uncomment the one I commented out above):

//Put this open_mine's function deletes everything
void open_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)

void FineMine(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 coordinates for mine clearance:>");
		scanf("%d %d",&x, &y);
		//Judging whether coordinates are legal
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			//Thunder Treading
			if (mine[x][y] == '1')
			{
				printf("Unfortunately, you were blown up\n");
				DisplayBoard(mine, row, col);
				break;
			}
			//No thunder
			else
			{
				int count = get_mine_count(mine, x, y);
				show[x][y] = count + '0';//Use the two above
				//open_mine(mine, show, x, y);// Delete everything that changes a function
				DisplayBoard(show, ROW, COL);//Print the board again after each mine clearance
				win++;
			}
		}
		else
		{
			printf("Illegal coordinates, please re-enter\n");
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("Congratulations, mine clearance was successful\n");
		DisplayBoard(mine, row, col);
	}
}

4. Minesweeper game full code

1.game.h

#pragma once

#define ROW 9
#define COL 9

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

#define EASY_COUNT 10


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

//Initialize the board
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
//Print board
void DisplayBoard(char board[ROWS][COLS], int row, int col); //The whole array is passed in, received with ROWS and COLS
//Mine Layout
void SetMine(char mine[ROWS][COLS], int row, int col);
//Mine clearance
void FineMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

2.game.c

#include "game.h"

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;
		}
	}
}

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	//Print the column number because a line number was previously printed, so make the first column 0, but print one more line.
	//So first i=0, then I < = row
	for (i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		//Print line numbers
		printf("%d ", i);
		//Print board
		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

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--;
		}
	}
}

static int get_mine_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';
}

void open_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
	int count = get_mine_count(mine, x, y);
	if (count == 0)
	{
		show[x][y] = ' ';
		if (show[x - 1][y - 1] == '*')
			open_mine(mine, show, x - 1, y - 1);
		if (show[x - 1][y] == '*')
			open_mine(mine, show, x - 1, y);
		if (show[x - 1][y + 1] == '*')
			open_mine(mine, show, x - 1, y + 1);
		if (show[x][y - 1] == '*')
			open_mine(mine, show, x, y - 1);
		if (show[x][y + 1] == '*')
			open_mine(mine, show, x, y + 1);
		if (show[x + 1][y - 1] == '*')
			open_mine(mine, show, x + 1, y - 1);
		if (show[x + 1][y] == '*')
			open_mine(mine, show, x + 1, y);
		if (show[x + 1][y + 1] == '*')
			open_mine(mine, show, x + 1, y + 1);
	}
	else
		show[x][y] = get_mine_count(mine, x, y) + '0';
}

void FineMine(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 coordinates for mine clearance:>");
		scanf("%d %d",&x, &y);
		//Judging whether coordinates are legal
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			//Thunder Treading
			if (mine[x][y] == '1')
			{
				printf("Unfortunately, you were blown up\n");
				DisplayBoard(mine, row, col);
				break;
			}
			//No thunder
			else
			{
				open_mine(mine, show, x, y);//If there is no thunder near the input coordinates, spread around until a thunder is encountered
				DisplayBoard(show, ROW, COL);//Print the board again after each mine clearance
				win++;
			}
		}
		else
		{
			printf("Illegal coordinates, please re-enter\n");
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("Congratulations, mine clearance was successful\n");
		DisplayBoard(mine, row, col);
	}
}

3.test.c

#include "game.h"

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

void game()
{
	//Create an array of mine information (inside, not for players)
	char mine[ROWS][COLS] = { 0 };
	//Create an array of mine-clearing information (external, for players to see)
	char show[ROWS][COLS] = { 0 };
	//Initialize the board
	InitBoard(mine, ROWS, COLS, '0');//Initialize all internals to 0
	InitBoard(show, ROWS, COLS, '*');//Initialize all exteriors to *
	//Mine Layout
	SetMine(mine, ROW, COL);
	//Print the board (you want to print the 9*9 board in the middle, but pass the whole array of 11*11)
	DisplayBoard(show, ROW, COL);

	//Mine clearance
	FineMine(mine, show, ROW, COL);
}

void test()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("Please select:>");
		scanf("%d", &input);
		switch(input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("Exit Game\n");
			break;
		default:
			printf("Selection error, please re-select\n");
		}
	} while (input);
}

int main()
{
	test();

	return 0;
}

5. Minesweeper game running

 

It wasn't my choice to be killed by the blast. It was to show you the effect of trampling lightning.

  Okay, that's the end of this minesweeper. I hope everyone can grow up in each of these codes until the day they become a big guy.

Last but not least, thank you all.

Posted by launchcode on Fri, 12 Nov 2021 09:18:25 -0800