function initMazeStep() {
  if (!initMaze.horizon.length) {
    // Done.
    startGame();
    return;
  }
  var randomIndex = Math.floor(Math.random() * initMaze.horizon.length);
  var cell = initMaze.horizon.splice(randomIndex, 1)[0];

  var wallCount =
    isWall(cell.x + 1, cell.y, true) + isWall(cell.x - 1, cell.y, true) +
    isWall(cell.x, cell.y + 1, true) + isWall(cell.x, cell.y - 1, true);
  if (wallCount >= 3) {
    if (isWall(cell.x + 1, cell.y, false)) {
      initMaze.horizon.push({x: cell.x + 1, y: cell.y});
    }
    if (isWall(cell.x - 1, cell.y, false)) {
      initMaze.horizon.push({x: cell.x - 1, y: cell.y});
    }
    if (isWall(cell.x, cell.y + 1, false)) {
      initMaze.horizon.push({x: cell.x, y: cell.y + 1});
    }
    if (isWall(cell.x, cell.y - 1, false)) {
      initMaze.horizon.push({x: cell.x, y: cell.y - 1});
    }
    walls[cell.x][cell.y] = false;
    draw(cell.x, cell.y);
    // Also redraw the four neighbouring cells since they may have been changed.
    draw(cell.x + 1, cell.y);
    draw(cell.x - 1, cell.y);
    draw(cell.x, cell.y + 1);
    draw(cell.x, cell.y - 1);
    setTimeout(initMazeStep, 0);
  } else {
    initMazeStep();
  }
}
// Grow the maze by one step.
Grow the maze by one step.
Grow the maze by one step .
function initMazeStep() {
  if (!initMaze.horizon.length) {
    startGame();
    return;
  }
  var randomIndex = Math.floor(Math.random() * initMaze.horizon.length);
  var cell = initMaze.horizon.splice(randomIndex, 1)[0];
  var wallCount =
    isWall(cell.x + 1, cell.y, true) + isWall(cell.x - 1, cell.y, true) +
    isWall(cell.x, cell.y + 1, true) + isWall(cell.x, cell.y - 1, true);
  if (wallCount >= 3) {
    if (isWall(cell.x + 1, cell.y, false)) {
      initMaze.horizon.push({x: cell.x + 1, y: cell.y});
    }
    if (isWall(cell.x - 1, cell.y, false)) {
      initMaze.horizon.push({x: cell.x - 1, y: cell.y});
    }
    if (isWall(cell.x, cell.y + 1, false)) {
      initMaze.horizon.push({x: cell.x, y: cell.y + 1});
    }
    if (isWall(cell.x, cell.y - 1, false)) {
      initMaze.horizon.push({x: cell.x, y: cell.y - 1});
    }
    walls[cell.x][cell.y] = false;
    draw(cell.x, cell.y);
    draw(cell.x + 1, cell.y);
    draw(cell.x - 1, cell.y);
    draw(cell.x, cell.y + 1);
    draw(cell.x, cell.y - 1);
    setTimeout(initMazeStep, 0);
  } else {
    initMazeStep();
  }
}
function initMazeStep ( ) { if ( ! initMaze . horizon . length ) { startGame ( ) ; return ; } var randomIndex = Math . floor ( Math . random ( ) * initMaze . horizon . length ) ; var cell = initMaze . horizon . splice ( randomIndex , 1 ) [ 0 ] ; var wallCount = isWall ( cell . x + 1 , cell . y , true ) + isWall ( cell . x - 1 , cell . y , true ) + isWall ( cell . x , cell . y + 1 , true ) + isWall ( cell . x , cell . y - 1 , true ) ; if ( wallCount >= 3 ) { if ( isWall ( cell . x + 1 , cell . y , false ) ) { initMaze . horizon . push ( { x : cell . x + 1 , y : cell . y } ) ; } if ( isWall ( cell . x - 1 , cell . y , false ) ) { initMaze . horizon . push ( { x : cell . x - 1 , y : cell . y } ) ; } if ( isWall ( cell . x , cell . y + 1 , false ) ) { initMaze . horizon . push ( { x : cell . x , y : cell . y + 1 } ) ; } if ( isWall ( cell . x , cell . y - 1 , false ) ) { initMaze . horizon . push ( { x : cell . x , y : cell . y - 1 } ) ; } walls [ cell . x ] [ cell . y ] = false ; draw ( cell . x , cell . y ) ; draw ( cell . x + 1 , cell . y ) ; draw ( cell . x - 1 , cell . y ) ; draw ( cell . x , cell . y + 1 ) ; draw ( cell . x , cell . y - 1 ) ; setTimeout ( initMazeStep , 0 ) ; } else { initMazeStep ( ) ; } }
Grow the maze by one step.
Grow the maze by one step .
// Done. // Also redraw the four neighbouring cells since they may have been changed.