C语言敲出扫雷游戏

该游戏我们也是利用数组完成,设置俩个数组一个mine数组,一个show数组。

mine数组存放雷,show数组显示棋盘并且显示该位置是否有雷或者显示该位置周围有几个雷。

数组大小有讲究,我们宏定义变量 ROW COL 为9 定义ROWS COLS为11,我们显示的是9X9的棋盘,也是将雷设置在9X9的位置内,但是我们设置数组是设置11X11,因为这样方便我们遍历9X9棋盘四边位置上某位置四周雷的数目,不然的话会发生越界错误。

对于雷的符号,我们设置空位置为 0,有雷的位置为1,这样易于我们统计某一位置周围有多少雷的数目。

重点在于扫雷函数,玩家输入x y位置,我们判断该位置是否有雷,否的话判断该位置周围有多少雷并在存于show数组display给玩家。并且我们输入一个位置当该位置不是雷的时候,计数器count++;若果该计数器count==col*row-EAXY_COUNT;我们就判断玩家赢。

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

#define ROW 9
#define COL 9

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

#define EASY_COUNT 10

//函数声明
void ChushiBoard(char board[ROWS][COLS],int rows,int cols,char set);
void Dayinboard(char board[ROWS][COLS], int row, int col);
void BuzhiBoard(char mine[ROWS][COLS], int row, int col);
void CaoleiBoard(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col);

void menu()
{
	printf("*****************************************\n");
	printf("*****************************************\n");
	printf("****  输入1开始游戏  输入0退出游戏  *****\n");
	printf("*****************************************\n");
	printf("*****************************************\n\n\n");
}

void game()
{
	char mine[ROWS][COLS];//存放雷
	char show[ROWS][COLS];//显示排查出来的雷
	//初始化
	ChushiBoard(mine, ROWS, COLS,'0');//'0'
	ChushiBoard(show, ROWS, COLS,'*');//'*'
	//布置雷
	BuzhiBoard(mine,ROW,COL);
	//Dayinboard(mine, ROW, COL);
	//打印棋盘
	Dayinboard(show, ROW, COL);
	//扫雷
	CaoleiBoard(mine,show,ROW,COL);
}



void test()
{
	int input = 0;
	do
	{
		menu();
    	printf("请输入:>");
    	scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n\n");
			break;
		default:
			printf("输入错误,重新输入\n\n");
			break;
		}
	} while (input);
}


int main()
{
	test();
	system("pause");
	return 0;
}


void ChushiBoard(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 Dayinboard(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 BuzhiBoard(char mine[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;
	while (count)
	{
		int x = rand()%row+1;
		int y = rand()%col+1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

static 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';
}



void CaoleiBoard(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("请输入要排查的坐标(格式:X空格X回车):>");
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
		{
			if (mine[x][y] == '1')
			{
				printf("       很遗憾,你被炸死了!!!\n");
				Dayinboard(mine, row, col);
				break;
			}
			else
			{
				int count = GetMineCount(mine,x,y);
				show[x][y] = count+'0';
				Dayinboard(show, row, col);
				win++;
			}

		}
		else
		{
			printf("输入的坐标非法\n");
		}
	}
	if (win == row*col - EASY_COUNT)
	{
		printf("     恭喜你,你排雷成功了!!!\n");
	}
}

运行结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值