Skip to content

Commit

Permalink
formatted files
Browse files Browse the repository at this point in the history
  • Loading branch information
mjrlowe committed Jul 22, 2020
1 parent b19901b commit c7a81d0
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 89 deletions.
29 changes: 24 additions & 5 deletions Maze.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" ||
Expand Down Expand Up @@ -106,7 +125,7 @@ class Maze {
}

step() {
if(this.finishedGenerating) return false;
if (this.finishedGenerating) return false;
this.takeStep();
return !this.finishedGenerating;
}
Expand Down
21 changes: 9 additions & 12 deletions algorithms/10Print.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,39 @@ 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;
}

takeStep() {
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]);
}

Expand All @@ -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;
export default TenPrint;
2 changes: 0 additions & 2 deletions algorithms/AldousBroder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -54,7 +53,6 @@ class AldousBroder extends Maze {
if (this.totalVisted >= this.width * this.height) {
this.finishedGenerating = true;
}

}
}

Expand Down
1 change: 0 additions & 1 deletion algorithms/BinaryTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class BinaryTree extends Maze {
) {
this.finishedGenerating = true;
}

}
}

Expand Down
2 changes: 0 additions & 2 deletions algorithms/Ellers.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,11 @@ class Eller extends Maze {
}

takeStep() {

if (this.mode === this.HORIZONTAL) {
this.horizontalStep();
} else {
this.verticalStep();
}

}
}

Expand Down
2 changes: 0 additions & 2 deletions algorithms/HuntAndKill.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class HuntAndKill extends Maze {
}

takeStep() {

//random walk
if (!this.hunting) {
this.visited[this.currentCell.y][this.currentCell.x] = true;
Expand Down Expand Up @@ -149,7 +148,6 @@ class HuntAndKill extends Maze {
}
}
}

}
}

Expand Down
2 changes: 0 additions & 2 deletions algorithms/Kruskals.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class Kruskals extends Maze {
}

takeStep() {

let edge = this.edges.pop();
let cell1 = {
x: edge.x,
Expand All @@ -57,7 +56,6 @@ class Kruskals extends Maze {
}

if (this.edges.length === 0) this.finishedGenerating = true;

}

getCellIndex(cell) {
Expand Down
2 changes: 0 additions & 2 deletions algorithms/ModifiedPrims.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class ModifiedPrims extends Maze {
}

takeStep() {

//find index of cell with minimum cost
let minCost = Infinity;
let cellIndex = 0;
Expand Down Expand Up @@ -80,7 +79,6 @@ class ModifiedPrims extends Maze {
}

if (this.activeCells.length === 0) this.finishedGenerating = true;

}
}

Expand Down
2 changes: 0 additions & 2 deletions algorithms/RecursiveDivision.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class RecursiveDivision extends Maze {
}

takeStep() {

switch (this.state) {
case this.CHOOSE_REGION:
this.chooseRegion();
Expand All @@ -55,7 +54,6 @@ class RecursiveDivision extends Maze {
this.state,
);
}

}

chooseRegion() {
Expand Down
1 change: 0 additions & 1 deletion algorithms/Sidewinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class Sidewinder extends Maze {
if (this.currentCell.y >= this.height) {
this.finishedGenerating = true;
}

}
}

Expand Down
1 change: 0 additions & 1 deletion algorithms/SimplifiedPrims.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class SimplifiedPrims extends Maze {
}

takeStep() {

let cellIndex = Math.floor(this.prng.random() * this.activeCells.length);
let cell = this.activeCells[cellIndex];

Expand Down
35 changes: 19 additions & 16 deletions algorithms/TruePrims.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
dx,
dy,
directions,
opposite
opposite,
} from "../directions.js";

class TruePrims extends Maze {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
}
}

Expand Down
9 changes: 4 additions & 5 deletions algorithms/Wilsons.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]++;
}
Expand Down Expand Up @@ -59,7 +59,7 @@ class Wilsons extends Maze {
}
}

console.log("i", i, this)
console.log("i", i, this);
}

walkStep() {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -149,7 +149,6 @@ class Wilsons extends Maze {
}

if (this.remaining === 0) this.finishedGenerating = true;

}

eraseLoopFrom(x, y) {
Expand Down
Loading

0 comments on commit c7a81d0

Please sign in to comment.