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:
@@ -1,19 +1,16 @@
|
||||
var cellWidth = 50;
|
||||
var columns = 0;
|
||||
var rows = 0;
|
||||
var grid = [];
|
||||
//var currentCell;
|
||||
var cellStack = [];
|
||||
var frontier = [];
|
||||
var explored = [];
|
||||
var currentNode = {};
|
||||
var maze = {};
|
||||
var algo = {};
|
||||
|
||||
function setup() {
|
||||
var canvas = createCanvas(500, 500);
|
||||
print("hell");
|
||||
var canvas = createCanvas(400, 400);
|
||||
canvas.parent('sketch-holder');
|
||||
maze = new Maze(width, height, cellWidth);
|
||||
frameRate(60);
|
||||
maze = new Maze(width, height, cellWidth, "Depth");
|
||||
frameRate(30);
|
||||
}
|
||||
|
||||
function draw() {
|
||||
@@ -21,139 +18,23 @@ function draw() {
|
||||
|
||||
maze.updateMazeDisplay();
|
||||
|
||||
if(maze.workingStack.length > 0){
|
||||
if(!maze.mazeGenDone){
|
||||
maze.buildNextCell();
|
||||
}
|
||||
/*else if (cellStack.length == 0 && frontier.length == 0 && explored.length == 0){
|
||||
for (var i = 0; i < grid.length; i++) {
|
||||
grid[i].visited = false;
|
||||
}
|
||||
|
||||
else if (maze.mazeGenDone && algo.frontier == undefined){
|
||||
maze.resetMazeState();
|
||||
algo = new MazeSolver(maze.grid[0], maze.grid[maze.index(7,7)]);
|
||||
print("Maze generation done");
|
||||
|
||||
grid[index(0, 0)].selected = true;
|
||||
grid[index(0, 0)].visited = true;
|
||||
grid[index(0, 0)].searched = true;
|
||||
grid[index(0, 0)].start = true;
|
||||
currentNode = grid[index(0, 0)];
|
||||
|
||||
frontier.push(grid[index(0, 0)]);
|
||||
frameRate(5);
|
||||
frameRate(10);
|
||||
}
|
||||
else if (maze.mazeGenDone && algo.frontier.length > 0){
|
||||
algo.breadthFirstNextCell(maze);
|
||||
}
|
||||
else if (cellStack.length == 0 && frontier.length > 0){
|
||||
currentNode.selected = false;
|
||||
currentNode = frontier.shift();
|
||||
|
||||
let neighbors = getAllNeighbors(currentNode);
|
||||
|
||||
currentNode.searched = true;
|
||||
currentNode.selected = true;
|
||||
|
||||
for(let index = 0; index < neighbors.length; index++){
|
||||
frontier.push(neighbors[index]);
|
||||
neighbors[index].parent = currentNode;
|
||||
}
|
||||
|
||||
explored.push(currentNode);
|
||||
|
||||
if(currentNode.x == 9 && currentNode.y == 9)
|
||||
//currentNode.end = true;
|
||||
frontier = [];
|
||||
}*/
|
||||
else{
|
||||
/*tmp = explored.pop();
|
||||
|
||||
while (tmp.parent != undefined) {
|
||||
tmp.finalPath = true;
|
||||
tmp = tmp.parent;
|
||||
}
|
||||
grid[index(0, 0)].finalPath = true;
|
||||
|
||||
for (var i = 0; i < grid.length; i++) {
|
||||
grid[i].show();
|
||||
}*/
|
||||
algo.highlightFinalPath(maze);
|
||||
|
||||
|
||||
print("Maze search done");
|
||||
noLoop();
|
||||
}
|
||||
}
|
||||
|
||||
function index(x, y) {
|
||||
if (x < 0 || y < 0 || x > columns - 1 || y > rows - 1) return -1;
|
||||
return x + y * columns;
|
||||
}
|
||||
|
||||
function removeWalls(a, b) {
|
||||
var x = a.x - b.x;
|
||||
var 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;
|
||||
}
|
||||
}
|
||||
|
||||
function getAllNeighbors(cell){
|
||||
var Neighbors = [];
|
||||
|
||||
var top = grid[index(cell.x, cell.y - 1)];
|
||||
var left = grid[index(cell.x - 1, cell.y)];
|
||||
var bottom = grid[index(cell.x, cell.y + 1)];
|
||||
var right = grid[index(cell.x + 1, cell.y)];
|
||||
|
||||
if (top && !top.searched && !cell.top) {
|
||||
Neighbors.push(top);
|
||||
}
|
||||
if (left && !left.searched && !cell.left) {
|
||||
Neighbors.push(left);
|
||||
}
|
||||
if (right && !right.searched && !cell.right) {
|
||||
Neighbors.push(right);
|
||||
}
|
||||
if (bottom && !bottom.searched && !cell.bottom) {
|
||||
Neighbors.push(bottom);
|
||||
}
|
||||
|
||||
return shuffle(Neighbors);
|
||||
}
|
||||
|
||||
function getNeighbors(cell){
|
||||
var visited = [];
|
||||
|
||||
var top = grid[index(cell.x, cell.y - 1)];
|
||||
var left = grid[index(cell.x - 1, cell.y)];
|
||||
var bottom = grid[index(cell.x, cell.y + 1)];
|
||||
var right = grid[index(cell.x + 1, cell.y)];
|
||||
|
||||
if (top && !top.visited) {
|
||||
visited.push(top);
|
||||
}
|
||||
if (left && !left.visited) {
|
||||
visited.push(left);
|
||||
}
|
||||
if (right && !right.visited) {
|
||||
visited.push(right);
|
||||
}
|
||||
if (bottom && !bottom.visited) {
|
||||
visited.push(bottom);
|
||||
}
|
||||
|
||||
if(visited.length == 1)
|
||||
return visited[0];
|
||||
else if (visited.length > 1)
|
||||
return visited[floor(random(0, visited.length))];
|
||||
else
|
||||
return undefined;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user