function generate(ctx, startX, startY) {
const visited = create2dArray(numCells, false);
function shuffled(array){
const copy = Array.from(array);
for (let i = 0; i < copy.length; i++) {
const randIdx = i + Math.floor(Math.random() * copy.length - i);
const temp = copy[randIdx];
copy[randIdx] = copy[i];
copy[i] = temp;
}
return copy;
}
function validCell(x,y) {
return x >= 0 && x < numCells && y >= 0 && y < numCells && !visited[y][x]
}
function getNghbrs(x, y) {
return [[x - 1, y], [x + 1, y], [x, y - 1], [x, y + 1]];
}
function dfs(ctx, startX, startY) {
visited[startY][startX] = true;
for (const [x,y] of shuffled(getNghbrs(startX, startY).filter(([x,y]) => validCell(x,y)))) {
if(validCell(x,y)) {
drawEdge(ctx, startX, startY, x, y, "white");
dfs(ctx, x, y);
}
}
}
dfs(ctx, startX, startY)
}