关于扫雷的核心点

文章目录

  • 扫雷菜单
  • test.c
    • 排查雷
  • game.c
    • 初始化棋盘Initboard
    • 打印棋盘DisplayBoard
    • 布置雷SetMine
    • 排查雷FindMine
    • 计算雷的个数 get_mine_count
  • game.h
    • 核心函数的声名
  • 扫雷完整代码

关于扫雷的核心点_第1张图片

扫雷菜单

test.c

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

}

	void game()
	{
		printf("扫雷游戏\n");
	}
	void test()
	{
		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);
	}
	
int main()
{
	test();

	return  0; 
}

这里我们需要两个棋盘 ,一个布置雷 ,一个排查雷

排查雷

统计周围8个坐标去确定雷,但是有可能会数组越界,为了防止坐标越界,我们给原先的9*9数组行增加2行,列增加2列。
关于扫雷的核心点_第2张图片


game.c

初始化棋盘Initboard

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

打印棋盘DisplayBoard

我们打印棋盘只需要打印9*9的棋盘 ,所以DisplayBoard 函数的参数时ROW COL而不是ROWS COLS
我们还需要控制列号。

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("---------扫雷----------\n");
	//控制列号
	for (j = 0; j <= col; j++)
	{
		printf("%d ", j);
	}
	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");
}

布置雷SetMine

创建一个 9* 9的数组
布置雷 这里使用rand函数 ,rand函数是专门用来生成随机数的 。rand函数返回的是0~RAND_MAX(32767)之间的一个随机数。rand函数在使用之前需要一个srand函数来设置随机数的生成器 。srand( ( unsight int ) time ( NULL) ) srand函数在程序中只需要调用一次就可以了不需要频繁调用否则生成的随机数是不随机的。
c语言中time函数会返回时间戳。

void SetMine(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--;
		}
	}
}

排查雷FindMine

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("请输入要排查的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (show[x][y] != '*')
			{
				printf("该坐标被排查过了\n");
				continue;
			}
			if (mine[x][y] == '1')
			{
				printf("很遗憾,你被炸死了\n");
				DisplayBoard(mine, ROW, COL);
				break;
			}
			else
			{
				int n = get_mine_count(mine, x, y);
				show[x][y] = n+'0';
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("坐标非法,重新输入\n");
		}
	}
	if (win == (row * col - EASY_COUNT))
	{
		printf("恭喜你,排雷成功\n");
		DisplayBoard(mine, ROW, COL);
	}
}

判断坐标是否合法且未被排查过
判断游戏输赢
我们假设是9 *9的棋盘 就是81个位置,如果布置10个雷 也就是说有71个不是雷 ,那我们只需要找出71个不是雷的位置就行了

计算雷的个数 get_mine_count

int get_mine_count(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');
}

关于扫雷的核心点_第3张图片
统计周围有几个雷
将周围的数全部加起来再- 8*‘0’就把字符转化为数字 ,数字是几就有几个雷

game.h

核心函数的声名

#include 
#include 
#include 

#define ROW 9
#define COL 9

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

#define EASY_COUNT 79

//初始化棋盘
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 mine[ROWS][COLS], int row, int col);

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

扫雷完整代码

  • test.c
#include "game.h"

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

void game()
{
	//1. 需要存放布置好的雷的信息,存放排查出的雷的信息,我们需要2个二维数组
	//2. 排查坐标的时候,为了防止坐标越界,我们给数组的行增加2行,列增加了2列
	char mine[ROWS][COLS] = {0};//布置好的雷的信息
	char show[ROWS][COLS] = {0};//排查出的雷的信息
	//初始化棋盘
	InitBoard(mine, ROWS,COLS, '0');
	InitBoard(show, ROWS,COLS, '*');

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

	//布置雷
	SetMine(mine, ROW, COL);
	DisplayBoard(mine, ROW, COL);
	//排查雷
	FindMine(mine, show, ROW, COL);
}

void test()
{
	srand((unsigned int)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);
}

int main()
{
	test();
	return 0;
}

  • game.h
#include 
#include 
#include 

#define ROW 9
#define COL 9

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

#define EASY_COUNT 79

//初始化棋盘
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 mine[ROWS][COLS], int row, int col);

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

  • game.c
#include "game.h"

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

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("---------扫雷----------\n");
	//控制列号
	for (j = 0; j <= col; j++)
	{
		printf("%d ", j);
	}
	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 SetMine(char mine[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;
	while (count)
	{
		//1. 生成随机下标
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		//2. 布置雷
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

int get_mine_count(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 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("请输入要排查的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (show[x][y] != '*')
			{
				printf("该坐标被排查过了\n");
				continue;
			}
			if (mine[x][y] == '1')
			{
				printf("很遗憾,你被炸死了\n");
				DisplayBoard(mine, ROW, COL);
				break;
			}
			else
			{![在这里插入图片描述](https://img-blog.csdnimg.cn/91fc3da3b88447ab8010f5ccfee27233.jpeg#pic_center)

				int n = get_mine_count(mine, x, y);
				show[x][y] = n+'0';
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("坐标非法,重新输入\n");
		}
	}
	if (win == (row * col - EASY_COUNT))
	{
		printf("恭喜你,排雷成功\n");
		DisplayBoard(mine, ROW, COL);
	}
}

如果你觉得这篇文章对你有帮助,不妨动动手指给点赞收藏加转发,给鄃鳕一个大大的关注
你们的每一次支持都将转化为我前进的动力!!!

关于扫雷的核心点_第4张图片

你可能感兴趣的:(c++,c#,算法)