Javascript中生成迷宫的递归除法
我想用javascript制作一个算法可视化工具,但我的算法有缺陷,我就是找不到缺陷。让我发疯 可视化工具是2D棋盘(类似于国际象棋棋盘),基本算法如下: 我必须修改上面的解决方案,因为我的基于网格的图。我想返回一个数组,它看起来像[[行坐标/列坐标]] 图片: 上面的图片展示了我目前的迷宫 我觉得我接近解决办法了。我认为错误是递归区域,但我就是找不到它Javascript中生成迷宫的递归除法,javascript,algorithm,maze,Javascript,Algorithm,Maze,我想用javascript制作一个算法可视化工具,但我的算法有缺陷,我就是找不到缺陷。让我发疯 可视化工具是2D棋盘(类似于国际象棋棋盘),基本算法如下: 我必须修改上面的解决方案,因为我的基于网格的图。我想返回一个数组,它看起来像[[行坐标/列坐标]] 图片: 上面的图片展示了我目前的迷宫 我觉得我接近解决办法了。我认为错误是递归区域,但我就是找不到它 class RecursiveDivision { constructor(graph, startNode, endNode) {
class RecursiveDivision {
constructor(graph, startNode, endNode) {
this.graph = graph;
this.startNode = startNode;
this.endNode = endNode;
}
runMaze() {
const wallList = [];
const start = [this.startNode.row, this.startNode.column];
const end = [this.endNode.row, this.endNode.column];
const minWidth = 0;
const minHeight = 0;
const width = this.graph[0].length; //21
const height = this.graph.length; // 35
// if start or end nodes are not in corner we set up border walls
//this.setUpBorder(wallList, start, end, width, height);
const count = 0;
this.mazeDivision(
wallList,
minHeight,
minWidth,
width,
height,
this.chooseOrientation(width, height)
);
return wallList;
}
mazeDivision(wallList, minHeight, minWidth, width, height, orientation) {
// condition to stop recursion. 2 because it prevents doubling walls
if (width <= 2 || height <= 2) return;
// location of the wall in the width height graph depending on orientation
// column
let wallX =
minWidth + (orientation === "Horizontal" ? 0 : this.randomNum(width - 2));
// row
let wallY =
minHeight +
(orientation === "Horizontal" ? this.randomNum(height - 2) : 0);
// location of the gap of the wall
const gapX =
wallX + (orientation === "Horizontal" ? this.randomNum(width) : 0);
const gapY =
wallY + (orientation === "Horizontal" ? 0 : this.randomNum(height));
// Wall units must be even and gaps uneven to prevent a wall in a gap
if (this.EvenOrOdd(wallX, wallY, gapX, gapY, orientation))
return this.mazeDivision(
wallList,
minHeight,
minWidth,
width,
height,
orientation
);
// get direction of the wall
const directionX = orientation === "Horizontal" ? 1 : 0;
const directionY = orientation === "Horizontal" ? 0 : 1;
const wallLength = orientation === "Horizontal" ? width : height;
// build the wall !
for (let i = 0; i < wallLength; i++) {
if (!(wallX === gapX && wallY === gapY)) wallList.push([wallY, wallX]);
wallX += directionX;
wallY += directionY;
}
//setup for recursion. Left chamber
let nextX = minWidth;
let nextY = minHeight;
let nextWidth = orientation === "Horizontal" ? width : wallX - minWidth + 1;
let nextHeight =
orientation === "Horizontal" ? wallY - minHeight + 1 : height;
this.mazeDivision(
wallList,
nextY,
nextX,
nextWidth,
nextHeight,
this.chooseOrientation(nextWidth, nextHeight)
);
// right chamber
nextX = orientation === "Horizontal" ? minWidth : wallX + 1;
nextY = orientation === "Horizontal" ? wallY + 1 : minHeight;
nextWidth =
orientation === "Horizontal" ? width : minWidth + width - wallX - 1;
nextHeight =
orientation === "Horizontal" ? minHeight + height - wallY - 1 : height;
this.mazeDivision(
wallList,
nextY,
nextX,
nextWidth,
nextHeight,
this.chooseOrientation(nextWidth, nextHeight)
);
}
EvenOrOdd(wallX, wallY, gapX, gapY, orientation) {
if (orientation === "Horizontal") return wallY % 2 === 0 && gapX % 2 !== 0;
return wallX % 2 === 0 && gapY % 2 !== 0;
}
randomNum(max) {
return Math.floor(max * Math.random());
}
chooseOrientation(width, height) {
// if the height of the grid is bigger than width -> horizontal
if (width < height) return "Horizontal";
//else ->vertikal
return "Vertikal";
}
}
export default RecursiveDivision;
};
类递归除法{
构造函数(图、开始节点、结束节点){
this.graph=图形;
this.startNode=startNode;
this.endNode=endNode;
}
runMaze(){
常量墙列表=[];
const start=[this.startNode.row,this.startNode.column];
const end=[this.endNode.row,this.endNode.column];
常数minWidth=0;
常数最小高度=0;
const width=this.graph[0].length;//21
const height=this.graph.length;//35
//若起点或终点节点不在拐角处,则设置边界墙
//此.setUpBorder(墙列表、起点、终点、宽度、高度);
常数计数=0;
这是我的梦想(
墙名单,
minHeight,
minWidth,
宽度,
高度,
此选项。选择方向(宽度、高度)
);
返回墙列表;
}
mazeDivision(墙列表、最小高度、最小宽度、宽度、高度、方向){
//条件停止递归。2,因为它可以防止双重墙
如果(垂直宽度)
返回“Vertikal”;
}
}
导出默认递归划分;
};
您能否为我们提供一个最简单的工作示例(例如,在codepen或JSFIDLE中),以便我们可以使用?