C language minesweeping games

Posted by Brenden Frank on Thu, 03 Feb 2022 13:08:32 +0100

I Rules of the game

Eliminate all the grids that are not bombs on a 9x9 small chessboard. If you step on one of the 10 mines, the game ends. If you don't step on it, judge the location of the surrounding 3x3 grid mines according to the information on the stepped grid until there are 10 mines left.

II Basic logic

1. Print menu
2. Initialize the chessboard
3. Display chessboard
4. Mine emplacement
5. Mine detection

III Step implementation

1. Print menu

A simple print function

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

2. Initialization

First, define the mine array and show array

	//The mine array is used to store the information of the arranged mine
	char mine[ROWS][COLS] = { 0 };
	//The show array is used to store the information of the detected mines
	char show[ROWS][COLS] = { 0 };

After understanding the rules of the game, we will know that there may be thunder in the last row and last column, but we must know the information of the surrounding 9 grids, so we define rows=row+2,cols=col+2
Macro is defined as:

Start initialization. The purpose of initializing the chessboard is to assign each chessboard a value of 0 or '*', so a new character variable set is introduced

void init_board(char arr[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++)
		{
			arr[i][j] = set;//To assign an array value of 0 or *, create a character variable.
		}
	}
}

3. Print chessboard

Use the cycle to print out 9x9 grids. For ease of viewing, line labels and lists will be printed

void show_board(char arr[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("***********mine clearance***********\n");

	for (i = 0; i <= col; i++)//Print column labels, starting from 0 for alignment
	{
		printf("%d ", i);
	}
	printf("\n");

	for (i = 1; i <= row; i++)//Print line labels, and print column labels at the first of each line at the beginning of each cycle
	{
		printf("%d ", i);

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

}

4. Arrange lightning

Easy will be arranged randomly_ Count mines are in the chessboard. In order to facilitate later modification, macro definition is used

Use the cycle to generate random coordinates, judge whether there is thunder in the coordinates, and assign it as thunder if there is no thunder
Use the rand function to generate random numbers. Note that in test C file is initialized with srand function, and the header function is added


Generate a random number and modulus a number x, and the final result is [0,x)

void set_mine(char mine[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;
	int x = 0;
	int y = 0;
	while (count)
	{
		x = rand() % row + 1;
		y = rand() % col + 1;//x. Y is the array coordinate
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

5. Start mine clearance

Enter a coordinate, and first judge whether it is on the chessboard. When judging whether it is a mine, it will exit. If it is not a mine, the coordinate will display the number of surrounding 8 grid mines until the remaining grid except mine is 0, and the game is over

void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;//Number of remaining grids during demining
	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("GG\n");
				show_board(mine, ROW, COL);
				break;
			}
			else
			{
				int count = get_mine_count(mine, x, y);//Displays the number of surrounding mines
				show[x][y] = count + '0';//count is the number of Lei, which needs to be changed into characters
				show_board(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("Illegal coordinates, re-enter\n");
		}
	}
	if(win == row * col - EASY_COUNT)
	{
		printf("good job\n");
		show_board(mine, ROW, COL);
	}
}

Count the number of Mines other than the selected coordinates in 3 * 3 grids
If there is' 1 'on a grid, 1 is returned

int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
	return mine[x - 1][y] + mine[x + 1][y] +
		mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] +
		mine[x - 1][y + 1] + mine[x][y + 1] + mine[x + 1][y + 1]
		- 8 * '0';
}

IV Game code

test.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()
{
	//Implementation of mine sweeping game
	//The mine array is used to store the information of the arranged mine
	char mine[ROWS][COLS] = { 0 };
	//The show array is used to store the information of the detected mines
	char show[ROWS][COLS] = { 0 };

	//Initialize chessboard
	init_board(mine, ROWS, COLS, '0');
	init_board(show, ROWS, COLS, '*');

	//Print chessboard
	//show_board(mine, ROW, COL);
	//Lay thunder
	set_mine(mine, ROW, COL);
	show_board(show, ROW, COL);
	//Check thunder
	find_mine(mine,show, ROW, COL);

}
int main()
{
	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 the game\n");
			break;
		default:
			printf("Input error, please re-enter\n");
			break;
		}

	} while (input);

 return 0;

}

game.h

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

#define ROW 9
#define COL 9

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

#define EASY_COUNT 10 / / number of Mines

void init_board(char arr[ROWS][COLS], int rows, int cols, char set);

void show_board(char arr[ROWS][COLS], int row, int col);//The array is still 11 * 11, but the formal parameter is still 9 * 9

void set_mine(char mine[ROWS][COLS],int row,int col);

void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

game.c

#include"game.h"

void init_board(char arr[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++)
		{
			arr[i][j] = set;//To assign an array value of 0 or *, create a character variable.
		}
	}
}

void show_board(char arr[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("***********mine clearance***********\n");

	for (i = 0; i <= col; i++)//Print column labels, starting from 0 for alignment
	{
		printf("%d ", i);
	}
	printf("\n");

	for (i = 1; i <= row; i++)//Print line labels, and print column labels at the first of each line at the beginning of each cycle
	{
		printf("%d ", i);

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

}

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

int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
	return mine[x - 1][y] + mine[x + 1][y] +
		mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] +
		mine[x - 1][y + 1] + mine[x][y + 1] + mine[x + 1][y + 1]
		- 8 * '0';
}

void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;//Number of remaining grids during demining
	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("GG\n");
				show_board(mine, ROW, COL);
				break;
			}
			else
			{
				int count = get_mine_count(mine, x, y);//Displays the number of surrounding mines
				show[x][y] = count + '0';
				show_board(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("Illegal coordinates, re-enter\n");
		}
	}
	if(win == row * col - EASY_COUNT)
	{
		printf("good job\n");
		show_board(mine, ROW, COL);
	}
}

V Achievement display


Thank you for your watching. You are welcome to point out the deficiencies.

Topics: C Back-end