Added a readme.

This commit is contained in:
2021-02-08 00:19:30 -06:00
parent 7405f88bd6
commit 6f23674730
4 changed files with 79 additions and 19 deletions
+60 -15
View File
@@ -1,13 +1,9 @@
class MazeSolver {
constructor(startingCell, targetCell, searchedColor = [210, 180, 140], finalPathColor = [173, 216, 230]){
constructor(startingCell, targetCell, searchedColor = [210, 180, 140]){
this.frontier = [];
this.explored = [];
this.startingCellColor = [255, 0, 0];
this.endingCellColor = [0, 0, 255];
this.finalPathColor = finalPathColor;
this.targetCell = targetCell;
this.startingCell = startingCell;
this.searchedColor = searchedColor;
this.currentCell = startingCell;
this.frontier.push(startingCell);
@@ -20,18 +16,18 @@ class MazeSolver {
this.statesExplored = 0;
}
highlightFinalPath(maze){
highlightFinalPath(maze, finalPathColor = [173, 216, 230], startingCellColor = [255, 0, 0], endingCellColor = [0, 0, 255]){
let tmp = this.explored.pop();
tmp.color = this.endingCellColor;
tmp.color = endingCellColor;
tmp = tmp.parent;
while(tmp != undefined){
tmp.color = this.finalPathColor;
tmp.color = finalPathColor;
if(tmp.parent == undefined)
tmp.color = this.startingCellColor;
tmp.color = startingCellColor;
tmp = tmp.parent;
}
@@ -89,11 +85,60 @@ class BreadthFirstSolver extends MazeSolver {
this.currentCell.color = this.searchedColor;
}
}
/*
class AStarSolver extends MazeSolver {
constructor(startingCell, targetCell, searchedColor = [210, 180, 140], finalPathColor = [173, 216, 230]) {
super(staringCell, targetCell, searchedColor, finalPathColor);
class GreedyDepthFirstSolver extends MazeSolver {
solve(maze) {
let lowest = Infinity;
this.
this.currentCell.color = this.searchedColor;
this.currentCell = this.frontier.pop();
this.currentCell.visited = true;
let moves = maze.getLegalMoves(this.currentCell);
print(moves);
this.sortFrontier(moves);
print(moves);
for (let i = 0; i < moves.length; i++){
this.frontier.push(moves[i]);
moves[i].parent = this.currentCell;
}
this.explored.push(this.currentCell);
this.statesExplored++;
if(this.currentCell.x == this.targetCell.x && this.currentCell.y == this.targetCell.y)
this.frontier = [];
this.currentCell.color = this.searchedColor;
}
}*/
// Quick and dirty bubble sort
sortFrontier(list){
if(list.length == 1) return;
let swapped = true;
while(swapped){
for(let i = 0; i < list.length - 1; i++){
var current = this.calcDistance(this.targetCell, list[i]);
var next = this.calcDistance(this.targetCell, list[i + 1]);
if(current < next){
let tmp = list[i];
let tmp2 = list[i+1];
list[i + 1] = tmp2;
list[i] = tmp;
swapped = true;
}
else swapped = false;
}
}
}
calcDistance(target, current){
let x = target.x - current.x;
let y = target.y - current.y;
return x + y;
}
}