xxxxxxxxxx
73
// from Coding Train: https://www.youtube.com/watch?v=aKYlikFAV4k&list=RDCMUCvjgXvBlbQiydffZU7m1_aw&index=2&ab_channel=TheCodingTrain.
let cols = 5;
let rows = 5;
let grid = new Array(cols);
let openSet = [];
let closedSet = [];
let start;
let end;
let w, h;
function Spot(i, j) {
this.x = i;
this.y = j;
this.f = 0;
this.g = 0;
this.h = 0;
this.show = function (col) {
fill(col);
// noStroke();
rect(this.x * w, this.y * h, w, h);
};
}
function setup() {
createCanvas(400, 400);
w = width / cols;
h = height / rows;
for (let i = 0; i < cols; i++) {
grid[i] = new Array(rows);
}
// create the grid
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
grid[i][j] = new Spot(i, j);
}
}
start = grid[0][0];
end = grid[cols - 1][rows - 1];
openSet.push(start);
console.table(grid);
}
function draw() {
background(255);
if (openSet.length > 0) {
// keep going
} else {
// no solution
}
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
grid[i][j].show(color(255));
}
}
for (let i = 0; i < closedSet.length; i++) {
closedSet[i].show(color(255,0,0));
}
for (let i = 0; i < openSet.length; i++) {
openSet[i].show(color(0,255,0));
}
}