【c语言 扫雷】

1.扫雷描述

9*9的方块矩阵中随机布置10个地雷,由玩家逐个翻开方块。翻开的格子下是地雷,游戏失败;翻开的格子下不是地雷,则会标记周围一圈格子中雷的个数

2.代码方案

实现扫雷游戏的关键点梳理:场景构造,放置雷,排查雷

2.1场景构建

void InitBoard(char board[Rows][Cols], int rows, int cols, char set);
void DisplayBoard(char board[Rows][Cols],int row,int col);

2.2放置雷

void SetMine(char board[Rows][Cols], int row, int col);

2.3排查雷

void FinMine(char mine[Rows][Cols],char show[Rows][Cols],int row,int col);

3.代码实现

主函数的框架

#include"game2.h"
void meau()			//菜单
{
	printf("*********************\n");
	printf("****** 1.play  ******\n");
	printf("****** 2.exit  ******\n");
	printf("*********************\n");

}
void game()
{
	//创建雷
	char mine[Rows][Cols];
	//显示雷的个数
	char show[Rows][Cols];
	//初始化
	InitBoard(mine, Rows, Cols,'0');
	InitBoard(show, Rows, Cols,'*');
	 //打印显示雷个数的棋盘
	DisplayBoard(show, Row, Col);
	//布置雷
	SetMine(mine, Row, Col);
	DisplayBoard(mine, Row, Col);
	//排查雷
	FinMine(mine, show, Row, Col);
}
int main()
{
	int input;
	do
	{
		srand((unsigned int)time(NULL));
		meau();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出程序\n");
			break;
		default:
			printf("输入错误,请重新输入:");
			break;
		}
	} while (input);
	return 0;
}

头文件

#pragma once
#define _CRT_SECURE_NO_WARNINGS

#define Row 9		//棋盘大小
#define Col 9
#define Rows Row + 2	//避免排查雷时的越界访问
#define Cols Col + 2
#define Count 80		//雷的数量

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



//初始化
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 board[Rows][Cols], int row, int col);

//排查雷
void FinMine(char mine[Rows][Cols],char show[Rows][Cols],int row,int col);


void InitBoard(char board[Rows][Cols], int rows, int cols, char set);

void InitBoard(char board[Rows][Cols], int rows, int cols, char set)
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}

void DisplayBoard(char board[Rows][Cols],int row,int col);

/*
1.函数DisplayBoard包含三个for循环
2.第一个for 用于显示出棋盘的列数
3.第二个for 用于显示出棋盘的行数,
*/
void DisplayBoard(char board[Rows][Cols], int row, int col)
{
	printf("***********扫雷***************\n");
	for (int i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (int i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (int j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

void SetMine(char board[Rows][Cols], int row, int col);

void SetMine(char board[Rows][Cols], int row, int col)
{
	int count = Count;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}

void FinMine(char mine[Rows][Cols],char show[Rows][Cols],int row,int col);

int GetMineCount(char mine[Rows][Cols], int x, int y)		//判断雷有多少个
{
	return (
		mine[x - 1][y] +
		mine[x - 1][y - 1] +
		mine[x - 1][y + 1] +
		mine[x][y - 1] +
		mine[x][y + 1] +
		mine[x + 1][y - 1] +
		mine[x + 1][y] +
		mine[x + 1][y + 1] - 8 * '0'
		);
}
void FinMine(char mine[Rows][Cols], char show[Rows][Cols], int row, int col)
{
	int x, y;
	int win = 0;
	while (win<row*col-Count)
	{
		printf("请输入要查找的坐标:>");
		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("输入的坐标非法,请重新输入:");
		}
	}
	if (win == row * col - Count)
	{
		printf("恭喜你排雷成功了\n");
	}
}

4.运行实例演示

step 1: 启动在这里插入图片描述

step 2:找出雷的位置
在这里插入图片描述
step 3:排雷成功
在这里插入图片描述

5.其他说明

方案可以优化的地方:当排查的位置没有雷且该位置周围没有雷时,就展开其周围一圈的8个坐标,然后看这8个坐标是否可以再逐个向其自身周围接着展开。
注:该代码只在vs2022运行过

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值