xxxxxxxxxx
212
const n_rows = 10
const n_cols = 10
const wh = 60
let rows = []
const DELAY = 1100
let globalArray = []
let stopBFS = false
let stopDFS = true
const walls = new Set([1.1, 1.3, 1.5, 1.6, 1.7, 1.8, 2.3, 2.7, 2.8, 3.1, 3.3, 3.8, 4.0, 4.5, 4.8, 5.2, 5.4, 5.5, 5.7, 5.8, 6.2, 6.8, 7.0, 7.2, 7.4, 7.9, 8.0, 8.2, 8.3, 8.6, 8.9, 9.0]);
class Cell{
constructor(row, col, wall){
this.row = row
this.col = col
//this.wall = wall % 2 === 0
this.wall = walls.has(col + (row/10))
this.visited = false
this.cellColor = this.wall ? 'blue' : 'white'
}
show() {
fill(this.cellColor)
//noFill()
rect(this.row * wh, this.col * wh, wh, wh)
if (!this.wall){
fill('black');
textSize(16);
text(`${(this.col * 10) + this.row}`, (this.row * wh + (wh/3)), ((this.col * wh) - wh/2) + wh);
}
}
changeColor(){
this.cellColor = 'orange'
}
}
const drawMaze = () => {
background(101);
for (let i = 0; i < n_rows; i++){
for (let j = 0; j < n_cols; j++){
rows[i][j].show()
}
}
}
const drawArray = () => {
fill(101)
rect(0 * wh, 600 + wh, 1000, wh)
for(let i = 0; i < globalArray.length; i++){
fill('yellow')
rect(i * wh + wh, 600 + wh, wh, wh)
fill('black');
const cell = globalArray[i]
textSize(16);
text(`${(cell.col * 10) + cell.row}`, i * wh + wh + wh/3, 600 + wh + wh/2);
}
}
const button = (title, x, y, pressedFn) => {
const btn = createButton(title);
btn.position(x, y);
btn.mousePressed(pressedFn);
return btn;
};
const generateMaze = () => {
rows = []
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))
}
rows.push(cols)
}
}
const startBFS = () => {
stopDFS = true
generateMaze()
globalArray = []
rows[0][0].wall = false
drawMaze()
drawArray()
findPathBFS()
}
const startDFS = () => {
stopBFS = true
generateMaze()
globalArray = []
rows[0][0].wall = false
console.log(globalArray)
drawMaze()
drawArray()
findPathDFS()
}
function setup() {
bfsBtn = button("Breath First Search", 650, 10, startBFS);
dfsBtn = button("Depth First Search", 650, 50, startDFS);
createCanvas(1000, 800);
generateMaze()
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const findPathBFS = async () => {
const visitQueue = globalArray
stopDFS = true
stopBFS = false
const directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]
visitQueue.push(rows[0][0])
drawArray()
while (visitQueue.length !== 0 && !stopBFS){
const cur = visitQueue.shift()
if (!cur.visited){
directions.forEach((dir) => {
neighboorRow = cur.row + dir[0]
neighboorCol = cur.col + dir[1]
if (neighboorRow >= 0 && neighboorRow < n_rows && neighboorCol >= 0 && neighboorCol < n_cols) {
neighboorCell = rows[neighboorRow][neighboorCol]
if (!neighboorCell.visited && !neighboorCell.wall) {
visitQueue.push(neighboorCell)
}
}
})
await sleep(DELAY)
cur.changeColor()
drawMaze()
drawArray()
}
cur.visited = true
}
}
const findPathDFS = async () => {
const visitStack = globalArray
stopBFS = true
stopDFS = false
const directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]
visitStack.push(rows[0][0])
drawArray()
while (visitStack.length !== 0 && !stopDFS) {
const cur = visitStack.pop()
if (!cur.visited){
cur.visited = true
directions.forEach((dir) => {
neighboorRow = cur.row + dir[0]
neighboorCol = cur.col + dir[1]
if (neighboorRow >= 0 && neighboorRow < n_rows && neighboorCol >= 0 && neighboorCol < n_cols) {
neighboorCell = rows[neighboorRow][neighboorCol]
if (!neighboorCell.visited && !neighboorCell.wall) {
visitStack.push(neighboorCell)
}
}
})
await sleep(DELAY)
cur.changeColor()
drawMaze()
drawArray()
}
}
}
function draw() {
noLoop();
rows[0][0].wall = false
drawMaze()
findPathBFS()
//findPathDFS()
}