C Language: A Simple Minesweeper Game

       Everyone must have played the minesweeper game, like some big guys, they can clear the level in ten seconds, give them a thumbs up, then let’s design a minesweeper game, how to achieve it, follow my steps, and see how to use C language How to implement a simple minesweeper game.

80843dfeb63b46d9939227cd60408c82.webp

 

 One: The framework of the minesweeper game

b18f86d60d084fe9bcc0968fa48edb75.png

Two: Implementation of test.c

①: Write out the main function.

The main function is mainly able to loop, continue to play, and can include all functions of minesweeping.

792ef95f917b43c4b6b82f3244e28555.png

②: Second, the implementation of the game() function. (realize playing games)

//1. Initialize the board
void InitBord(char board[ROWS][COLS], int row, int col, char set);

//2. Print the board
void DisPlayBoard(char board[ROWS][COLS], int row, int col);

//3. Arrange mine
void SetMine(char board[ROWS][COLS], int row, int col);

//4.排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

void game()
{
	char mine[ROWS][COLS] = { '0' };//存放布好的雷
	char show[ROWS][COLS] = { '*' };//存放排查出雷的信息
	
	//初始化棋盘
	InitBord(mine, ROWS, COLS, '0');//存放布好的雷
	InitBord(show, ROWS, COLS, '*');//存放排查出雷的信息
	
	//打印棋盘
	printf("雷的布置为\n");
	DisPlayBoard(mine, ROW, COL);//存放布好的雷
	printf("排查出雷的信息\n");
	DisPlayBoard(show, ROW, COL);//存放排查出雷的信息

	//布置雷
	SetMine(mine, ROW, COL);
	DisPlayBoard(mine, ROW, COL);//存放布好的雷

	//排查雷
	FindMine(mine, show,ROW,COL);
}

Attention to details: The arrangement of mines does not need to be printed out, here is just a demonstration, where ROWS is column = 11, COLS is row = 11

ROW is column=9, COL is row=9, so why should it be initialized like this?

71ad87c774cf4cab8a56f6fabeb0129b.png

 Three: Implementation of game.c

①: Print menu

//打印菜单
void menu()
{
	printf("*****************************\n");
	printf("*******     1.play     ******\n");
	printf("*******     0.exit     ******\n");
	printf("*****************************\n");
}

②: Initialize the board

Details: It should be initialized to a grid of 11×11.
    InitBord(mine, ROWS, COLS, '0'); store the laid mine
    InitBord(show, ROWS, COLS, '*'); store the information of mine detection

//初始化棋盘
void InitBord(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;
		}
	}
	打印(测试)
	//for (i = 0; i < row; i++)
	//{
	//	for (j = 0; j < col; j++)
	//	{
	//		printf("%c",board[i][j]);

	//	}
	//	printf("\n");
	//}
}

③: Print the chessboard

Details: You can print the layout of the initialization mine and the chessboard layout first.

In order to make it look better, some output of ranks and columns have been added.

2c8d1858d97f4c0fa7615abe29cf6f76.png

//打印棋盘
void DisPlayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 1;
	int j = 1;
	printf("-----扫雷 游戏-----\n");
	for (i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

④: Arrange mines.

Details:

A #define Thunder 10 is defined to make the number of thunder 10,

#include<stdlib.h> #include<time.h> Using two functions, can generate random numbers.

In the main function, write srand((unsigned int)time(NULL));

//布置雷
void SetMine(char board[ROWS][COLS], int row, int col)
{
	int thunder = Thunder;
	int count = 1;
	while (count <= thunder)
	{
		int x = rand() % row + 1; //范围 1~9
		int y = rand() % row + 1; //范围 1~9
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count++;
		}
	}
}

⑤: Mining check (emphasis)

Pay attention to details:

0a9a5aa5ad7948ce94c0fc04498c2408.png

 faef598522fb470ba0f2a459ed61c6b3.png

 

//排查雷
//计算周围雷的个数
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
	return (mine[x][y + 1] + mine[x][y - 1] +
			mine[x - 1][y] + mine[x + 1][y] +
			mine[x - 1][y + 1] + mine[x + 1][y + 1] +
			mine[x - 1][y - 1] + mine[x + 1][y - 1] -
			8 * '0');
}

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 1;
	int y = 1;
	int win_count = 0;
	while (win_count <= row*col - Thunder)
	{
		printf("请玩家输入坐标:");
		scanf("%d %d", &x, &y);

		//判断玩家输入的坐标是否正确
		if ((x < 1 && x > 9) && (y < 1 && y > 9))
		{
			printf("输入的坐标有误,请重新输入:");
			scanf("%d %d", &x, &y);
		}

		//判断玩家是否踩到雷
		if (mine[x][y] == '1')
		{
			printf("你踩到雷了,游戏结束\n");
			DisPlayBoard(mine, ROW, COL);//存放布好的雷
			break;
		}
		else
		{
			int ret = GetMineCount(mine, x, y);//判断输赢函数
			show[x][y] = ret + '0';
			win_count++;
			DisPlayBoard(show, ROW, COL);//存放排查出雷的信息
			
			if (win_count == row * col - Thunder)
			{
				printf("恭喜你,排雷成功^-^\n");
				printf("---查看雷的排布---\n");
				DisPlayBoard(mine, ROW, COL);
				break;
			}
		}
	}
}

Four: game.h implementation

#pragma once

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

#define ROW 9           //列      
#define COL 9           //行
#define ROWS ROW + 2    //列 11 
#define COLS COL + 2    //行 11
#define Thunder 10      //雷的个数

//1.打印菜单
void menu();

//2.初始化棋盘
void InitBord(char board[ROWS][COLS], int row, int col, char set);

//3.打印棋盘
void DisPlayBoard(char board[ROWS][COLS], int row, int col);

//4.布置雷
void SetMine(char board[ROWS][COLS], int row, int col);

//5.排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

        Before you know it, it has come to the end. As a novice, I may not write very well. If there is something wrong, please leave a message to me, thank you.7221dfce3a9741758214cd250913d198.pngfbd9f005bc434c6194c5e57df8ebe7ae.png

d2e0d89ca18346e297af6ae28ad277b6.gif

 

 

Guess you like

Origin blog.csdn.net/AAlykk/article/details/130578044