xxxxxxxxxx
117
var cols,rows;
var scl = 40;
var grid = [];
var stack = [];
var ends = [];
var current;
var gate1 = 0;
function setup() {
createCanvas(400, 400);
cols = floor(width/scl);
rows = floor(height/scl);
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];
//frameRate(2);
}
function draw() {
background(51);
if(gate1 == 0){
for(let i = 0; i < 1; i ++){
backtracking();
}
}else if(gate1 == 1){
for(let end of ends){
end.highlight();
let choice = random(1);
if(choice < 0.5){
let wall = random(end.walls);
console.log("1",end.walls)
while(!wall){
wall = random(end.walls);
}
end.wall = false;
console.log("2",end.walls)
}
}
gate1++;
}else{
for(let i = 0; i < grid.length; i++){
grid[i].show();
}
}
}
var gate = true;
function backtracking(){
for(var i = 0; i < grid.length; i++){
grid[i].show();
}
current.visited = true;
current.highlight();
//Step 1
var next = current.checkNeigbors();
if (next) {
next.visited = true;
//Step 2
stack.push(current);
//Step 3
removeWalls(current,next);
//Step 4
current = next;
gate = true;
} else if(stack.length > 0){
if(gate){
ends.push(current);
gate = false;
}
current = stack.pop();
}
if(current == grid[0]){
gate1 ++;
}
}
function index(i,j){
if(i < 0 || j < 0 || i > cols-1|| j > rows-1){
return -1;
}
return i + j * cols;
}
function removeWalls(a,b) {
var x = a.i - b.i;
if(x === 1){
a.walls[3] = false;
b.walls[1] = false;
} else if (x === -1){
a.walls[1] = false;
b.walls[3] = false;
}
var y = a.j - b.j;
if(y === 1){
a.walls[0] = false;
b.walls[2] = false;
} else if (y === -1){
a.walls[2] = false;
b.walls[0] = false;
}
}