问题描述
我试图在我的寻路可视化解决方案中包括一个迷宫生成器,以生成关于当前应用程序可以工作的迷宫。
我找到了一个结构良好的网站,有许多迷宫生成算法,但我主要关注的是:http://weblog.jamisbuck.org/2011/1/12/maze-generation-recursive-division-algorithm
我的编程语言知识主要是C ++,而我拥有的应用程序就是使用该+ SDL2.0构建的。
我的“网格”(2D矩阵)是由“单元/节点”组成的,这些“单元/节点”由呈现在窗口表面上的盒子纹理表示。每个“细胞”可以处于不同的状态-障碍==它是一堵墙-白色状态==它在通过。
我面临的一个问题是,我的“通道”有时会被随机生成的下一道墙所阻挡-导致无法解决的迷宫。
问题是如何避免生成的墙不阻塞先前打开的通道?
代码:
void RecursiveDivision::divide(NodesMaP* map,int x,int y,int width,int
height,Orientation orientation,SDL_Renderer* renderer)
{
if (width < 2|| height < 2)
{
return;
}
bool wallIsHorizontal = orientation == Orientation::Horizontal ? true : false;
//Where the wall is
int wX = x + (wallIsHorizontal ? 0 : rand() % (width-1));
int wY = y + (wallIsHorizontal ? rand() % (height-1): 0);
//Where the passage is
int pX = wX + (wallIsHorizontal ? (rand() % width) : 0);
int pY = wY + (wallIsHorizontal ? 0 : (rand() % height));
//How long is the wall
int wallLenght = (wallIsHorizontal ? width : height);
//On whitch axis will the wall be drawn
int dX = wallIsHorizontal ? 1 : 0;
int dY = wallIsHorizontal ? 0 : 1;
//Draw the wall
for (int i = 0; i < wallLenght; ++i)
{
if (wX != pX || wY != pY)
{
map->getNode(wX,wY)->hardChangeState(NodeState::OBSTACLE);
}
wX += dX;
wY += dY;
}
//Render the current state
map->render(renderer,nullptr);
SDL_RenderPresent(renderer);
int nX = x; int nY = y;
int w = wallIsHorizontal ? width : (wX - x);
int h = wallIsHorizontal ? (wY - y ) : height;
divide(map,nX,nY,w,h,chooSEOrientation(w,h),renderer);
nX = wallIsHorizontal ? x : (wX + 1);
nY = wallIsHorizontal ? (wY + 1) : y;
w = wallIsHorizontal ? width : (x + width - wX -1);
h = wallIsHorizontal ? (y + height - wY-1 ) : height;
divide(map,renderer);
}
示例:
2 step from the start of algorithm
示例-20x20瓷砖地图上的“成品迷宫”:
注意
您看到的屏幕截图来自程序的单独运行,因此有所不同。希望您能明白这一点。
解决方法
与您受到启发的链接相反,您的墙壁占据了一个单元格的空间。避免出现问题的最简单方法是,您的墙壁只能放在一列/两列中。
这就是迷宫般薄壁的东西
这就是厚壁的墙(您正在使用的墙)
如您所见,它们的网格大小不一样,第一个是3x3,最后一个是5x5,没有边框(已编辑,因为您的边框没有边框)。
- 您需要一个带有奇数边的网格。如果它是基于薄迷宫,则使侧面2 * n-1变大,其长度为薄迷宫的侧面的长度*
- 仅将墙壁放在奇数行和列(从索引0开始)
- 在墙壁上的偶数行和列上放置开口
To resume,you can place walls
o o o o o
o o o o o <- here
o o o o o
o o o o o <- here
o o o o o
^ ^
here here
(*)2n-1没有边界,否则请使用2n +1
如何在您的代码中实现这一点:
const int truewidth = (width-1)/2;
const int trueheight = (height-1)/2;
//Where the wall is
int wX = x + (wallIsHorizontal ? 0 : 2 * (rand() % (truewidth)) + 1);
int wY = y + (wallIsHorizontal ? 2 * (rand() % (trueheight)) + 1: 0);
//Where the passage is
int pX = wX + (wallIsHorizontal ? 2 * (rand() % (truewidth)) : 0);
int pY = wY + (wallIsHorizontal ? 0 : 2 * (rand() % (trueheight)));
/!\尚未测试