xxxxxxxxxx
105
function Cell(i, j){
this.i = i;
this.j = j;
this.walls = [true, true, true, true]
this.visited = false;
this.neighbors = [undefined,undefined,undefined,undefined];
this.f = 0;
this.g = 0;
this.h = 0;
this.previous = undefined;
this.checkNeighborsMaze = function(grid) {
var neighbors = [];
var top;
var right;
var bottom;
var left;
if(j>0){
top = grid[i][j-1];
}
if(i<cols - 1){
right = grid[i+1][j];
}
if(i<rows - 1){
bottom = grid[i][j+1];
}
if(i>0){
left = grid[i-1][j];
}
if(top && !top.visited){
neighbors.push(top);
}
if(right && !right.visited){
neighbors.push(right);
}
if(bottom && !bottom.visited){
neighbors.push(bottom);
}
if(left && !left.visited){
neighbors.push(left);
}
if(neighbors.length > 0){
var r = floor(random(0,neighbors.length));
return neighbors[r]
}else{
return undefined;
}
}
this.addNeighbor = function(n){
if(this.neighbors[0]){
this.neighbors[1] = n;
} else if(this.neighbors[1]){
this.neighbors[2] = n;
} else if(this.neighbors[2]){
this.neighbors[3] = n;
} else{
this.neighbors[0] = n;
}
}
this.highlight = function(c) {
var x = this.i * scl;
var y = this.j * scl;
noStroke();
fill(c);
rect(x,y,scl,scl)
}
this.show = function(c){
var x = this.i * scl;
var y = this.j * scl;
stroke(255);
fill(c);
//top
if(this.walls[0]){
line(x,y,x+scl,y);
}
//right
if(this.walls[1]){
line(x+scl,y,x+scl,y+scl);
}
//bottom
if(this.walls[2]){
line(x+scl,y+scl,x,y+scl);
}
//left
if(this.walls[3]){
line(x,y+scl,x,y);
}
if(this.visited){
noStroke();
fill(255,0,255,90);
rect(x,y,scl,scl)
}
}
}