Games: minesweeping (implemented in C language)

Posted by bagsobrands on Sun, 02 Jan 2022 07:50:30 +0100

Game introduction

About mine sweeping, it can be said that it is a classic in educational games. Then briefly introduce mine sweeping. The mine sweeping interface will be more than one × Many squares, in which thunder is placed, we choose one of the squares. If it is thunder, it will be killed; If it's not a mine, print the number of surrounding mines until all mines are found. Game victory.

This is a brief introduction to minesweeping, but our minesweeping game (implemented in C language) can't achieve that effect like that made by web pages. We use 9 × 9 grid, just a simple implementation,. It still needs to be improved. Well, let's see how to achieve it.

Realization idea

First, we divide the code into three parts -- game H (header file), game C (game function), test C (main function implementation). First, we need a large framework, put the menu in it, select "1", enter the game, select "0", exit the game, enter other numbers, print "input error", enter the game, and we initialize two arrays, the first to store the information of deploying mines and the second to store the information of troubleshooting mines. Then, we initialize two checkerboards. The first two-dimensional array is all set to the character "0", and the second two-bit array is all set to the character "*". Next, we randomly generate thunder from the first two-dimensional array (represented by the character "1"); Print our second two-dimensional array (at this time, the second two-dimensional array is all "*"), we check the thunder once and judge: 1. Step on the thunder, blow up and end the game! 2. If you don't step on the mine, print several mines around you. 3. Input illegal coordinates (e.g. 10, 10). When you don't step on the mine 71 times, the game wins (demining one by one, 9) × 9 square, 10 mines in total).

code implementation

Print menu

void menu()
{
	printf("****************************\n");
	printf("********   1,play   *******\n");
	printf("********   0,exit   *******\n");
	printf("****************************\n");
}
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("Selection error, reselect\n");
			break;
		}
	} while (input);
	return 0;
}

Function: print menu, select 1 to enter the game; Select 0 to exit the game. If you enter other numbers, the print selection is wrong. If you want to continue the game after playing one, select 1 again to enter the game again.

Initialize chessboard

void InitBoard(char board[ROWS][COLS], int rows, int cols ,char set)
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}

This function is used to initialize the chessboard. The first one sets all elements to the character "0", and the second one sets all elements to "*"; It is used to store and check mines. The two do not affect each other.

Display chessboard

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

This function is used to display the chessboard and observe the current status of mine investigation.

Lay thunder

void SetMine(char board[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;
	while (count)
	{
		//1. Generate random subscript
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] != '1')
		{
			board[x][y] = '1';
			count--;
		}
	}
}

This function is used for the arrangement of mines, randomly generate coordinates, and put the character "1" into the first chessboard. Note that the arranged mines may coincide, so we need to judge whether there are mines at the current position of the arranged mines.

Check thunder

int GetMineCount(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 FindMine(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 to check:>\n");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("I'm sorry you were killed\n");
				DisplayBoard(mine, ROW, COL);
				break;
			}
			else
			{
				int count = GetMineCount(mine, x, y);
				show[x][y] = count + '0';
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("Illegal coordinates, re-enter:\n");
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("Congratulations on your success\n"); 
		DisplayBoard(mine, ROW, COL);
	}
}

This function is used to check thunder. Enter the coordinates to check and judge: 1. If it is thunder, it will blow up and end the game. 2. If not, calculate the number of mines around the inspection coordinates, store the character "0" in the second chessboard and print it. 3. Enter illegal coordinates and print "illegal coordinates". When 71 checks, the game is complete.

Complete code

game. H (header file)

#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
//Initialize chessboard
void InitBoard(char board[ROWS][COLS],int rows ,int cols ,char set);
//Display chessboard
void DisplayBoard(char board[ROWS][COLS],int row,int col);
//Lay thunder
void SetMine(char board[ROWS][COLS], int row, int col);
//Check thunder
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col);

This can change the number of mines and the size of the chessboard.

game.c

#include "game.h"

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

void SetMine(char board[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;
	while (count)
	{
		//1. Generate random subscript
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] != '1')
		{
			board[x][y] = '1';
			count--;
		}
	}
}

int GetMineCount(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 FindMine(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 to check:>\n");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("I'm sorry you were killed\n");
				DisplayBoard(mine, ROW, COL);
				break;
			}
			else
			{
				int count = GetMineCount(mine, x, y);
				show[x][y] = count + '0';
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("Illegal coordinates, re-enter:\n");
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("Congratulations on your success\n"); 
		DisplayBoard(mine, ROW, COL);
	}
}

Put the entire function header used by game () into the game. File, where H header file.

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 };//Store mine information
	char show[ROWS][COLS] = { 0 };//Store information about mine detection
	//Initialize the chessboard
	InitBoard(mine,ROWS,COLS,'0');
	InitBoard(show,ROWS,COLS,'*');
	//Lay thunder
	SetMine(mine,ROW,COL);
	DisplayBoard(show, ROW, COL);
	//Check thunder
	FindMine(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("Selection error, reselect\n");
			break;
		}
	} while (input);
	return 0;
}

In the main function part, this part is also the implementation part. We can see the operation process of the game() function, create two arrays to store mine information and mine screening information, arrange 10 mines and display mine screening information (at this time, the second array is all "/ *"), and then conduct mine clearance.

Operation display

summary

Compared with the nature of Sanzi chess, the minesweeping game has a deeper understanding of arrays, so the minesweeping game has a deeper understanding of arrays, but it is not so comprehensive compared with Sanzi chess. Although it is also involved, it is a good opportunity to practice C language code anyway. I hope you can take the time to knock, If there are any wrong problems, I hope you can correct them. Thank you!

Topics: C Mini Program