递归除法,用于在Javascript中生成迷宫

问题描述

我想用javascript制作一个算法可视化器,但是我的算法有错误,我只是找不到错误。使我发疯。

可视化器是2D面板(如国际象棋棋盘),此处的基本算法是:http://weblog.jamisbuck.org/2011/1/12/maze-generation-recursive-division-algorithm

由于基于网格的图形,我必须修改上述解决方案。我想返回一个看起来像[[行坐标/列坐标]]的数组。

图片https://i.stack.imgur.com/0GGgp.png

上面的图片展示了我目前的迷宫。

我觉得我接近解决方案。我认为该错误是递归区域,但我找不到它。

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,height,this.chooSEOrientation(width,height)
    );
    return wallList;
  }

  mazeDivision(wallList,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,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,nextHeight)
    );
  }

  EvenorOdd(wallX,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;

};

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)