扫雷的简单实现

扫雷

一.前期准备

在实现扫雷代码时,我们使用三个文件,两个c类型的文件,一个头文件。在这里插入图片描述

思路

  1. 要实现的是扫雷的简单版,所以是99的,随机布置十颗雷在这里插入图片描述
    但是要充分考虑到边界情况,所以就可以直接把棋盘设计成11
    11的来方便使用,可以直接生成全局变量在这里插入图片描述

  2. 在实现时,可以用1来表示雷,用0来表示非零。经过实际检验用字符0和1来更加的方便。

  3. 在实现游戏时,可以使用两个字符数组,用来存放一个是实际雷的信息,一个是用户排查雷的信心

  4. 因为是分模块来写代码,所以可以将头文件放在game.h中,然后在其他文件中引用就可以了

#include "game.h"

二.主函数的实现

  1. 对于主函数内部,可以使用do while来实现,因为要无条件的先执行一次,然后根据选择语句stitch来确定实际内容
  2. 为了方便可以在游戏进行前先打印菜单
void mune()
{
	printf("*******************\n");
	printf("**** 1. play  *****\n");
	printf("**** 0. exit  *****\n");
	printf("*******************\n");
}

现在的代码应该是这个样子的

int main()
{
	int input = 0;//用来存放用户输入的数据
	do {
		mune();
		scanf("%d", &input);
		switch (input) 
		{
		case 1:
			printf("游戏\n");
			break;
		case 2:
			printf("退出游戏\n");
			break;
		default :
			printf("输入错误请重新输入:\n");
			break;
		}
	} while (input);

	return 0;
}

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

三.game函数的实现

逻辑没有问题后我们就可以继续来完善我们的代码,我们将输出游戏的地方写成game函数,之后完成代码
1.先写出一个代码,来初始化我们的数组,初始化数组的代码

void InitArr(char domn[ROWS][COLS], int row, int col, char ret)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			domn[i][j] = ret;
		}
	}
}

2.之后我们完成打印数组的函数

void DisArr(char domn[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			printf("%d ", domn[i][j]);
		}
		printf("\n");
	}
}

对于打印函数,我们可以将其优化

void DisArr(char domn[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("-----------------------\n");
	for (i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", domn[i][j]);
		}
		printf("\n");
	}
	printf("-----------------------\n");

}

优化前后对比
在这里插入图片描述

3.布置雷,这里为了生成随机数使用了rand函数,在使用rand的前要调用srand,一个整个程序只需要调用一次就行了,所以可以直接放在主函数的开端

srand((unsigned int)time(NULL));

布置雷的代码

void PutArr(char domn[ROWS][COLS], int row, int col)
{


	int count = EASY_COUNT;

	while (count)
	{
		int x = rand() % ROW + 1;
	    int y = rand() % COL + 1;
		if (domn[x][y] != '1')
		{
			domn[x][y] = '1';
			count--;
		}
	}

}
  1. 排查雷
void InvesArr(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	while (win < (COL * ROW - EASY_COUNT))
	{
		printf("请输入排查的坐标:\n");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾你被炸死了,雷的位置是:\n");
				DisArr(mine, ROW, COL);
				break;
			}
			else {
				win++;
				int ret = num(mine,ROW,COL);//周围的雷数
				show[x][y] = ret + '0';
				DisArr(show, ROW, COL);
			}
		}
	}
	if (win == (COL * ROW - EASY_COUNT))
	{
		printf("恭喜你排完所有雷,游戏结束~\n");
		DisArr(mine, x, y);
	}

}

这样一个简单的扫雷游戏我们就完成了

总结所有代码

  1. text.c文件中的代码
#include "game.h"

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

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

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

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

	//布置雷
	PutArr(mine, COL, ROW);
	//DisArr(mine, ROW, COL);

	//排查雷
	InvesArr(mine, show, ROWS, COLS);

}
int main()
{
	int input = 0;//用来存放用户输入的数据
	srand((unsigned int)time(NULL));
	do {
		mune();
		scanf("%d", &input);
		switch (input) 
		{
		case 1:
			game();
			break;
		case 2:
			printf("退出游戏\n");
			break;
		default :
			printf("输入错误请重新输入:\n");
			break;
		}
	} while (input);

	return 0;
}
  1. game.h中的代码
#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 InitArr(char domn[ROWS][COLS], int row, int col, char ret);

//打印数组
void DisArr(char domn[ROWS][COLS], int row, int col);

//放置雷
void PutArr(char domn[ROWS][COLS], int row, int col);

//排查雷
void InvesArr(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
  1. game.c中的代码
#include "game.h"

void InitArr(char domn[ROWS][COLS], int row, int col, char ret)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			domn[i][j] = ret;
		}
	}
}

void DisArr(char domn[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("-----------------------\n");
	for (i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", domn[i][j]);
		}
		printf("\n");
	}
	printf("-----------------------\n");

}


void PutArr(char domn[ROWS][COLS], int row, int col)
{


	int count = EASY_COUNT;

	while (count)
	{
		int x = rand() % ROW + 1;
		int y = rand() % COL + 1;
		if (domn[x][y] != '1')
		{
			domn[x][y] = '1';
			count--;
		}
	}

}

int num(char domn[ROWS][COLS], int x, int y)
{
	return domn[x - 1][y - 1] + domn[x - 1][y] +
		domn[x - 1][y + 1] + domn[x][y - 1] +
		domn[x][y + 1] + domn[x + 1][y - 1] +
		domn[x + 1][y] + domn[x + 1][y + 1] - 8 * '0';
}
void InvesArr(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	while (win < (COL * ROW - EASY_COUNT))
	{
		printf("请输入排查的坐标:\n");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾你被炸死了,雷的位置是:\n");
				DisArr(mine, ROW, COL);
				break;
			}
			else {
				win++;
				int ret = num(mine,ROW,COL);//周围的雷数
				show[x][y] = ret + '0';
				DisArr(show, ROW, COL);
			}
		}
	}
	if (win == (COL * ROW - EASY_COUNT))
	{
		printf("恭喜你排完所有雷,游戏结束~\n");
		DisArr(mine, x, y);
	}

}

总结

扫雷游戏的代码最基础的版本就是上述了,其中还有一些不足的地方希望能够指正。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十月三十二号的风情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值