UNPKG

@miketmoore/maze-generator

Version:

This is a javascript library, written in TypeScript that generates a maze data structure.

42 lines (41 loc) 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.carveIterative = void 0; const rand_1 = require("./rand"); function carveIterative(grid) { const coord = grid.getRandCoord(); const history = [coord]; let running = true; while (running) { const coord = history[history.length - 1]; const cell = grid.getCell(coord); if (!cell) { throw new Error('cell not found'); } // get list of walls not carved yet, that point to adjacent cells that have not been visited yet const availableWalls = grid.getAvailableCellWalls(cell, coord); if (availableWalls.length === 0) { if (history.length >= 2) { history.pop(); } else { running = false; } } else { const wallIndex = rand_1.randInRange(0, availableWalls.length); const availableWall = availableWalls[wallIndex]; grid.carveCellWall(coord, availableWall); const adjacentCoord = grid.getAdjacentCoord(availableWall, coord); if (adjacentCoord) { const adjacentCell = grid.getCell(adjacentCoord); if (adjacentCell && !adjacentCell.isVisited()) { const oppDir = adjacentCell.getOppositeDirection(availableWall); grid.carveCellWall(adjacentCoord, oppDir); history.push(adjacentCoord); } } } } } exports.carveIterative = carveIterative;