Added a readme.
This commit is contained in:
+3
-2
@@ -9,6 +9,7 @@
|
||||
body {
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
background-color: Gainsboro;
|
||||
}
|
||||
|
||||
.column {
|
||||
@@ -60,7 +61,6 @@
|
||||
frameRate(Number(document.getElementById("genFrameRate").value));
|
||||
algo = {};
|
||||
maze.resetMazeState();
|
||||
maze.updateMazeDisplay();
|
||||
loop();
|
||||
}
|
||||
</script>
|
||||
@@ -90,8 +90,9 @@
|
||||
<select id="searchAlgo">
|
||||
<option value="depthFirst">Depth First</option>
|
||||
<option value="breadthFirst">Breadth First</option>
|
||||
<!--<option value="a*">a*</option>-->
|
||||
<option value="greedy">Greedy Depth First</option>
|
||||
</select>
|
||||
<label id="statesExploredLabel" />
|
||||
</fieldset>
|
||||
<fieldset id="misc" disabled="disabled">
|
||||
<legend>Misc.</legend>
|
||||
|
||||
+60
-15
@@ -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);
|
||||
|
||||
this.
|
||||
class GreedyDepthFirstSolver extends MazeSolver {
|
||||
solve(maze) {
|
||||
let lowest = Infinity;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
A JavaScript maze solver I wrote while doing an AI class that Harvard offered.
|
||||
Mostly was me playing around with a few different ways to build a maze solving algorithm.
|
||||
@@ -1,5 +1,17 @@
|
||||
var maze = {};
|
||||
var algo = {};
|
||||
var targetCell = {};
|
||||
|
||||
function mouseClicked(){
|
||||
if(mouseX > width || mouseY > height) return;
|
||||
//let x = floor(maze.columns * maze.cellWidth / mouseX);
|
||||
//let y = floor(maze.rows * maze.cellWidth / mouseY);
|
||||
let x = floor(mouseX / maze.cellWidth);
|
||||
let y = floor(mouseY / maze.cellWidth);
|
||||
|
||||
maze.grid[maze.index(x, y)].color = [0, 0, 0];
|
||||
redraw();
|
||||
}
|
||||
|
||||
function setup() {
|
||||
var canvas = createCanvas(500, 500);
|
||||
@@ -33,8 +45,8 @@ function draw() {
|
||||
algo = new DepthFirstSolver(maze.grid[0], maze.grid[maze.index(7, 7)]);
|
||||
else if(selectedAlgo == "breadthFirst")
|
||||
algo = new BreadthFirstSolver(maze.grid[0], maze.grid[maze.index(7,7)]);
|
||||
//else if(selectedAlgo == "a*")
|
||||
|
||||
else if(selectedAlgo == "greedy")
|
||||
algo = new GreedyDepthFirstSolver(maze.grid[0], maze.grid[maze.index(7,7)]);
|
||||
print("Maze generation done");
|
||||
frameRate(Number(document.getElementById("solvingFrameRate").value));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user