C language small project - mine clearance (basic version)

preface

Minesweeping game is a small project that C language beginners need to practice. The most basic version of minesweeping implemented in this paper (i.e. only one grid can be swept at a time). The actual minesweeping game will have a function: when players click on the grid, it will expand outward until they are about to encounter thunder. This function is not implemented in this paper at present.

Code explanation

1, Main function

int main()
{
	int i = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();//Print menu
		scanf("%d", &i);
		switch (i)
		{
		case 1:
			game();
			break;
		case 0:
			printf("game over\n");
			break;
		default:
			printf("Input error, please re-enter\n");
			break;
		}
	} while (i);
	return 0;
}

1,srand((unsigned int)time(NULL))

It is a random number generator, and the rand() function needs to be used in subsequent mine layout.

2. Do while loop

It is used for players to play multiple times until the player selects the end of the game, and then the cycle is terminated.
The menu() function is a menu function, which is used to print a simple menu for players to choose.

void menu()
{
	printf("*************************\n");
	printf("****** 1,Start the game ******\n");
	printf("****** 0,End the game ******\n");
	printf("*************************\n");
}

Using the switch case function, if the player enters "1", the game will start; If the player enters "0", the game ends and the cycle stops; If the player enters another number, the player will be prompted and asked to re-enter until the entered number is "0" or "1".

2, Game implementation function

The implementation of the mine sweeping game relies on two two-dimensional arrays: mine array is used for the system to randomly arrange mines, and show array is displayed to players for demining. When all the squares except thunder are selected by the player, the player wins.

void game()
{
	//initialization
	char mine[ROWS][COLS] = { 0 };//Store the information of mine layout
	char show[ROWS][COLS] = { 0 };//Store player operation information
	//Initialize mine layout interface
	Init_board(mine, ROWS, COLS, '0');
	//Initialize player operation interface
	Init_board(show, ROWS, COLS, '*');
	//Randomly generated mine
	Mine_set(mine, ROW, COL);
	//Print minesweeping interface
	Display_board(show, ROW, COL);
	//mine clearance
	Mine_sweep(mine, show, ROW, COL);
}

1,char mine[ROWS][COLS] = { 0 },char show[ROWS][COLS] = { 0 }

It is used to create and initialize a two-dimensional array as the minesweeping interface
The project adopts 9 * 9 grid. In order to facilitate future modification, define ROW and COL are adopted here.
In addition to ROW and COL, two additional variable names, ROWS and COLS, are defined. When the player sweeps mines, if no mines are selected, the selected grid will display the number of surrounding mines, but part of the grid at the boundary will be outside the boundary during calculation. If it is not processed, it is not conducive to subsequent calculation. Therefore, an additional circle is added at the outermost edge of the project, so that the number of surrounding mines can be calculated even at the boundary.

#define ROW 9
#define COL 9

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

2,void Init_board(char board[ROWS][COLS], int row, int col, char set)

This function is declared as the initialization function of minesweeping interface, initializing mine to character '0' and show to '*' (here, by establishing a variable set of char type and giving different values to set, the interface can be initialized to any character, avoiding the need to establish two functions to initialize mine and show respectively)

void Init_board(char board[ROWS][COLS], int row, int col, char set)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			board[i][j] = set;
		}
	}
}

3,Mine_set(mine, ROW, COL)

This function is used to randomly generate mines. Count by count. If there are no mines arranged there, place the character '1' to represent mines (place '1' here to facilitate the subsequent calculation of the number of surrounding mines), and the count will be reduced by 1 accordingly.

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

Easy is defined here_ Mode, which is convenient for modifying the number of mines in the future.

#define easy_mode 10

4,Display_board(show, ROW, COL)

This function is used to print the interface for players to clear mines. In addition to the 9 * 9 grid, the line number and column number are printed to facilitate players to input coordinates for demining.

void Display_board(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	//Print column number
	for (j = 0; j <= col; j++) {
		printf("[%d]", j);
	}
	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");

5,Mine_sweep(mine, show, ROW, COL)

This function is the core of the whole minesweeping game. When the player selects all the squares except thunder, the player wins.

void Mine_sweep(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_mode)
	{
		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("You're blown up. The game's over\n");
				Display_board(mine, ROW, COL);
				break;
			}
			else
			{
				int n = get_mine_amount(mine, x, y);//Judge the number of surrounding mines
				show[x][y] = n + '0';
				win++;
				Display_board(show, ROW, COL);
			}
		}
		else
		{
			printf("Out of range, please re-enter\n");
		}
	}
	if (win == row * col - easy_mode)
	{
		printf("Congratulations on your victory\n");
		Display_board(mine, ROW, COL);
	}
}

After the player enters the coordinates, first check whether the coordinates entered by the player are within the interface range. If they are within the range, continue to judge whether the player stepped on the thunder. If he stepped on the thunder, he will jump out of the loop and end the game. Otherwise, use get_ mine_ The amount (mine, x, y) function calculates the number of surrounding mines and prints the number on the interface for players to judge in the next step.
A trick is used here: a number plus the character '0' can be transformed into the corresponding character (the number plus the character '0' is equivalent to the ASCII value of the number plus' 0 ').
So in get_ mine_ In the amount function, we only need to add up the characters in the 8 grids around the coordinates selected by the player, subtract 8 and multiply '0' to get the number of corresponding mines, which is why the character '1' was previously selected as the mark of mines.

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

Since the two-dimensional array show is of char type, the character '0' needs to be added during assignment to convert the number into the corresponding character.

Code display

1, Game h

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

//Define rows and columns
#define ROW 9
#define COL 9

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

#define easy_mode 10

//Initialize minesweeping interface
void Init_board(char board[ROWS][COLS], int row, int col, char set);

//Print minesweeping interface
void Display_board(char board[ROWS][COLS], int row, int col);

//Randomly generated mine
void Mine_set(char board[ROWS][COLS], int row, int col);

//mine clearance
void Mine_sweep(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

2, Test c

#define _CRT_SECURE_NO_WARNINGS
#include "game.h"

//Menu function
void menu()
{
	printf("*************************\n");
	printf("****** 1,Start the game ******\n");
	printf("****** 0,End the game ******\n");
	printf("*************************\n");
}

//The Minesweeper game
void game()
{
	//initialization
	char mine[ROWS][COLS] = { 0 };//Store the information of mine layout
	char show[ROWS][COLS] = { 0 };//Store player operation information
	//Initialize mine layout interface
	Init_board(mine, ROWS, COLS, '0');
	//Initialize player operation interface
	Init_board(show, ROWS, COLS, '*');
	//Randomly generated mine
	Mine_set(mine, ROW, COL);
	//Print minesweeping interface
	Display_board(show, ROW, COL);
	//mine clearance
	Mine_sweep(mine, show, ROW, COL);
}


//Main function
int main()
{
	int i = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();//Print menu
		scanf("%d", &i);
		switch (i)
		{
		case 1:
			game();
			break;
		case 0:
			printf("game over\n");
			break;
		default:
			printf("Input error, please re-enter\n");
			break;
		}
	} while (i);
	return 0;
}

3, Game c

#define _CRT_SECURE_NO_WARNINGS
#include "game.h"

//Initialize minesweeping interface
void Init_board(char board[ROWS][COLS], int row, int col, char set)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			board[i][j] = set;
		}
	}
}

//Print minesweeping interface
void Display_board(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	//Print column number
	for (j = 0; j <= col; j++) {
		printf("[%d]", j);
	}
	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");
	}
}

//Randomly generated mine
void Mine_set(char board[ROWS][COLS], int row, int col)
{
	int count = easy_mode;
	while (count > 0)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}

//Judge the number of surrounding mines
int get_mine_amount(char mine[ROWS][COLS], int x, int y)
{
	return mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1]
		+ mine[x - 1][y] + mine[x + 1][y]
		+ mine[x - 1][y + 1] + mine[x][y + 1] + mine[x + 1][y + 1]
		- 8 * '0';
}

//mine clearance
void Mine_sweep(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_mode)
	{
		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("You're blown up. The game's over\n");
				Display_board(mine, ROW, COL);
				break;
			}
			else
			{
				int n = get_mine_amount(mine, x, y);//Judge the number of surrounding mines
				show[x][y] = n + '0';
				win++;
				Display_board(show, ROW, COL);
			}
		}
		else
		{
			printf("Out of range, please re-enter\n");
		}
	}
	if (win == row * col - easy_mode)
	{
		printf("Congratulations on your victory\n");
		Display_board(mine, ROW, COL);
	}
}

summary

This project basically realizes the minesweeping game, but there is still a large space for modification. For example, the expansion function after selecting the grid is realized by recursion

Keywords: C

Added by lightpace on Sat, 25 Dec 2021 16:38:59 +0200