title: Maze (recursive division) algorithm design
body: <p>I am currently developing a random maze generator that stores the maze in a 2-dimensional array called <code>grid</code>. This will then be used later on to generate a real 3D maze that the user can then walk through.</p>
  <p>After doing some research, I attempted to create this maze generator using the recursive division algorithm, however due to the nature of the format of the maze, this isn't really working for me.</p>
  <p>From what I understand, the recursive division method does not treat walls as <em>cells</em>.</p>
  <p>For instance, my grid would look like this:</p>
  <pre><code>  a b c d e f g h
  1 - - - - - - - -
  2 |   |   | |   |
  3 |       |     |
  4 | -   - |   - |
  5 |     |     | |
  6 |   - |   -   |
  7 x             |
  8 - - - - - - - -
  </code></pre>
  <p>The point that I'm trying to get across here is that the grid I am trying to create will be represented something like this:</p>
  <pre><code>w w w w w w w w
  w   w   w w   w
  w       w     w
  w w   w w   w w
  w     w     w w
  w   w w   w   w
  g             w
  w w w w w w w w
  </code></pre>
  <p>Where 'w' is a wall and 'g' is the entrance/exit. So walls are placed into the grid, e.g. <code>grid[1][2] == 'w'</code></p>
  <p>The problem with the recursive division algorithm is that walls are not treated as members of the cell. All of the 'cells' would essentially contain whitespace and the walls would be placed around them.</p>
  <p>So when I tried to implement this algorithm in my situation, I ended up with a result like this: (the black squares are walls, the white squares are empty, and the red square is the entrance) <img src="https://i.stack.imgur.com/LpPmW.png" alt="enter image description here" /></p>
  <h2><a href="http://jsfiddle.net/tPm3s/" rel="noreferrer">My JSFiddle is located here.</a></h2>
  <p>Essentially the user will start at the red square and have to go through the maze and find keys that will open the door (which is the red square) to escape, so all of the whitespace in the maze would have to be accessible.</p>
  <p>Does anyone have any ideas on how I can rewrite this algorithm to make sure that there is always a path from the red square to any other space in the maze? Ideally, the path would never be more than one square wide.</p>
  <h2>Code:</h2>
  <pre><code>var grid;
  
  function generate(dimensions, numDoors) {
      //numDoors is unused right now
  
      grid = new Array();
      for (var i = 0; i &lt; dimensions; i++) {
          grid[i] = new Array();
  
          for (var j = 0; j &lt; dimensions; j++) {
              grid[i][j] = &quot;&quot;;
          }
      }
  
      addOuterWalls();
      var ent = addEntrance();
      addInnerWalls(true, 1, grid.length - 2, 1, grid.length - 2, ent);
  }
  
  function addOuterWalls() {
      for (var i = 0; i &lt; grid.length; i++) {
          if (i == 0 || i == (grid.length - 1)) {
              for (var j = 0; j &lt; grid.length; j++) {
                  grid[i][j] = &quot;w&quot;;
              }
          } else {
              grid[i][0] = &quot;w&quot;;
              grid[i][grid.length - 1] = &quot;w&quot;;
          }
      }
  }
  
  function addEntrance() {
      var x = randomNumber(1, grid.length - 1);
      grid[grid.length - 1][x] = &quot;g&quot;;
      return x;
  }
  
  function addInnerWalls(h, minX, maxX, minY, maxY, gate) {
      if (h) {
  
          if (maxX - minX &lt; 2) {
              return;
          }
  
          var y = randomNumber(minY, maxY);
          addHWall(minX, maxX, y);
  
          addInnerWalls(!h, minX, maxX, minY, y-1, gate);
          addInnerWalls(!h, minX, maxX, y + 1, maxY, gate);
      } else {
          if (maxY - minY &lt; 2) {
              return;
          }
  
          var x = randomNumber(minX, maxX);
          addVWall(minY, maxY, x);
  
          addInnerWalls(!h, minX, x-1, minY, maxY, gate);
          addInnerWalls(!h, x + 1, maxX, minY, maxY, gate);
      }
  }
  
  function addHWall(minX, maxX, y) {
      var hole = randomNumber(minX, maxX);
  
      for (var i = minX; i &lt;= maxX; i++) {
          if (i == hole) grid[y][i] = &quot;&quot;;
          else grid[y][i] = &quot;w&quot;;
      }
  }
  
  function addVWall(minY, maxY, x) {
      var hole = randomNumber(minY, maxY);
  
      for (var i = minY; i &lt;= maxY; i++) {
          if (i == hole) grid[i][x] = &quot;&quot;;
          else grid[i][x] = &quot;w&quot;;
      }
  }
  
  function randomNumber(min, max) {
      return Math.floor(Math.random() * (max - min + 1) + min);
  }
  
  function display() {
      document.getElementById(&quot;cnt&quot;).innerHTML = &quot;&quot;;
  
      for (var i = 0; i &lt; grid.length; i++) {
          var output = &quot;&lt;div&gt;&quot;;
          for (var j = 0; j &lt; grid.length; j++) {
              output += &quot;&lt;b &quot; + grid[i][j] + &quot;&gt;&lt;/b&gt;&quot;;
          }
          output += &quot;&lt;/div&gt;&quot;;
          document.getElementById(&quot;cnt&quot;).innerHTML += output;
      }
  }
  generate(30, 1, 1);
  display();
  </code></pre>