Building a maze
- create a grid of cells
- pick a random starting cell
- for that cell, build a random-ordered list of neighbors
- if a neighbor has been visited before, remove it from the list
- for each remaining neighbor, move to it and remove the wall between those two cells
- repeat '2' for this new neighbor
const grid = Array(cells)
.fill(null)
.map(() => Array(cells).fill(false));
const verticals = Array(cells)
.fill(null)
.map(() => Array(cells - 1).fill(false));
const horizontals = Array(cells - 1)
.fill(null)
.map(() => Array(cells).fill(false));
const startRow = Math.floor(Math.random() * cells);
const startCol = Math.floor(Math.random() * cells);
console.log(startRow, startCol);
const stepThroughCell = (row, column) => {
// If i have visted the cell at [row, column], then return
if (grid[row][column]) {
return;
}
// Mark this cell as being visited
grid[row][column] = true;
// Assemble randomly-ordered list of neighbors
const neighbors = shuffle([
[row - 1, column, "up"],
[row, column + 1, "right"],
[row + 1, column, "down"],
[row, column - 1, "left"],
]);
// For each neighbor....
for (let neighbor of neighbors) {
const [nextRow, nextColumn, direction] = neighbor;
// See if that neighbor is out of bounds
if (
nextRow < 0 ||
nextRow >= cells ||
nextColumn < 0 ||
nextColumn >= cells
) {
continue;
}
// If we have visited that neighbor, continue to next neighbor
if (grid[nextRow][nextColumn]) {
continue;
}
// Remove a wall from either horizontals or verticals
if (direction === "left") {
verticals[row][column - 1] = true;
} else if (direction === "right") {
verticals[row][column] = true;
} else if (direction === "up") {
horizontals[row - 1][column] = true;
} else if (direction === "down") {
horizontals[row][column] = true;
}
stepThroughCell(nextRow, nextColumn);
}
};