Implementation of C language minesweeping game (detailed analysis)

Keywords: C

Let's sort out our ideas first, and then write the mine sweeping code. In this way, we can write the code efficiently

1. First of all, play the game with at least one interface! The interface allows people to choose whether to play the game or quit, or continue to play after playing the game

2. Initialize array

3. Print chessboard

4. Number of mines placed

5. Demining

Implementation code of laidan

The code is as follows

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


void test()
{
	int input = 0;
	srand((unsigned int)time(NULL));//Don't worry about it for the time being. This is the timestamp random number used by the later code to mine
	do
	{
		menu();
		printf("Please select");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("Entered the game\n");
			game();
			break;
		case 0:
			printf("Quit the game\n");
		default:
			printf("Input error, please re-enter\n");
		}


	} while (input);
}


int main()
{
	test();
	return 0;
}

Some people may wonder why they write so many functions. Can't I handle it with one function?

It can be done with a function, but it's not easy to modify the code after you modify it, and it's not easy to use if others use your code, so you should abide by high cohesion and low coupling when writing code.

What is high cohesion and low coupling? That is to make the functions of each function correspond well. My function corresponds to the calculation, rather than superimposing the results of calculation and print calculation. This has the advantage that it is convenient to modify the errors of the code, and others can directly call this function with your code instead of changing it. It is not as fast as writing it yourself.

We should get the chessboard after we finish the laidan

The code is as follows

	char mine[ROWS][COLS] = { 0 };//Thundering chessboard
	char show[ROWS][COLS] = { 0 };//Chessboard for players

I choose to use two arrays. One array is mine and the other is the printed array chessboard.

Why choose two arrays? The reason is obvious, because one can't achieve two functions.

Then we begin to initialize the function

The code is as follows

void initboard(char board[ROWS][COLS], int rows, int cols, char set)//Initialize the chessboard for mine discharge
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}

Because the two arrays are the same size, you can print with the same function.

After initializing the array, you can start printing the chessboard

The code is as follows

void displayboard(char board[ROWS][COLS], int row, int col)//Print chessboard
{
	int i = 0;
	for (i = 0; i <= row; i++)//that 's ok
	{
		printf("%d ", i);
	}
	printf("\n");
	int j = 0;
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);//column
		for (j = 1; j <=col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

Because it is the same function, we can use the same function to print the chessboard, but because the chessboard with thunder cannot be seen by the player, printing the chessboard is only used to test whether the code is wrong.

Number of mines placed

The code is as follows

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

}

Be careful to put Ray's chessboard here. Don't make a mistake.

Now it's time for the final demining process

The code is as follows

void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)//Find ray
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("Please enter>");
		scanf("%d %d", &x,&y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("unfortunately,You were killed\n");
				displayboard(mine, row, col);
				break;
			}
			else
			{
				system("cls");
				int n = get_minr_count(mine,x,y);//Judge how many mines are nearby
				show[x][y] = n+'0';
				displayboard(show, row, col);//Print chessboard
				
			}
		}
		else
		{
			printf("The coordinates are incorrect. Please re-enter\n");
		}
	}
}

The estimate in question here is show[x][y] = n+'0'; Here, because n is an integer and we print the chessboard with% c, we don't know it when printing an integer, but we add a character '0' and it knows it‘ 0 'has a value in asslc. Since the array was initialized before, it is also' 0 ', so the number of Mines nearby can be printed here

The second is   get_minr_count(mine,x,y); The implementation of

The code is as follows * 8

static int get_minr_count(char mine[ROWS][COLS], int x, int y)//Judge the number of surrounding mines
{
	return mine[x - 1][y] +
		mine[x - 1][y - 1] +
		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';
}

The number of Mines nearby is determined by the range of the Jiugong grid, so you can know by adding each number of the Jiugong grid and subtracting 8 * '0'

All the codes of the main function are presented

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"


void game()
{
	char mine[ROWS][COLS] = { 0 };//Thundering chessboard
	char show[ROWS][COLS] = { 0 };//Chessboard for players
	initboard(mine, ROWS, COLS, '0');//Initialize the chessboard for mine discharge
	initboard(show, ROWS, COLS, '*');//Initialize the chessboard for players
	setmine(mine, ROW, COL);//Place mine
	//displayboard(mine, ROW, COL);// Print chessboard
	displayboard(show, ROW, COL);//Print chessboard
	find_mine(mine,show, ROW, COL);//Find ray

}



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


void test()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("Please select");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("Entered the game\n");
			game();
			break;
		case 0:
			printf("Quit the game\n");
		default:
			printf("Input error, please re-enter\n");
		}


	} while (input);
}


int main()
{
	test();
	return 0;
}

Header file function

#pragma once
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<windows.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2 
#define easy_hard 10 



void initboard(char board[ROWS][COLS], int rows, int cols, char set);//Initialize chessboard
void displayboard(char board[ROWS][COLS], int row, int col);//Print chessboard
void setmine(char mine[ROWS][COLS], int row,int col);//Place mine
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS],int row, int col);//Find ray

Game function

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"


static int get_minr_count(char mine[ROWS][COLS], int x, int y)//Judge the number of surrounding mines
{
	return mine[x - 1][y] +
		mine[x - 1][y - 1] +
		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 initboard(char board[ROWS][COLS], int rows, int cols, char set)//Initialize the chessboard for mine discharge
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}

void displayboard(char board[ROWS][COLS], int row, int col)//Print chessboard
{
	int i = 0;
	for (i = 0; i <= row; i++)//that 's ok
	{
		printf("%d ", i);
	}
	printf("\n");
	int j = 0;
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);//column
		for (j = 1; j <=col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}


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

}

void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)//Find ray
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("Please enter>");
		scanf("%d %d", &x,&y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("unfortunately,You were killed\n");
				displayboard(mine, row, col);
				break;
			}
			else
			{
				system("cls");
				int n = get_minr_count(mine,x,y);
				show[x][y] = n+'0';
				displayboard(show, row, col);//Print chessboard
				
			}
		}
		else
		{
			printf("The coordinates are incorrect. Please re-enter\n");
		}
	}
}

Finally, put some real pictures of mine sweeping you play!

 

 

 

Posted by ltd on Thu, 11 Nov 2021 10:38:57 -0800