C语言-扫雷

本章重点

本次扫雷只是增加了一个递归展开功能,以后会加入标记等功能

主函数

srand是以时间戳随机设计一个种子

int main()
{
	srand((unsigned)time(NULL));
	int input = 0;
	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;
}

menu()

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

game()

void game()
{
	char mine[ROWS][COLS];
	char show[ROWS][COLS];

	//初始化数组
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');

	//打印数组
	//PrintBoard(mine, ROW, COL);
	PrintBoard(show, ROW, COL);

	//埋雷
	MineBoard(mine);

	//PrintBoard(mine, ROW, COL);

	//排查雷
	SearchMine(mine, show);

	PrintBoard(show, ROW, COL);
	

}

初始化数组

void InitBoard(char board[ROWS][COLS], int row, int col, char ch)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			board[i][j] = ch;
		}
	}
}

要有两个数组,且每个数组都要比设定的棋盘大一圈,就是行+2,列+2
初始化为数组mine全部变成‘0’,show全部变成‘*’

PrintBoard打印棋盘

void PrintBoard(char board[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 = 1;
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("-------扫雷游戏---------\n");

}

埋雷

void MineBoard(char mine[ROWS][COLS])
{
	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--;
		}
	}
}

计算输入的坐标周围有几个雷

int AroundMine(char mine[ROWS][COLS], int x, int y)
{
	return 	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] +
			mine[x][y - 1] +
			mine[x][y + 1] - 8 * '0';

}

递归展开

如果输入的坐标周围没有雷,把本身变成‘ ’,然后在递归这八个坐标

void open(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y,int row,int col)
{

	int ret = AroundMine(mine, x, y);
	if (ret == 0)
	{
		show[x][y] = ' ';
		if (( x - 1 >0 && y - 1 > 0) && show[x - 1][y - 1] == '*')
		{
			open(mine, show, x - 1, y - 1,row,col);
		}
		if ((x - 1 > 0 && y > 0) && show[x - 1][y] == '*')
		{
			open(mine, show, x - 1, y , row, col);
		}
		if ((x - 1 > 0 && y + 1 <= col) && show[x - 1][y + 1] == '*')
		{
			open(mine, show, x - 1, y + 1, row, col);
		}
		if ((x - 1 > 0 && y - 1 > 0) && show[x][y - 1] == '*')
		{
			open(mine, show, x - 1, y - 1, row, col);
		}
		if ((x > 0 && y + 1 <= col) && show[x ][y + 1] == '*')
		{
			open(mine, show, x , y + 1, row, col);
		}
		
		if( (x + 1 <=row && y - 1 > 0) && show[x + 1][y - 1] == '*')
		{
			open(mine, show, x + 1, y - 1, row, col);
		}
		if ((x + 1 <= row && y > 0) && show[x + 1][y] == '*')
		{
			open(mine, show, x + 1, y , row, col);
		}
		if (( x + 1 <= row &&  y + 1 <= col) && show[x + 1][y + 1] == '*')
		{
			open(mine, show, x + 1, y + 1, row, col);
		}
	}

	else
	{
		show[x][y] = ret + '0';	//如果不是'0',则显示雷的个数
	}
	
}

判断玩家输入的个数和递归展开后剩下可使用的个数

int IsWin(char show[ROWS][COLS], int row, int col )		//找到被改变的格子
{
	int count = 0;
	for (int i = 1; i <= row; i++)
	{
		for (int j = 1; j <= col; j++)
		{
			if (show[i][j] != '*')
			{
				count++;
			}
		}
	}
	if (count == ROW * COL - EASY_COUNT)
	{
		return 0;
	}
	return count;
}

排查雷

void SearchMine(char mine[ROWS][COLS], char show[ROWS][COLS])
{
	int number = ROW * COL - EASY_COUNT;
	while (number)
	{
		int x = 0;
		int y = 0;
		printf("请输入你要排查的坐标:");
		scanf("%d %d", &x, &y);
		if ((x >= 1 && x <= 9) && (y >= 1 && y <= 9))
		{
			if (mine[x][y] == '1')
			{
				
				break;
			}
			else
			{
				int count = AroundMine(mine, x, y);
				open(mine, show, x, y, ROW, COL);	//展开

				PrintBoard(show, ROW, COL);
				
				number = IsWin(show, ROW, COL);
			}
		}
		else
		{
			printf("坐标非法,请重新输入\n");
		}
	}
	if (number > 0)
	{
		printf("你被炸死了\n");
		PrintBoard(mine, ROW, COL);

	}
	else
	{
		printf("恭喜你,你赢了\n");
	}
}

完整代码

上传到我的gitee上了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值