@ludw1gj/maze-generation
Version:
Maze generation algorithms.
49 lines • 1.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const cell_1 = require("./cell");
const direction_1 = require("./direction");
class Grid {
constructor(height, width) {
const cells = new Array(height);
for (let y = 0; y < cells.length; y++) {
cells[y] = new Array(width);
for (let x = 0; x < cells[y].length; x++) {
cells[y][x] = new cell_1.Cell({ x, y });
}
}
this.cells = cells;
this._height = height;
this._width = width;
}
get height() {
return this._height;
}
get width() {
return this._width;
}
getCell(pos) {
if (this.isPositionValid(pos)) {
return this.cells[pos.y][pos.x];
}
return undefined;
}
exportCells() {
return this.cells.map(row => row.map(cell => cell.createCopy()));
}
getUnvisitedNeighbours(cell) {
return direction_1.DIRECTIONS.map(dir => {
const neighbourPos = cell.getChangeInPosition(dir);
const neighbourCell = this.getCell(neighbourPos);
return { neighbourCell, dir };
}).filter(val => val.neighbourCell !== undefined && !val.neighbourCell.isVisited);
}
isPositionValid(pos) {
return pos.x >= 0 && pos.y >= 0 && pos.x < this._width && pos.y < this._height;
}
static getRandomElement(arr) {
const randIndex = Math.floor(Math.random() * arr.length);
return arr[randIndex];
}
}
exports.Grid = Grid;
//# sourceMappingURL=grid.js.map