xxxxxxxxxx
86
const n_rows = 10
const n_cols = 10
const wh = 40
const rows = []
const DELAY = 1000
class Cell{
constructor(row, col, wall){
this.row = row
this.col = col
this.wall = wall % 2 === 0
this.visited = false
}
show() {
if (this.wall) fill(255);
else fill(100)
// noFill()
rect(this.row * wh, this.col * wh, wh, wh)
}
changeColor(){
fill((0, 255, 0))
rect(this.row * wh, this.col * wh, wh, wh)
}
}
const drawMaze = () => {
background(51);
for (let i = 0; i < n_rows; i++){
for (let j = 0; j < n_cols; j++){
rows[i][j].show()
}
}
}
function setup() {
createCanvas(400, 400);
for (let i = 0; i < n_rows; i++){
cols = []
for (let j = 0; j < n_cols; j++){
let a = Math.floor(Math.random() * 10)
cols.push(new Cell(i, j, a))
console.log(a)
}
rows.push(cols)
}
}
function draw() {
noLoop();
const directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]
const stackN = [rows[0][0]]
rows[0][0].wall = false
drawMaze()
while (stackN.length !== 0){
while (actionInProgress)
{}
actionInProgress = true
let cur = stackN.shift()
cur.visited = true
console.log(cur)
directions.forEach((dir) => {
console.log(cur.row + dir[0], cur.col + dir[1], cur.wall)
console.log(cur.row + dir[0] >= 0, cur.row + dir[0] < n_rows, cur.col + dir[1] >= 0, cur.col + dir[1] < n_cols, !cur.wall)
if (cur.row + dir[0] >= 0 && cur.row + dir[0] < n_rows && cur.col + dir[1] >= 0 && cur.col + dir[1] < n_cols && !cur.wall) {
console.log(`Pushing`)
console.log(rows[cur.row + dir[0]][cur.col + dir[1]])
console.log(rows[cur.row + dir[0]][cur.col + dir[1]].visited)
if (!rows[cur.row + dir[0]][cur.col + dir[1]].visited) {
stackN.push(rows[cur.row + dir[0]][cur.col + dir[1]])
console.log("Pushed")
console.log(stackN.length)
}
}
})
setTimeout(() => {
cur.changeColor()
}, DELAY);
console.log(stackN.length)
}
}