Skip to content

Commit

Permalink
formatted files
Browse files Browse the repository at this point in the history
  • Loading branch information
mjrlowe committed Sep 13, 2020
1 parent b2965cb commit 539a272
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
6 changes: 3 additions & 3 deletions Maze.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ class Maze {
case "ellers":
return new Maze.algorithms.Ellers(settings);

case "wilson":
case "wilsons":
return new Maze.algorithms.Wilsons(settings);
case "wilson":
case "wilsons":
return new Maze.algorithms.Wilsons(settings);

default:
throw "Invalid algorithm";
Expand Down
44 changes: 22 additions & 22 deletions algorithms/Wilsons.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
dx,
dy,
opposite,
directions
directions,
} from "../directions.js";

class Wilsons extends Maze {
Expand All @@ -16,7 +16,7 @@ class Wilsons extends Maze {
this.visited[y][x] = false;
this.unvisited.push({
x,
y
y,
});
}
}
Expand All @@ -29,62 +29,62 @@ class Wilsons extends Maze {
}

takeStep() {

//pick a random cell to start a new path
this.currentCell =
this.unvisited[Math.floor(this.prng.random() * this.unvisited.length)];
let path = [this.currentCell];

while (!this.visited[this.currentCell.y][this.currentCell.x]) {
let validNeighbours = [];
for (let direction of directions) {
let neighbour = {
x: this.currentCell.x + dx[direction],
y: this.currentCell.y + dy[direction],
direction
}
if(this.cellIsInMaze(neighbour)) validNeighbours.push(neighbour)
direction,
};
if (this.cellIsInMaze(neighbour)) validNeighbours.push(neighbour);
}
let newCell = validNeighbours[Math.floor(this.prng.random() * validNeighbours.length)];
let newCell =
validNeighbours[
Math.floor(this.prng.random() * validNeighbours.length)
];
let cellVisited = false;
let cellPreviousIndex = -1;
path.forEach((pathCell, index) => {
if(pathCell.x === newCell.x && pathCell.y === newCell.y){
if (pathCell.x === newCell.x && pathCell.y === newCell.y) {
cellVisited = true;
cellPreviousIndex = index;
}
})
});

if(!cellVisited ){
path.push(newCell)
if (!cellVisited) {
path.push(newCell);
this.currentCell = newCell;
}else{
} else {
this.currentCell = path[cellPreviousIndex];
path = path.slice(0, cellPreviousIndex+1)
path = path.slice(0, cellPreviousIndex + 1);
}
}

for(let cell of path){
this.removeWall({x: cell.x, y: cell.y}, opposite[cell.direction])
this.markAsVisited(cell)
for (let cell of path) {
this.removeWall({ x: cell.x, y: cell.y }, opposite[cell.direction]);
this.markAsVisited(cell);
}

if (this.unvisited.length === 0) this.finishedGenerating = true;
}

markAsVisited(newlyVisitedCell) {

this.visited[newlyVisitedCell.y][newlyVisitedCell.x] = true;
this.unvisited.forEach((
cell,
index
index,
) => {
if (cell.x === newlyVisitedCell.x && cell.y === newlyVisitedCell.y) {
this.unvisited.splice(index, 1);
}

})
});
}
}

export default Wilsons;
export default Wilsons;
2 changes: 1 addition & 1 deletion examples/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ let m = Maze.create(mazeSettings);
m.generate();

//print it to the console
m.printString();
m.printString();
4 changes: 2 additions & 2 deletions mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Maze.algorithms = {
BinaryTree,
TruePrims,
TenPrint,
Wilsons
Wilsons,
};

export { Maze };
export { Maze };

0 comments on commit 539a272

Please sign in to comment.