Simple implementation of minesweeper game


[Project introduction]: Use the basic knowledge of two-dimensional arrays and functions in C language to simply implement a minesweeper game. Its basic function is to initialize the minefield and display area, arrange the minefield with random numbers, and judge whether the mine clearance is successful. This project is suitable for beginners of C language, and interested friends can learn about it.

[Project defect]: Only one mine can be fired at a time, instead of a mine that expands outward.

[Project Expansion]: ①The first mine clearance will not blow up (attracting players doubt )
                        ②If the current coordinates are not mines, expand the place with the current coordinates as the center, and you can discharge a mine at a time.

[Code Frame]:
①Header file (game.h)
#ifndef _GAME_H_
#define _GAME_H_

#pragma warning(disable:4996)
#include<stdio.h>
#include<time.h>
#include<stdlib.h>

enum
{
	EXIT,
	START
};

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

#define mine_count 10

typedef unsigned int uint;

void init_board(char board[ROWS][COLS], int rows, int cols, char set);
void print_board(char board[ROWS][COLS], int row, int col);
void  set_mine(char mine[ROWS][COLS], int row, int col);
void find_mine(char board[ROWS][COLS], char mine[ROWS][COLS], int row, int col);
int get_mine_count(char mine[ROWS][COLS], int x, int y);
//declaration of each function

#endif //_GAME_H_

②Test file (test.c)
#include"game.h"

void meun()
{
	printf("****** Minesweeper game******\n");
	printf("**************************\n");
	printf("******   1.PLAY     ******\n");
	printf("******   2.EXIT     ******\n");
	printf("**************************\n");

}
void game()
{
	char board[ROWS][COLS] = { ' 0 ' };
	char mine[ROWS][COLS] = { ' 0 ' };

	init_board(board, ROWS, COLS, '*');
	init_board(mine, ROWS, COLS, '0');
	set_mine(mine, ROW, COL);
	print_board(mine, ROW, COL);
	print_board(board, ROW, COL);
	find_mine(board, mine, ROW, COL);
	

}

intmain()
{
	int input = 0;
	srand((uint)time(NULL));
	do{
		meun();
		printf("Please choose: ");
		scanf("%d", &input);
		switch (input)
		{
		case START:
			game();
			break;
		case EXIT:
			break;
		default:
			printf("Incorrect input, please try again!\n");
			break;
		}
	} while (input);
}

[Function implementation]:
①Initialize the minefield and display area: traverse with a two-dimensional array
void init_board(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;
		}
	}
}

②Formatted printing minefields and display areas
void print_board(char board[ROWS][COLS], int row, int col)
{
	{
		int i = 0;
		int j = 0;
		for (i = 0; i <= row; i++)
		{
			printf("%d ", i);//Comment the line and column numbers to the minefield
		}
		printf("\n");
		for (i = 0; i <row; i++)
		{
			printf("%d ", i+1);
			for (j = 0; j < col; j++)
			{
				printf("%c ", board[i + 1][j + 1]);
			}
			printf("\n");
		}
	}
}

Show results:

③ Mining in minefields
void set_mine(char mine[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int count = mine_count;
	while (count)
	{
	     x = rand() % row  + 1;
		 y = rand() % col + 1;
		 //generate coordinates from random numbers
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

④Start demining
static int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
	int count = (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';
	return count;
}
//Get the number of mines around (x,y) coordinates
void find_mine(char board[ROWS][COLS], char mine[ROWS][COLS], int row, int col)
{
	int sum = 0;
	int x = 0;
	int y = 0;
	while (sum <= (row*col - mine_count))
	{
		printf("Please enter the coordinates to clear mine:\n");
		scanf("%d%d", &x, &y);
		if (mine[x][y] == '1')
		{
			printf("Sorry, you were killed!\n");
			break;
		}
		else
		{
			board[x][y] = get_mine_count(mine, x, y) + '0';
			print_board(board, row, col);
			sum++;
		}
	}
	if (sum > (row*col - mine_count))
	{
		printf("Congratulations, mine clearance is successful!\n");
		print_board(mine, row, col);
	}
}

【operation result】:

For the complete code, please go to --> Minesweeper game source code . Welcome to reprint, reprint please declare the source.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325951174&siteId=291194637