class Maze{ constructor(mazeWidth, mazeHeight, cellWidth){ this.columns = floor(mazeWidth / cellWidth); this.rows = floor(mazeHeight / cellWidth); this.width = mazeWidth; this.height = mazeHeight; this.cellWidth = cellWidth; this.workingStack = []; this.grid = []; for (let y = 0; y < this.rows; y++) { for (var x = 0; x < this.columns; x++) { this.grid.push(new Cell(x, y, this.cellWidth)); } } this.grid[this.index(0,0)].visited = true; this.grid[this.index(0,0)].selected = true; this.workingStack.push(this.grid[this.index(0,0)]); } getStartingCell(){ return this.grid[0]; } getEndingCell(){ return this.grid[this.index(9, 9)]; } updateMazeDisplay(){ for (let i = 0; i < this.grid.length; i++) { this.grid[i].show(); } } resetMazeState(){ for (let i = 0; i < this.grid.length; i++){ this.grid[i].visited = false; this.grid[i].color = this.grid[i].defaultColor; this.grid[i].parent = undefined; } } buildNextCell(selectedColor = [50, 175, 50]){ //Build the maze let currentCell = this.workingStack.pop(); let nextCell = this.getNeighbors(currentCell); if(nextCell){ this.workingStack.push(currentCell); currentCell.color = currentCell.defaultColor; currentCell.visited = true; nextCell.visited = true; nextCell.color = selectedColor; this.removeWalls(currentCell, nextCell); this.workingStack.push(nextCell); } else this.workingStack.pop(); currentCell.color = currentCell.defaultColor; } get mazeGenDone() { return this.workingStack.length == 0; } getNeighbors(cell){ let visited = []; let top = this.grid[this.index(cell.x, cell.y - 1)]; let left = this.grid[this.index(cell.x - 1, cell.y)]; let bottom = this.grid[this.index(cell.x, cell.y + 1)]; let right = this.grid[this.index(cell.x + 1, cell.y)]; if (top && !top.visited) { visited.push(top); } if (left && !left.visited) { visited.push(left); } if (right && !right.visited) { visited.push(right); } if (bottom && !bottom.visited) { visited.push(bottom); } if(visited.length == 1) return visited[0]; else if (visited.length > 1) return visited[floor(random(0, visited.length))]; else return undefined; } getLegalMoves(cell){ let neighbors = []; let top = this.grid[this.index(cell.x, cell.y - 1)]; let left = this.grid[this.index(cell.x - 1, cell.y)]; let bottom = this.grid[this.index(cell.x, cell.y + 1)]; let right = this.grid[this.index(cell.x + 1, cell.y)]; if (top && !top.visited && !cell.top) { neighbors.push(top); } if (left && !left.visited && !cell.left) { neighbors.push(left); } if (right && !right.visited && !cell.right) { neighbors.push(right); } if (bottom && !bottom.visited && !cell.bottom) { neighbors.push(bottom); } return shuffle(neighbors); } removeWalls(a, b) { let x = a.x - b.x; let y = a.y - b.y; if (x === -1) { a.setRight = false; b.setLeft = false; } else if (x === 1) { a.setLeft = false; b.setRight = false; } if (y === -1) { a.setBottom = false; b.setTop = false; } else if (y === 1) { a.setTop = false; b.setBottom = false; } } index(x, y) { if (x < 0 || y < 0 || x > this.columns - 1 || y > this.rows - 1) return -1; return x + y * this.columns; } }