I have a 2-D array filled with objects. I want to convert this array into a maze where some objects are walls (they have a isWall property that should be marked true) and some objects are passageways (isWall will be false).
I'm trying to use Recursive Backtracking (I'm using a stack as opposed to recursion) for this, but my Maze is showing up like this

The black squares represent walls whereas the white squares represent passageways. The numbers inside the white squares are irrelevant (just weights I use for Djikstra).
Here is my implementation of Recursive Backtracking in JavaScript.
JavaScript
function isValid(r, c, grid) {
if (r < 0 || c < 0) {
return false;
}
if (r < grid.length && c < grid[r].length) {
return true;
}
return false;
}
export default function recursiveBacktracker(grid, startNode, endNode) {
// Fill Grid with walls
for (let r = 0; r < grid.length; r++) {
for (let c = 0; c < grid[r].length; c++) {
grid[r][c].isWall = true;
}
}
let visited = new Set();
let stack = [startNode];
let neighbors = [
[2, 0],
[-2, 0],
[0, 2],
[0, -2]
];
while (stack.length > 0) {
let cur = stack.pop();
let r = cur[0],
c = cur[1];
console.log(r, c);
visited.add(r + "." + c);
grid[r][c].isWall = false;
for (let n of neighbors) {
let rr = r + n[0];
let cc = c + n[1];
if (isValid(rr, cc, grid) && !visited.has(rr + "." + cc)) {
grid[r + n[0] / 2][c + n[0] / 2].isWall = false;
stack.push([rr, cc]);
break;
}
}
}
return grid;
}
I'd really appreciate some guidance on where my code is going wrong. Thank you!