迷宫游戏的原理!

此代码功能随机生成任意难度迷宫:) 所有迷宫原理八九不离十

#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#define NUMBER 50
#define RANK 80

char arr[NUMBER][NUMBER];

int fx[4] = { -1,1,0,0 };
int fy[4] = { 0,0,-1,1 };

/*起始坐标*/
int firstx = 0;
int firsty = 0;
/*终点坐标*/
int lastx = 0;
int lasty = 0;
void showmap();
bool makemap(int x, int y);//制作地图
bool unerror(int x, int y);//移除四角
bool wherewego(int x,int y);//路径
bool iswork(int x,int y); //走的判断


int main() {

	//初始化数组
	for (int i = 0; i < NUMBER - 1; i++)
	{
		for (int j = 0; j < NUMBER - 1; j++)
		{
			arr[i][j] = '#';
		}
	}

	srand(time(NULL));


	int x = 0;
	int y = 0;
	//获取起始坐标
	do
	{
		x = 0 + rand() % (NUMBER - 1);
		y = 0 + rand() % (NUMBER - 1);
		while (x != 0 && y != 0 && y != NUMBER - 2 && x != NUMBER - 2)
		{
			x = 0 + rand() % (NUMBER - 1);
			y = 0 + rand() % (NUMBER - 1);
		}
	} while (unerror(x, y) == false);//等于四角就重来

	arr[x][y] = '.';
	firstx = x;
	firsty = y;
	printf("迷宫起始坐标:(%d,%d)\n", x, y);
	/*迷宫函数调用*/
	makemap(x, y);

	/*随机获取迷宫终点坐标*/
	int counter = 0;
	do
	{
		lastx = 0 + rand() % (NUMBER - 1);
		lasty = 0 + rand() % (NUMBER - 1);
		while (lastx != 0 && lasty != 0 && lasty != NUMBER - 2 && lastx != NUMBER - 2)
		{
			lastx = 0 + rand() % (NUMBER - 1);
			lasty = 0 + rand() % (NUMBER - 1);
		}
		++counter;
	} while (unerror(lastx, lasty) == false && counter < NUMBER * 4);

	if (counter < NUMBER * 4)
	{
		arr[lastx][lasty] = '.';
		printf("迷宫终点坐标:(%d,%d)\n", lastx, lasty);
	}
	else
	{
		printf("生成失败!\n");
	}
	//起点打印个X作为标记
	arr[firstx][firsty] = 'X';
	/*走迷宫*/
	makemap(firstx, firsty);
	/*打印迷宫*/
	showmap();

	system("pause");

	return 0;
}




//判断越界问题
bool iswork( int x, int y)
{
	if (arr[x][y] != '#' && arr[x][y] != 'X' && x > 0 && y > 0 && x < NUMBER && y < NUMBER)
	{
		return true;
	}

	return false;

}

bool wherewego(int x, int y)
{
	int count = 0;
	for (int i = 0; i < 4; i++)
	{
		int nextx = x + fx[i];
		int nexty = y + fy[i];
		if (arr[nextx][nexty] == '#')
		{
			count++;
			if (count >= 3)
			{
				return true;
			}
		}
	}
	return false;


}

bool makemap(int x, int y)
{
	bool wherewego(int x, int y);//选择路径

	for (int j = 0; j < RANK; j++)
	{
		int i = rand() % 4;
		int nextx = x + fx[i];
		int nexty = y + fy[i];
		if (arr[nextx][nexty] == '#' && nextx >= 0 && nextx < NUMBER - 1 && nexty < NUMBER - 1 && nexty >= 0)
		{
			if (wherewego(nextx, nexty) == true)
			{
				arr[nextx][nexty] = '.';
				if (makemap(nextx, nexty) == true) return true;
			}
		}
	}

	return false;


}

//排除四个角
bool unerror(int x, int y) {

	if ((x == 0 && y == 0) || (x == 0 && y == 11) || (x == 11 && y == 0) || (x == 11 && y == 11)) {
	
		return false;
	}
	
	return true;

}


void showmap()
{
	for (int i = 0; i < NUMBER; i++)
	{
		for (int j = 0; j < NUMBER; j++)
		{
			printf("%2c", arr[i][j]);
		}
		printf("\n");
	}



}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的迷宫游戏 C++ 代码示例,实现了基本的移动和寻找出口功能: ``` #include <iostream> #include <vector> using namespace std; // 迷宫大小 const int N = 5; // 迷宫地图 vector<vector<int>> maze = { {0, 0, 1, 0, 0}, {0, 0, 1, 0, 0}, {0, 0, 0, 0, 1}, {0, 1, 1, 0, 0}, {0, 0, 0, 1, 0}, }; // 标记访问过的位置 vector<vector<bool>> visited(N, vector<bool>(N, false)); // 上下左右四个方向的移动 const int dx[] = {0, 0, -1, 1}; const int dy[] = {-1, 1, 0, 0}; // 搜索函数,返回是否找到出口 bool dfs(int x, int y) { if (x < 0 || x >= N || y < 0 || y >= N) return false; // 越界 if (maze[x][y] == 1 || visited[x][y]) return false; // 障碍或已访问过 if (x == N - 1 && y == N - 1) return true; // 找到出口 visited[x][y] = true; // 标记为已访问 for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; // 移动到相邻位置 if (dfs(nx, ny)) return true; // 搜索成功,直接返回 } return false; // 没有找到出口 } int main() { if (dfs(0, 0)) { cout << "找到了出口" << endl; } else { cout << "没有找到出口" << endl; } return 0; } ``` 代码的大致思路是使用深度优先搜索遍历迷宫,用二维数组 `maze` 表示迷宫地图,其中 0 表示空地,1 表示障碍物。用二维数组 `visited` 标记哪些位置已经访问过。 搜索函数 `dfs` 的参数是当前位置的坐标 `(x, y)`,每次搜索时,先检查是否越界、是否是障碍或已访问过的位置,然后标记为已访问,再尝试向上下左右四个方向移动,如果移动成功并找到出口,则直接返回 true,否则继续尝试其他方向。 在 `main` 函数中,调用 `dfs` 函数从起点开始搜索,如果搜索成功(即找到了出口),输出提示信息,否则输出另一种提示信息。 运行原理是通过深度优先搜索遍历迷宫,从起点开始,尝试向上下左右四个方向移动,如果移动成功并找到出口,则直接返回 true,否则继续尝试其他方向。在搜索过程中,使用 `visited` 数组标记哪些位置已经访问过,以避免重复访问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值