var cellWidth = 50; var columns = 0; var rows = 0; var grid = []; //var currentCell; var cellStack = []; var frontier = []; var explored = []; var currentNode = {}; var maze = {}; function setup() { var canvas = createCanvas(500, 500); canvas.parent('sketch-holder'); maze = new Maze(width, height, cellWidth); frameRate(60); } function draw() { background(220); maze.updateMazeDisplay(); if(maze.workingStack.length > 0){ 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; } 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); } 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(); }*/ 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; }