Added a maze solver object to handle the search algorithms that will be used to solve the maze. Updated the sketch code to use said object.
This commit is contained in:
+77
-17
@@ -19,31 +19,47 @@ class Maze{
|
||||
this.workingStack.push(this.grid[this.index(0,0)]);
|
||||
}
|
||||
|
||||
getStartingCell(){
|
||||
return this.grid[0];
|
||||
}
|
||||
|
||||
getEndingCell(){
|
||||
return this.grid[this.index(9, 9)];
|
||||
}
|
||||
|
||||
updateMazeDisplay(){
|
||||
for (var i = 0; i < this.grid.length; i++) {
|
||||
for (let i = 0; i < this.grid.length; i++) {
|
||||
this.grid[i].show();
|
||||
}
|
||||
}
|
||||
|
||||
buildNextCell(){
|
||||
resetMazeState(){
|
||||
for (let i = 0; i < this.grid.length; i++){
|
||||
this.grid[i].visited = false;
|
||||
this.grid[i].color = this.grid[i].defaultColor;
|
||||
this.grid[i].parent = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
buildNextCell(selectedColor = [50, 175, 50]){
|
||||
//Build the maze
|
||||
let currentCell = this.workingStack.pop();
|
||||
//currentCell.visited = true;
|
||||
|
||||
let nextCell = this.getNeighbors(currentCell);
|
||||
|
||||
if(nextCell){
|
||||
this.workingStack.push(currentCell);
|
||||
currentCell.Selected = false;
|
||||
currentCell.color = currentCell.defaultColor;
|
||||
currentCell.visited = true;
|
||||
nextCell.visited = true;
|
||||
nextCell.selected = true;
|
||||
removeWalls(currentCell, nextCell);
|
||||
nextCell.color = selectedColor;
|
||||
this.removeWalls(currentCell, nextCell);
|
||||
this.workingStack.push(nextCell);
|
||||
}
|
||||
else
|
||||
this.workingStack.pop();
|
||||
|
||||
currentCell.selected = false;
|
||||
currentCell.color = currentCell.defaultColor;
|
||||
}
|
||||
|
||||
get mazeGenDone() {
|
||||
@@ -51,12 +67,12 @@ class Maze{
|
||||
}
|
||||
|
||||
getNeighbors(cell){
|
||||
var visited = [];
|
||||
let visited = [];
|
||||
|
||||
var top = this.grid[this.index(cell.x, cell.y - 1)];
|
||||
var left = this.grid[this.index(cell.x - 1, cell.y)];
|
||||
var bottom = this.grid[this.index(cell.x, cell.y + 1)];
|
||||
var right = this.grid[this.index(cell.x + 1, cell.y)];
|
||||
let top = this.grid[this.index(cell.x, cell.y - 1)];
|
||||
let left = this.grid[this.index(cell.x - 1, cell.y)];
|
||||
let bottom = this.grid[this.index(cell.x, cell.y + 1)];
|
||||
let right = this.grid[this.index(cell.x + 1, cell.y)];
|
||||
|
||||
if (top && !top.visited) {
|
||||
visited.push(top);
|
||||
@@ -77,11 +93,55 @@ class Maze{
|
||||
return visited[floor(random(0, visited.length))];
|
||||
else
|
||||
return undefined;
|
||||
}
|
||||
|
||||
index(x, y) {
|
||||
if (x < 0 || y < 0 || x > this.columns - 1 || y > this.rows - 1) return -1;
|
||||
return x + y * this.columns;
|
||||
}
|
||||
|
||||
getLegalMoves(cell){
|
||||
let neighbors = [];
|
||||
|
||||
let top = this.grid[this.index(cell.x, cell.y - 1)];
|
||||
let left = this.grid[this.index(cell.x - 1, cell.y)];
|
||||
let bottom = this.grid[this.index(cell.x, cell.y + 1)];
|
||||
let right = this.grid[this.index(cell.x + 1, cell.y)];
|
||||
|
||||
if (top && !top.visited && !cell.top) {
|
||||
neighbors.push(top);
|
||||
}
|
||||
if (left && !left.visited && !cell.left) {
|
||||
neighbors.push(left);
|
||||
}
|
||||
if (right && !right.visited && !cell.right) {
|
||||
neighbors.push(right);
|
||||
}
|
||||
if (bottom && !bottom.visited && !cell.bottom) {
|
||||
neighbors.push(bottom);
|
||||
}
|
||||
|
||||
return shuffle(neighbors);
|
||||
}
|
||||
|
||||
removeWalls(a, b) {
|
||||
let x = a.x - b.x;
|
||||
let y = a.y - b.y;
|
||||
|
||||
if (x === -1) {
|
||||
a.setRight = false;
|
||||
b.setLeft = false;
|
||||
} else if (x === 1) {
|
||||
a.setLeft = false;
|
||||
b.setRight = false;
|
||||
}
|
||||
|
||||
if (y === -1) {
|
||||
a.setBottom = false;
|
||||
b.setTop = false;
|
||||
} else if (y === 1) {
|
||||
a.setTop = false;
|
||||
b.setBottom = false;
|
||||
}
|
||||
}
|
||||
|
||||
index(x, y) {
|
||||
if (x < 0 || y < 0 || x > this.columns - 1 || y > this.rows - 1) return -1;
|
||||
return x + y * this.columns;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user