Javascript 递归算法(迷宫)是阻塞门

Javascript 递归算法(迷宫)是阻塞门,javascript,reactjs,algorithm,Javascript,Reactjs,Algorithm,我正在使用JavaScript创建一个递归算法来创建迷宫。问题是算法偶尔会阻塞一些门,导致一些迷宫无法解决。我的网格是一个二维数组,31*31个单元格。我包括了一个无法解决的迷宫的图像,我不知道什么是错误的,我认为我的随机数是错误的,但我不认为它们是错误的 // Recursive Division Algorithm // walls to store the maze let mazeWalls = []; // store all doors, to prevent blocking th

我正在使用JavaScript创建一个递归算法来创建迷宫。问题是算法偶尔会阻塞一些门,导致一些迷宫无法解决。我的网格是一个二维数组,31*31个单元格。我包括了一个无法解决的迷宫的图像,我不知道什么是错误的,我认为我的随机数是错误的,但我不认为它们是错误的

// Recursive Division Algorithm
// walls to store the maze
let mazeWalls = [];
// store all doors, to prevent blocking them
let allDoors = [];

const randomNumber = (min, max) => {
  return Math.floor(Math.random() * (max - min + 1) + min);
};

const addBorderWalls = (grid, cols, rows) => {
  for (let i = 0; i < cols; i++) {
    if (i === 0 || i === cols - 1) {
      for (let j = 0; j < rows; j++) {
        mazeWalls.push(grid[i][j]);
      }
    } else {
      mazeWalls.push(grid[i][0]);
      mazeWalls.push(grid[i][rows - 1]);
    }
  }
  return grid;
};

export const recursiveDivision = (graph, cols, rows) => {
  allDoors = [];
  mazeWalls = [];
  // generating the border walls of the grid
  // this returns the grid with border walls
  let grid = addBorderWalls(graph, cols, rows);

  const addHorizontalWall = (minX, maxX, y) => {
    // randomNumber this way creates only odd numbers
    const door = Math.floor(randomNumber(minX, maxX) / 2) * 2 + 1;

    for (let i = minX; i <= maxX; i++) {
      if (i !== door && !allDoors.includes(grid[i][y]))
        mazeWalls.push(grid[i][y]);
    }
    allDoors.push(door);
  };

  const addVerticalWall = (minY, maxY, x) => {
    // randomNumber this way creates only odd numbers
    const door = Math.floor(randomNumber(minY, maxY) / 2) * 2 + 1;

    for (let i = minY; i <= maxY; i++) {
      if (i !== door && !allDoors.includes(grid[x][i]))
        mazeWalls.push(grid[x][i]);
    }
    allDoors.push(door);
  };

  const addInnerWalls = (orientation, minX, maxX, minY, maxY) => {
    if (orientation) {
      // 27 < 2
      if (maxX - minX < 2) {
        // break recursive function
        return;
      }
      // randomNumber this way, creates an even number
      const y = Math.floor(randomNumber(minY, maxY) / 2) * 2;
      // creates only horizontal walls in the x-axis
      addHorizontalWall(minX, maxX, y);

      addInnerWalls(!orientation, minX, maxX, minY, y - 1);
      addInnerWalls(!orientation, minX, maxX, y + 1, maxY);
    } else {
      if (maxY - minY < 2) {
        // break recursive function
        return;
      }
      // randomNumber this way, creates an even number
      const x = Math.floor(randomNumber(minX, maxX) / 2) * 2;
      // creates only vertical walls in the y-axis
      addVerticalWall(minY, maxY, x);

      addInnerWalls(!orientation, minX, x - 1, minY, maxY);
      addInnerWalls(!orientation, x + 1, maxX, minY, maxY);
    }
  };

  // the walls that actually form the maze (inside)
  addInnerWalls(true, 1, cols - 2, 1, rows - 2);
  return mazeWalls;
};

//递归除法算法
//存放迷宫的墙壁
让mazeWalls=[];
//存放所有门,以防堵塞
让所有门=[];
常量随机数=(最小值,最大值)=>{
返回Math.floor(Math.random()*(max-min+1)+min);
};
const addBorderWalls=(网格、列、行)=>{
for(设i=0;i{
所有门=[];
mazeWalls=[];
//生成网格的边界墙
//这将返回带有边框墙的栅格
让网格=添加边界墙(图形、列、行);
常量addHorizontalWall=(最小、最大、y)=>{
//随机数这种方式只创建奇数
常数门=数学楼层(随机数(最小、最大)/2)*2+1;
for(让i=minX;i{
//随机数这种方式只创建奇数
常数门=数学楼层(随机数(最小、最大)/2)*2+1;
因为(让我=米尼;我{
如果(方向){
// 27 < 2
如果(最大值-最小值<2){
//中断递归函数
返回;
}
//随机数通过这种方式创建偶数
常数y=数学楼层(随机数(最小、最大)/2)*2;
//仅在x轴上创建水平墙
添加水平墙(最小、最大、y);
附加壁(!方向,最小值,最大值,最小值,y-1);
附加壁(!方向,最小值,最大值,y+1,最大值);
}否则{
如果(最大值-最小值<2){
//中断递归函数
返回;
}
//随机数通过这种方式创建偶数
常数x=数学层(随机数(minX,maxX)/2)*2;
//仅在y轴上创建垂直墙
addVerticalWall(minY、maxY、x);
addInnerWalls(!方向,minX,x-1,minY,maxY);
附加壁(!方向,x+1,最大,最小,最大);
}
};
//实际上形成迷宫的墙(内部)
addInnerWalls(正确,1,列-2,1,行-2);
返回迷宫;
};