C语言小游戏 -- “扫雷”

游戏规则:
以9*9棋盘为例,棋盘上随机分布着10个地雷,玩家在棋盘上进行点击,如果被点击的格子是地雷,则玩家被炸“死”,游戏结束;如果被点击的格子上没有地雷,与被点击的格子相邻的格子(被点击格子的上下左右还有斜向,共八个格子)有地雷,则在被点击的格子上显示这些地雷的总数,如果与被点击的格子相邻的八个格子都没有地雷,则棋盘自动展开,直到与展开的格子相邻的格子有地雷才停止。此时最后被展开的格子显示其相邻格子共有的地雷数。

代码采用模块化思想:
在这里插入图片描述
game.h 模块:
存放 头文件和函数声明

#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 模块:
游戏运行时 函数之间的调用

#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 模块:
程序的入口,写main函数,用来调用本次游戏运行所需的函数

#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;
}

猜你喜欢

转载自blog.csdn.net/qq_45658339/article/details/108690857