C language -- application example of array: minesweeping game

Posted by aurheim on Wed, 26 Jan 2022 01:01:57 +0100

1, Rough model

 

2, Design ideas

Referring to the idea of normal minesweeping game, we should first know the number of grids on a chessboard and the number of mines, and then mark the grids that have been swept in the minesweeping process, and know the number of mines in the eight grids around this grid. When we successfully mark all the swept positions, all the rest are the positions of mines, Then the minesweeping was successful.

3, Code implementation process

(1) Create a test C file is used to test game logic

1. The menu function is used to enter the game interface

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

2. The game function is used to realize the game process

Define two arrays. Array mine is used to store the information of arranged mines, and array show is used to store the information of checked mines;

Initialize two arrays. Array mine is printed with the symbol 0 (note the symbol), and array show is printed with the symbol *;

Randomly generate the location of mines and customize the number of mines. A function of printing chessboard is required. The location of arranged mines can be printed for viewing and verification;

The player controls the coordinates to check the position of the thunder;

void game()
{
	//Implementation of minesweeping game
	
	//The array mine is used to store the information of the arranged mine
	char mine[ROWS][COLS] = { 0 };

	//The array show 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(mine, ROW, COL); 
	show_board(show, ROW, COL);
	//Check thunder
	find_mine(mine, show, ROW, COL);
}

Write into main function

int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	//do while loop to judge the game process
	do
	{
		menu();//An option to remind you if you need to start the game
		printf("Please select:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("Game start:\n");
			game();//Game implementation
			break;
		case 0:
			printf("Exit the game \n");
			break;
		default:
			printf("Selection error \n");
			break;
		}
	} while (input);
	return 0;
}

(2) create a game C file is used to realize the game

1. Initialization

#include"game.h"

//initialization
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;
		}
	}
}

2. Printed functions

In order to more easily see the coordinates of each grid, print out the rows and columns of the chessboard.

//Print
void show_board(char arr[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;

	printf("-------mine clearance--------\n");//Split row

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

	for (i = 1; i <= row; i++)//Print checkerboard content
	{
		printf("%d ", i);//Print line coordinates
		for (j = 1; j <= col; j++)
		{
			printf("%c ", arr[i][j]);
		}
		printf("\n");
	}
	printf("-------mine clearance--------\n");//Split row
}

3. Location of randomly generated mines

Random coordinates are generated by rand function. Character 0 indicates that it is not thunder and character 1 indicates that it is thunder.

//Random generation of layout lightning
void set_mine(char mine[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;//Number of layout mines
	int x = 0;
	int y = 0;
	while (count)
	{
		x = rand() % row + 1;//Generate random coordinates
		y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';//Lay thunder
			count--;
		}
	}
}

4. The player controls the location of the mine

The game process is divided into three situations:

First, the game continues. Within the specified range of coordinates, if the coordinates specified by us are not thunder, it is judged that the eight coordinates around this coordinate are the number of thunder, and the characters representing this number are stored on this coordinate. We know that character 1 minus character 0 equals 1. We can define a judgment function to return the number of mines in the surrounding 8 coordinates.

Second, the situation of mine sweeping failure and the end of the game.

Third, the success of minesweeping and the end of the game. After all other positions except mine positions are specified, that is, after the grid is used up, it indicates that the mine sweeping is successful.

//Check thunder

int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
	//'1'='0'=1
	//Judge the number of mines in the surrounding 8 coordinates
	return mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + 
		   mine[x + 1][y] + 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 grids
	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("Minesweeping failed!\n");
				show_board(mine, ROW, COL);
				break;
			}
			else
			{
				int count = get_mine_count(mine, x, y);
				show[x][y] = count + '0';//Put a character
				show_board(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("Illegal coordinates, please re-enter.\n");
		}
	}
	if (win == row * col - EASY_COUNT)//The grid is full and there is no thunder
	{
		printf("Minesweeping succeeded!\n");
		show_board(show, ROW, COL);
	}
}

(3) Header file game H function declaration

#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

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

//Print
void show_board(char arr[ROWS][COLS], int row, int col);

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

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

4, Test results

(1) Select Start Game

(2) Failure of mine clearance

(3) Successful mine clearance

Set the number of mines to 80 to test the success of mine clearance.

 

 

 

Topics: C Game Development