diff --git a/Maze.js b/Maze.js index 922d15d..67e2f0a 100644 --- a/Maze.js +++ b/Maze.js @@ -14,20 +14,39 @@ class Maze { constructor(mazeSettings) { this.prng = mazeSettings.prng ?? Math; this.width = mazeSettings.width || - mazeSettings.xSize || mazeSettings.size || mazeSettings.height || mazeSettings.ySize || 30; + mazeSettings.xSize || mazeSettings.size || mazeSettings.height || + mazeSettings.ySize || 30; this.height = mazeSettings.height || mazeSettings.ySize || mazeSettings.size || this.width; this.finishedGenerating = false; this.seed = mazeSettings.seed ?? Math.floor(Math.random() * 10e8); this.algorithm = mazeSettings.algorithm; this.algorithmId = mazeSettings.algorithmId; - + this.entrance = this.getXYPosition(mazeSettings.entrance ?? "top left"); this.exit = this.getXYPosition(mazeSettings.exit ?? "bottom right"); - this.entrance.direction = this.entrance.direction ?? (this.entrance.x <= 0 ? "W" : this.entrance.x >= this.width-1 ? "E" : this.entrance.y <= 0 ? "N" : this.entrance.y >= this.width-1 ? "S" : " "); - this.exit.direction = this.exit.direction ?? (this.exit.x <= 0 ? "W" : this.exit.x >= this.width-1 ? "E" : this.exit.y <= 0 ? "N" : this.exit.y >= this.width-1 ? "S" : " "); + this.entrance.direction = this.entrance.direction ?? + (this.entrance.x <= 0 + ? "W" + : this.entrance.x >= this.width - 1 + ? "E" + : this.entrance.y <= 0 + ? "N" + : this.entrance.y >= this.width - 1 + ? "S" + : " "); + this.exit.direction = this.exit.direction ?? + (this.exit.x <= 0 + ? "W" + : this.exit.x >= this.width - 1 + ? "E" + : this.exit.y <= 0 + ? "N" + : this.exit.y >= this.width - 1 + ? "S" + : " "); if ( this.algorithmId === "sidewinder" || @@ -106,7 +125,7 @@ class Maze { } step() { - if(this.finishedGenerating) return false; + if (this.finishedGenerating) return false; this.takeStep(); return !this.finishedGenerating; } diff --git a/algorithms/10Print.js b/algorithms/10Print.js index ccad2ff..7db339e 100644 --- a/algorithms/10Print.js +++ b/algorithms/10Print.js @@ -2,15 +2,15 @@ import Maze from "../Maze.js"; import { opposite, dx, - dy + dy, } from "../directions.js"; class TenPrint extends Maze { resetVariables() { this.currentCell = { x: 0, - y: 0 - } + y: 0, + }; this.allowOutsideConnections = false; } @@ -18,23 +18,23 @@ class TenPrint extends Maze { let passageDirection = this.prng.random() < 0.5 ? "S" : "E"; let neighbor1 = { x: this.currentCell.x + dx[passageDirection], - y: this.currentCell.y + dy[passageDirection] + y: this.currentCell.y + dy[passageDirection], }; let neighbor2 = { x: this.currentCell.x + dx[opposite[passageDirection]], - y: this.currentCell.y + dy[opposite[passageDirection]] + y: this.currentCell.y + dy[opposite[passageDirection]], }; - + if (this.cellIsInMaze(neighbor1) || this.allowOutsideConnections) { this.removeWall({ x: this.currentCell.x, - y: this.currentCell.y + y: this.currentCell.y, }, passageDirection); } if (this.cellIsInMaze(neighbor2) || this.allowOutsideConnections) { this.removeWall({ x: this.currentCell.x, - y: this.currentCell.y + y: this.currentCell.y, }, opposite[passageDirection]); } @@ -44,10 +44,7 @@ class TenPrint extends Maze { this.currentCell.x = this.currentCell.y % 2; if (this.currentCell.y >= this.height) this.finishedGenerating = true; } - } - } - -export default TenPrint; \ No newline at end of file +export default TenPrint; diff --git a/algorithms/AldousBroder.js b/algorithms/AldousBroder.js index d8576f7..15038ce 100644 --- a/algorithms/AldousBroder.js +++ b/algorithms/AldousBroder.js @@ -25,7 +25,6 @@ class AldousBroder extends Maze { //called every time the maze needs to be updated takeStep() { - let possibleDirections = []; if (this.currentCell.y !== 0) possibleDirections.push("N"); if (this.currentCell.y !== this.height - 1) possibleDirections.push("S"); @@ -54,7 +53,6 @@ class AldousBroder extends Maze { if (this.totalVisted >= this.width * this.height) { this.finishedGenerating = true; } - } } diff --git a/algorithms/BinaryTree.js b/algorithms/BinaryTree.js index 5ef439d..56ef5fd 100644 --- a/algorithms/BinaryTree.js +++ b/algorithms/BinaryTree.js @@ -22,7 +22,6 @@ class BinaryTree extends Maze { ) { this.finishedGenerating = true; } - } } diff --git a/algorithms/Ellers.js b/algorithms/Ellers.js index dab68c3..9abc98b 100644 --- a/algorithms/Ellers.js +++ b/algorithms/Ellers.js @@ -75,13 +75,11 @@ class Eller extends Maze { } takeStep() { - if (this.mode === this.HORIZONTAL) { this.horizontalStep(); } else { this.verticalStep(); } - } } diff --git a/algorithms/HuntAndKill.js b/algorithms/HuntAndKill.js index 48f45fc..89826d8 100644 --- a/algorithms/HuntAndKill.js +++ b/algorithms/HuntAndKill.js @@ -23,7 +23,6 @@ class HuntAndKill extends Maze { } takeStep() { - //random walk if (!this.hunting) { this.visited[this.currentCell.y][this.currentCell.x] = true; @@ -149,7 +148,6 @@ class HuntAndKill extends Maze { } } } - } } diff --git a/algorithms/Kruskals.js b/algorithms/Kruskals.js index ef518ac..b642607 100644 --- a/algorithms/Kruskals.js +++ b/algorithms/Kruskals.js @@ -32,7 +32,6 @@ class Kruskals extends Maze { } takeStep() { - let edge = this.edges.pop(); let cell1 = { x: edge.x, @@ -57,7 +56,6 @@ class Kruskals extends Maze { } if (this.edges.length === 0) this.finishedGenerating = true; - } getCellIndex(cell) { diff --git a/algorithms/ModifiedPrims.js b/algorithms/ModifiedPrims.js index eced9e3..3344a36 100644 --- a/algorithms/ModifiedPrims.js +++ b/algorithms/ModifiedPrims.js @@ -34,7 +34,6 @@ class ModifiedPrims extends Maze { } takeStep() { - //find index of cell with minimum cost let minCost = Infinity; let cellIndex = 0; @@ -80,7 +79,6 @@ class ModifiedPrims extends Maze { } if (this.activeCells.length === 0) this.finishedGenerating = true; - } } diff --git a/algorithms/RecursiveDivision.js b/algorithms/RecursiveDivision.js index d11182d..814950e 100644 --- a/algorithms/RecursiveDivision.js +++ b/algorithms/RecursiveDivision.js @@ -38,7 +38,6 @@ class RecursiveDivision extends Maze { } takeStep() { - switch (this.state) { case this.CHOOSE_REGION: this.chooseRegion(); @@ -55,7 +54,6 @@ class RecursiveDivision extends Maze { this.state, ); } - } chooseRegion() { diff --git a/algorithms/Sidewinder.js b/algorithms/Sidewinder.js index 0c42722..755ff67 100644 --- a/algorithms/Sidewinder.js +++ b/algorithms/Sidewinder.js @@ -46,7 +46,6 @@ class Sidewinder extends Maze { if (this.currentCell.y >= this.height) { this.finishedGenerating = true; } - } } diff --git a/algorithms/SimplifiedPrims.js b/algorithms/SimplifiedPrims.js index 0b8a49c..847f36e 100644 --- a/algorithms/SimplifiedPrims.js +++ b/algorithms/SimplifiedPrims.js @@ -28,7 +28,6 @@ class SimplifiedPrims extends Maze { } takeStep() { - let cellIndex = Math.floor(this.prng.random() * this.activeCells.length); let cell = this.activeCells[cellIndex]; diff --git a/algorithms/TruePrims.js b/algorithms/TruePrims.js index 327683d..87bc547 100644 --- a/algorithms/TruePrims.js +++ b/algorithms/TruePrims.js @@ -3,7 +3,7 @@ import { dx, dy, directions, - opposite + opposite, } from "../directions.js"; class TruePrims extends Maze { @@ -21,13 +21,17 @@ class TruePrims extends Maze { for (let direction of directions) { let isBorderWall = !this.cellIsInMaze({ x: x + dx[direction], - y: y + dy[direction] + y: y + dy[direction], }); let passageCost = isBorderWall ? Infinity : this.prng.random(); - if((direction === "N" || direction === "W") && !isBorderWall){ - passageCost == this.costs[y+dy[direction]][x+dx[direction]][opposite[direction]]; + if ((direction === "N" || direction === "W") && !isBorderWall) { + passageCost == + this + .costs[y + dy[direction]][x + dx[direction]][ + opposite[direction] + ]; } this.costs[y][x][direction] = passageCost; @@ -42,16 +46,15 @@ class TruePrims extends Maze { x: this.start.x, y: this.start.y, }; - - for(let direction of directions){ - this.activePassages.push({x: startCell.x, y: startCell.y, direction}); + + for (let direction of directions) { + this.activePassages.push({ x: startCell.x, y: startCell.y, direction }); } this.visited[startCell.y][startCell.x] = true; } takeStep() { - //find index of cell with minimum cost let minCost = Infinity; let passageIndex = 0; @@ -68,24 +71,24 @@ class TruePrims extends Maze { x: passage.x + dx[passage.direction], y: passage.y + dy[passage.direction], }; - if (this.cellIsInMaze(newCell) && !this.visited[newCell.y][newCell.x] > 0) { - - this.removeWall({x: passage.x, y: passage.y}, passage.direction); + this.removeWall({ x: passage.x, y: passage.y }, passage.direction); this.visited[newCell.y][newCell.x] = true; this.totalVisted++; - for(let direction of directions){ - if(direction !== opposite[passage.direction]) this.activePassages.push({x: newCell.x, y: newCell.y, direction}); + for (let direction of directions) { + if (direction !== opposite[passage.direction]) { + this.activePassages.push({ x: newCell.x, y: newCell.y, direction }); + } } - } else { this.activePassages.splice(passageIndex, 1); } - if (this.activePassages.length === 0 || minCost === Infinity) this.finishedGenerating = true; - + if (this.activePassages.length === 0 || minCost === Infinity) { + this.finishedGenerating = true; + } } } diff --git a/algorithms/Wilsons.js b/algorithms/Wilsons.js index b515f99..72234c3 100644 --- a/algorithms/Wilsons.js +++ b/algorithms/Wilsons.js @@ -27,7 +27,7 @@ class Wilsons extends Maze { } addVisit(x, y, dir = "S") { - console.log("🎻", x, y, dir) + console.log("🎻", x, y, dir); this.visits[`${x}:${y}`] = dir; this.visited[y][x]++; } @@ -59,7 +59,7 @@ class Wilsons extends Maze { } } - console.log("i", i, this) + console.log("i", i, this); } walkStep() { @@ -68,7 +68,7 @@ class Wilsons extends Maze { let ny = this.y + dy[direction]; if (this.cellIsInMaze({ x: nx, y: ny })) { - console.log(67, nx, ny, JSON.stringify(this)) + console.log(67, nx, ny, JSON.stringify(this)); let x = this.x; let y = this.y; @@ -106,7 +106,7 @@ class Wilsons extends Maze { console.log("🏃‍♀️", nx, ny, dir, JSON.stringify(this)); - if (this.cellIsInMaze({x: nx, y: ny}) && !this.visited[ny][nx]) { + if (this.cellIsInMaze({ x: nx, y: ny }) && !this.visited[ny][nx]) { this.resetVisits(); this.state = 1; } @@ -149,7 +149,6 @@ class Wilsons extends Maze { } if (this.remaining === 0) this.finishedGenerating = true; - } eraseLoopFrom(x, y) { diff --git a/algorithms/Wilsons2.js b/algorithms/Wilsons2.js index d783fde..d29370e 100644 --- a/algorithms/Wilsons2.js +++ b/algorithms/Wilsons2.js @@ -6,7 +6,7 @@ class Wilsons extends Maze { this.unvisited = []; for (let y = 0; y < this.height; y++) { for (let x = 0; x < this.width; x++) { - this.unvisited .push(`${x},${y}`); + this.unvisited.push(`${x},${y}`); } } @@ -14,23 +14,29 @@ class Wilsons extends Maze { x: this.start.x, y: this.start.y, }; - this.unvisited.splice(this.unvisited.indexOf(`${currentCell.x},${currentCell.y}`), 1); + this.unvisited.splice( + this.unvisited.indexOf(`${currentCell.x},${currentCell.y}`), + 1, + ); this.state = 0; this.remaining = this.width * this.height; this.visits = {}; } - takeStep(){ - curre - while(this.unvisited.length > 0){ - let cell = this.unvisited[Math.floor(this.prng.random()*this.unvisited.length)]; + takeStep() { + curre; + while (this.unvisited.length > 0) { + let cell = + this.unvisited[Math.floor(this.prng.random() * this.unvisited.length)]; let path = [cell]; - while(this.unvisited.indexOf(`${currentCell.x},${currentCell.y}`) !== -1){ + while ( + this.unvisited.indexOf(`${currentCell.x},${currentCell.y}`) !== -1 + ) { } } - if(this.unvisited.length === 0 ) this.finishedGenerating = true; + if (this.unvisited.length === 0) this.finishedGenerating = true; } } diff --git a/createWidget.js b/createWidget.js index 3300eb7..50d805c 100644 --- a/createWidget.js +++ b/createWidget.js @@ -1,28 +1,34 @@ import Maze from "./Maze.js"; //Node doesn't have a fetch function so we need to fix that -if (typeof window === 'undefined'){ - globalThis.fetch = globalThis.fetch ?? require('node-fetch'); +if (typeof window === "undefined") { + globalThis.fetch = globalThis.fetch ?? require("node-fetch"); } -export default function createWidget(mazeSettings={}, displaySettings=mazeSettings, widgetSettings=mazeSettings) { +export default function createWidget( + mazeSettings = {}, + displaySettings = mazeSettings, + widgetSettings = mazeSettings, +) { mazeSettings = { size: mazeSettings.height || mazeSettings.ySize || 15, - ...mazeSettings - } + ...mazeSettings, + }; displaySettings = { displayMode: 0, antiAliasing: false, - ...displaySettings - } + ...displaySettings, + }; widgetSettings = { paused: false, imageButtons: false, - containerElement: document.getElementById("maze-widget-container") ?? document?.getElementsByClassName("maze-widget-container")[0] ?? document.body, - ...widgetSettings - } + containerElement: document.getElementById("maze-widget-container") ?? + document?.getElementsByClassName("maze-widget-container")[0] ?? + document.body, + ...widgetSettings, + }; let maze = Maze.create(mazeSettings); @@ -32,7 +38,8 @@ export default function createWidget(mazeSettings={}, displaySettings=mazeSettin const iconImageFolderURL = "https://x.nest.land/maze_generator@0.1.0/images/button-icons"; - const cssFileURL = "https://x.nest.land/maze_generator@0.1.0/widget-styles.css"; + const cssFileURL = + "https://x.nest.land/maze_generator@0.1.0/widget-styles.css"; function getButtonInnerHTML(buttonName) { if (widgetSettings.imageButtons) { diff --git a/display.js b/display.js index f82f693..2a40597 100644 --- a/display.js +++ b/display.js @@ -1,4 +1,4 @@ -import {directions} from "./directions.js" +import { directions } from "./directions.js"; export default function display({ maze, @@ -15,32 +15,36 @@ export default function display({ showSolution = false, solutionColor = "#F00", distanceFrom = maze.start, - removeWallsAtEntranceAndExit = true + removeWallsAtEntranceAndExit = true, }) { if (!canvas) { console.error("Tried to display maze without a canvas"); return false; } - let { distances, maxDistance, - } = coloringMode=== "distance" ? maze.getDistances(distanceFrom) : {distances: null, maxDistance: null}; + } = coloringMode === "distance" + ? maze.getDistances(distanceFrom) + : { distances: null, maxDistance: null }; //remove the walls at the entrance and exit if it is set to that let entranceWallBefore; let exitWallBefore; - if(removeWallsAtEntranceAndExit){ + if (removeWallsAtEntranceAndExit) { //if the entrance wall is a valid direction - if(directions.indexOf(maze.entrance.direction) !== -1){ - entranceWallBefore = maze.walls[maze.entrance.y][maze.entrance.x][maze.entrance.direction] - maze.walls[maze.entrance.y][maze.entrance.x][maze.entrance.direction] = false; + if (directions.indexOf(maze.entrance.direction) !== -1) { + entranceWallBefore = + maze.walls[maze.entrance.y][maze.entrance.x][maze.entrance.direction]; + maze.walls[maze.entrance.y][maze.entrance.x][maze.entrance.direction] = + false; } //if the exit wall is a valid direction - if(directions.indexOf(maze.exit.direction) !== -1){ - exitWallBefore = maze.walls[maze.exit.y][maze.exit.x][maze.exit.direction] + if (directions.indexOf(maze.exit.direction) !== -1) { + exitWallBefore = + maze.walls[maze.exit.y][maze.exit.x][maze.exit.direction]; maze.walls[maze.exit.y][maze.exit.x][maze.exit.direction] = false; } } @@ -254,15 +258,17 @@ export default function display({ } //put the walls at the entrance and exit back if they were there before - if(removeWallsAtEntranceAndExit){ + if (removeWallsAtEntranceAndExit) { //re-add the entrance wall if it was taken away to begin with - if(directions.indexOf(maze.entrance.direction) !== -1){ - maze.walls[maze.entrance.y][maze.entrance.x][maze.entrance.direction] = entranceWallBefore; + if (directions.indexOf(maze.entrance.direction) !== -1) { + maze.walls[maze.entrance.y][maze.entrance.x][maze.entrance.direction] = + entranceWallBefore; } //re-add the exit wall if it was taken away to begin with - if(directions.indexOf(maze.exit.direction) !== -1){ - maze.walls[maze.exit.y][maze.exit.x][maze.exit.direction] = exitWallBefore; + if (directions.indexOf(maze.exit.direction) !== -1) { + maze.walls[maze.exit.y][maze.exit.x][maze.exit.direction] = + exitWallBefore; } } diff --git a/mod.js b/mod.js index 8f8f4d6..cc02d60 100644 --- a/mod.js +++ b/mod.js @@ -30,7 +30,7 @@ Maze.algorithms = { sidewinder: Sidewinder, binarytree: BinaryTree, trueprims: TruePrims, - tenprint: TenPrint + tenprint: TenPrint, }; export { default as analyze } from "./analyze.js"; @@ -50,5 +50,5 @@ export { Kruskals, BinaryTree, Wilsons, - TruePrims + TruePrims, };