Skip to content

Commit df6a97d

Browse files
committed
Recursive backtracking working
1 parent 46cdfad commit df6a97d

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

‎src/components/Algorithms/Maze/Generation/RecursiveBacktrackCreation.js‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,24 @@ export function RecursiveBacktrackCreation(board, start, end) {
1919
console.log('End: ' + endCell.id + ' (' + endCell.x + ' | ' + endCell.y + ')');
2020

2121
startCell.visited = true;
22-
carvePath(board, startCell);
22+
var found = false;
23+
carvePath(board, startCell, found);
2324

2425
// _SetWalls(board);
2526

2627

2728
}
2829

2930

30-
function carvePath(board, cell) {
31-
let found = false;
31+
function carvePath(board, cell, found) {
3232
const neighbors = _GetNeighborsCell(board, cell);
3333
if (neighbors.length === 0) {
3434
console.log('No neighbors found for cell: ' + cell.id + ' (' + cell.x + ' | ' + cell.y + ')');
3535
return;
3636
}
37+
shuffle( neighbors);
3738
for (let i = 0; i < neighbors.length; i++) {
38-
const neighbor = neighbors[Math.floor(Math.random() * neighbors.length)];
39+
const neighbor = neighbors[i];
3940
if (!neighbor.visited) {
4041
if (neighbor.type === GOAL_TYPE || found) {
4142
console.log('Found goal');
@@ -44,10 +45,15 @@ function carvePath(board, cell) {
4445
}
4546
neighbor.visited = true;
4647
neighbor.type = FLOOR_TYPE;
47-
found = carvePath(board, neighbor);
48+
found = carvePath(board, neighbor, found);
4849
if (found) {
4950
return found;
5051
}
5152
}
5253
}
53-
}
54+
}
55+
56+
57+
function shuffle(array) {
58+
array.sort(() => Math.random() - 0.5);
59+
}

0 commit comments

Comments
 (0)