扫雷游戏的实现

本文详细介绍了使用C语言实现的扫雷游戏,包括菜单操作、9*9棋盘布局、随机布雷、排查雷规律以及游戏流程。涉及文件结构、函数声明与定义,展示了如何使用多文件管理游戏逻辑。
摘要由CSDN通过智能技术生成

扫雷游戏功能说明

存在菜单实现开始游戏,退出游戏功能

扫雷棋盘时9*9的格子

随即布置10个雷

排查雷的规律

  1. 若果排查的位置不是雷,就显示排查位置周围有几个雷。
  2. 如果排查位置是雷,就炸死,那么游戏失败。
  3. 把除了10个雷之外的所有非雷都找出来,就算排雷成功,胜利,游戏结束。

文件结构

学过多文件的形式对函数的声明和定义,那么我们将整个程序设计成三个文件

test.c//运行游戏,测试逻辑
game.c//游戏的逻辑
game.h//游戏的逻辑

扫雷代码实现

game.h

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

#define ROW 9//定义一个符号 ROW代表9,
#define COL 9
#define ROWS ROW+2
#define COLS COL+2

#define EASY_COUNT 10//雷


//棋盘初始化的函数
void InitBoard(char arr[ROWS][COLS], int rows, int cols,char set);

//打印棋盘
void DisplayBoard(char arr[ROWS][COLS], int row, int col);

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

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

game.c

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

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

void SetMine(char arr[ROWS][COLS], int row, int col) 
{
	int count = EASY_COUNT;
	while (count)
	{
		//布置雷的范围为1-9,所以生成随机数1-9
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (arr[x][y] == '0')//如果随机生成的坐标里面没有雷,就执行语句
		{
			arr[x][y] = '1';
			count--;
		}
	}
}

static int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
	int i = 0;
	int count = 0;
	for (i = x - 1; i <= x + 1; i++)
	{
		int j = 0;
		for (j = y - 1; j <= y + 1; j++)
		{
			count = count + (mine[i][j] - '0');
		}
	}
	return count;
}
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("请输入要排查雷的坐标:\n");
		scanf("%d %d", &x, &y);
		//判断坐标的有效性
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (show[x][y] == '*')
			{
				//点到雷,炸死了
				if (mine[x][y] == '1')
				{
					printf("很遗憾,你被炸死了\n");
					DisplayBoard(mine, ROW, COL);//死得明白
					break;
				}
				else//不是雷
				{
					//该坐标不是雷,就得统计该坐标周围有几颗雷
					int count = GetMineCount(mine, x, y);
					show[x][y] = count + '0';
					DisplayBoard(show, ROW, COL);//显示棋盘
					win++;
				}
			}
			else
			{
				printf("该坐标已经被排查,重新输入坐标\n");
			}
		}
		else
		{
			printf("坐标无效,请重新输入\n");
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("恭喜你,排雷成功\n");
		DisplayBoard(mine, ROW, COL);
	}
}

test.c

void menu()//菜单
{
	printf("*************************\n");
	printf("******* 1. play  ********\n");
	printf("******* 0. exit  ********\n");
	printf("*************************\n");
}
void game()//扫雷游戏
{
	//完成扫雷游戏
	//mine数组中存放布置好的雷的信息
	char mine[ROWS][COLS] = {0};

	//show数组中存放排查出的雷的信息
	char show[ROWS][COLS] = {0};
	
    //初始化棋盘 mine
	InitBoard(mine,ROWS,COLS,'0');
	InitBoard(show,ROWS,COLS,'*');


	//布置雷
	SetMine(mine,ROW,COL);

	//打印棋盘
	DisplayBoard(show, ROW, COL);

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

void test()//逻辑
{
	int input = 0;
	srand((unsigned)time(NULL));
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d",&input);
		switch (input)
		{
		case 1:
			//printf("开始扫雷游戏\n");
			game();
			break;
		case 0:
			printf("游戏结束,退出游戏\n");
			break;
		default:
			printf("选择错误,重新选择\n");
			break;
		}
	} while (input);
}
int main()
{
	test();
	return 0;
}

具体细节,在下一篇,尽请期待哦

以上内容,如果有任何问题,请留言或私信我。期待与大家沟通交流。

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值