C language game-"minesweeper"

Game rules:
Take a 9*9 board as an example. There are 10 mines randomly distributed on the board. The player clicks on the board. If the clicked grid is a mine, the player will be blown up and the game will end; There are no mines on the grid, and the grids adjacent to the clicked grid (the clicked grid has eight grids diagonally up and down), then the total number of these mines will be displayed on the clicked grid. If there are no mines in the eight adjacent grids of the clicked grid, the chessboard will automatically expand until the grid adjacent to the expanded grid has mines. At this time, the last expanded grid shows the number of mines shared by its neighboring grids.

The code adopts a modular idea:
Insert picture description here
game.h module:
store header files and function declarations

#define _CRT_SECURE_NO_WARNINGS 1

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

#define EASY_COUNT 10
#define ROW 9
#define COL 9

#define ROWS ROW+2
#define COLS COL+2
//初始化
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col);

void SetMine(char mine[ROWS][COLS], int row, int col);

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS],int row,int col);

game.c module:
calls between functions during game runtime

#include "game.h"

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

void InitBoard(char board[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++)
		{
    
    
			board[i][j] = set;
		}
	}
}

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
    
    
	int i = 0;
	int j = 0;
	printf("----------------------------------\n");
	//列号的打印
	for (i = 0; i <= col; 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");
	}
	printf("----------------------------------\n");

}

void SetMine(char board[ROWS][COLS], int row, int col)
{
    
    
	//1. 随机找坐标布置雷
	//布置多少个雷 - 10
	int count = EASY_COUNT;
	while (count)
	{
    
    
		//布置成功一个雷,count--
		//1. 生产随机的坐标
		int x = rand()%row+1;//1-9
		int y = rand()%col+1;//1-9
		//2. 布置
		if (board[x][y] == '0')
		{
    
    
			board[x][y] = '1';
			count--;
		}
	}
}


int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
    
    
		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';
}

//
//扫雷游戏是怎么结束的?
//1. 炸死了
//2. 正常排查了所有不是雷的位置
//
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("请输入要排查的坐标:>");
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
    
    
			//判断x,y坐标处是否是雷
			if (mine[x][y] == '1')
			{
    
    
				printf("很遗憾,你被炸死了\n");
				DisplayBoard(mine, row, col);
				break;
			}
			else
			{
    
    
				//如果x,y坐标不是雷,就统计周围有几个雷
				int count = GetMineCount(mine, x, y);
				show[x][y] = count + '0';
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
		{
    
    
			printf("坐标非法\n");
		}
	}
	if (win == ROW*COL - EASY_COUNT)
	{
    
    
		printf("恭喜你,排雷成功\n");
		DisplayBoard(mine, row, col);
	}
}

void game()
{
    
    
	//真正扫雷的过程
	//创建2个数组
	//存放布置好的雷
	char mine[ROWS][COLS] = {
    
    0};//'0'
	//存放排查出来的雷的信息
	char show[ROWS][COLS] = {
    
    0};//'*'
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');
	DisplayBoard(show, ROW, COL);
	//布置好的雷的信息不应该轻易打印
	//DisplayBoard(mine, ROW, COL);
	//1. 布置雷
	SetMine(mine, ROW, COL);
	DisplayBoard(mine, ROW, COL);
	//2. 扫雷
	FindMine(mine, show, ROW, COL);
}

test.c module:
the entry point of the program, write the main function, used to call the functions needed for this game to run

#include "game.h"

int main()
{
    
    
	int input = 0;
	//time_t -- 整形
	srand((unsigned int)time(NULL));//设置随机数的生成起点的

	do
	{
    
    
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
    
    
		case 1:
			game();//扫雷游戏
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误!\n");
			break;
		}
		//
	} while (input);
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_45658339/article/details/108690857