Simple minesweeping game [C language version]

Posted by chord on Wed, 29 Dec 2021 03:07:50 +0100

1, Game Description

First, open the game interface and a minesweeping area will appear. There are three minesweeping areas to choose from. The minesweeping difficulty of each minesweeping area increases in turn
Primary: 81 blocks, 10 mines (the primary version of mine sweeping is implemented here)
Intermediate: 256 squares, 40 mines
Advanced: 480 diamonds, 99 mines
take laws and regulations lightly
The rules of the minesweeping game are very simple:
Dig a mine (that is, click to the box set as mine), be blown up, and the game is over.
Open the empty box and you can continue to play.
If the number is excavated, it indicates how many mines there are in the eight blocks around it. This information can be used to infer which blocks nearby can be safely clicked.
The situation during playing the game is as follows:

2, Game design steps

1. Create menu
2. Create and initialize the game minesweeping area
3. Set mine location
4. Print out the minesweeping area
5. Start mine clearance

3, Code implementation

1. Create menu

Specify that input 1 is to play the game and input 0 is to exit the game

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

2. Create and initialize the game minesweeping area

First, creating a minefield requires two two-dimensional arrays

  1. Mine[ROWS][COLS] is used to store the information of minefield layout and will not be displayed
  2. Show[ROWS][COLS] is used to display the data during the minesweeping game
  3. The InitBoard(char board[ROWS][COLS], int rows, int cols,char set) function is used to initialize the game interface
  4. The InitBoard(Mine, ROWS, COLS, '0') function initializes all elements in the Mine array to the character '0'
  5. The InitBoard(Show, ROWS, COLS, '*') function initializes all elements in the Show array to '*'
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;
		}
	}
}

3. Set lightning

Use the random number generation mechanism (if you don't understand the generation of random numbers, you can see: How to generate random numbers )Randomly generate the required number of mines in the array Mine[ROWS][COLS], and specify that the character '1' is mine and the character '0' is not mine

void SetMine(char Mine[ROWS][COLS], int row, int col)
{
	int x = rand() % row + 1;
	int y = rand() % col + 1;
	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--;
		}
	}
}

4. Print minesweeping area

Similarly, the corresponding Mine array or Show array is passed in
You can print the corresponding interface in the DisplayBoard(char board[ROWS][COLS], int row, int col) function

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	for (i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	int j = 0;
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

5. Mine clearance

FineMine(char Mine[ROWS][COLS], char Show[ROWS][COLS], int row, int col) is a minesweeping function

First, enter the coordinates to be demined. If the coordinates are legal, demining will be carried out
When the coordinate of the platoon is yes, it will be blown up; If it is not a mine, the number of surrounding mines is displayed in this coordinate
The function to display the number of mines is Get_mine_count(char Mine[ROWS][COLS], int x, int y)
When all mines are discharged, the demining is successful

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 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 the coordinates of the search>:");
		
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (Mine[x][y] == '1')
			{
				printf("This coordinate is thunder, boom! It's blown up!\n");
				DisplayBoard(Mine, row, col);
				break;
			}
			if (Mine[x][y] == '0')
			{
				int count = Get_mine_count(Mine, x, y);
				Show[x][y] = count + '0';//Because the Show array is a character array, the number of   
				                         //Word elements are stored in ASCII code in memory 
				                         //count+'0' can be used as a word for this position
				                         //In the form of a number
				DisplayBoard(Show, row, col);
				win++;
			}
		}
		else
		{
			printf("Illegal coordinates, please re-enter\n");
		}
    }
	if (win == row * col - EASY_COUNT)
	{
		printf("Congratulations on your success!\n");
	}
}

The above is the specific implementation of each step function. For convenience, we divide the code into three files:
1.game.h ------ the inclusion of game related function declarations, symbol declarations and header files
2.game.c ------ implementation of game related functions
3.test.c ----- test the logic of the game

game.h
Note: when the minesweeping coordinates are the edge of the minesweeping area, in order to facilitate printing the number of mines near the coordinates on the edge of the minesweeping area, the actual array set here is "one circle larger" than the displayed array, that is, there are more rows up and down and more columns on the left and right

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9 / / number of rows displayed
#define COL 9 / / number of columns displayed
#define ROWS ROW+2 / / it is not convenient to count the number of minesweeps, actually the number of rows in the minesweeping area
#define COLS COL+2 / / the actual number of columns in the minesweeping area
#define EASY_COUNT 10 / / set the number of Mines
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char Mine[ROWS][COLS], int row, int col);
void FineMine(char Mine[ROWS][COLS],char Show[ROWS][COLS], int row, int col);

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;
	for (i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	int j = 0;
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

void SetMine(char Mine[ROWS][COLS], int row, int col)
{
	int x = rand() % row + 1;
	int y = rand() % col + 1;
	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 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 the coordinates of the search>:");
		
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (Mine[x][y] == '1')
			{
				printf("This coordinate is thunder, boom! It's blown up!\n");
				DisplayBoard(Mine, row, col);
				break;
			}
			if (Mine[x][y] == '0')
			{
				int count = Get_mine_count(Mine, x, y);
				Show[x][y] = count + '0';
				DisplayBoard(Show, row, col);
				win++;
			}
		}
		else
		{
			printf("Illegal coordinates, please re-enter\n");
		}
    }
	if (win == row * col - EASY_COUNT)
	{
		printf("Congratulations on your success!\n");
	}
}

test.c

#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 };
	InitBoard(Mine, ROWS, COLS,'0');
	InitBoard(Show, ROWS, COLS, '*');
	SetMine(Mine, ROW, COL);
	//DisplayBoard(Mine, ROW, COL);
	printf("--------mine clearance----------\n");
	DisplayBoard(Show, ROW, COL);
	printf("--------mine clearance----------\n");
	FineMine(Mine, Show, ROW, COL);
}

int main()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		menu();
		printf("Please select>:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("mine clearance\n");
			game();
			break;
		case 0:
			printf("Exit the game\n");
			break;
		default:
			printf("Please re-enter\n");
			break;
		}
	} while (input);
		return 0;
}

4, Operation effect


The simple version of the minesweeping game is completed!

Topics: C Game Development