Trying to port some of the code into objects to clean up the sketch.js file.

This commit is contained in:
2020-05-18 23:48:17 -05:00
commit 2fbcc0b345
4 changed files with 423 additions and 0 deletions
+191
View File
@@ -0,0 +1,191 @@
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);
rows = floor(width / cellWidth);
columns = floor(height / cellWidth);
for (let y = 0; y < rows; y++) {
for (var x = 0; x < columns; x++) {
grid.push(new Cell(x, y, cellWidth));
}
}
//currentCell = grid[0];
grid[0].visited = true;
grid[0].selected = true;
cellStack.push(grid[0]);
}
function draw() {
background(220);
for (var i = 0; i < grid.length; i++) {
grid[i].show();
}
if(cellStack.length > 0){
//Build the maze
let currentCell = cellStack.pop();
//currentCell.visited = true;
let nextCell = getNeighbors(currentCell);
if(nextCell){
cellStack.push(currentCell);
currentCell.Selected = false;
currentCell.visited = true;
nextCell.visited = true;
nextCell.selected = true;
removeWalls(currentCell, nextCell);
cellStack.push(nextCell);
} else cellStack.pop();
currentCell.selected = false;
}
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;
}