xxxxxxxxxx
94
var w = 30;
var rows, cols;
var grid = [];
var current;
var pcurrent;
var stack = [];
var finished = false;
var percentDone;
function setup() {
frameRate(60);
createCanvas(781, 781);
rows = floor(width / w);
cols = floor(height / w);
for (var j = 0; j < rows; j++) {
for (var i = 0; i < cols; i++) {
var cell = new Cell(i, j);
grid.push(cell);
}
}
current = grid[0];
}
function draw() {
background(51);
bar = [];
for (i = 0; i < 2; i++) {
bar.push(new Bar(58, 6, 200, 25, 2, percentDone));
}
bar.pop();
var totalVisited = 0;
if (pcurrent === current) {
for (var i = 0; i < grid.length; i++) {
grid[i].show();
}
}
if (!finished) {
current.visited = true;
var next = current.checkNeighbors();
bar[0].draw();
if (next) {
next.visited = true;
stack.push(current);
current.highlight();
removeWalls(current, next);
current = next;
} else if (stack.length > 0) {
current = stack.pop();
}
for (var i = 0; i < grid.length; i++) {
if (grid[i].visited) {
totalVisited++;
}
}
}
if (totalVisited === rows * cols) {
finished = true;
}
if (finished) {
noStroke();
textSize((width + height) / 20);
textAlign(CENTER);
fill(255);
text("Maze Finished!", width / 2, 7 / 15 * height);
}
if (!finished) {
percentDone = round(totalVisited / grid.length * 100);
fill(255);
strokeWeight(2);
textSize(24);
text(percentDone + "%", 5, 25);
noStroke();
noFill();
}
pcurrent = current;
}