Skip to content

Commit e67b341

Browse files
committed
Recursive backtracking somewhat working
1 parent e6c01c3 commit e67b341

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,20 @@ export function RecursiveBacktrackCreation(board, start, end) {
1414
if (start === undefined || start === null) {startCell = board[Math.floor(Math.random() * board.length)];}
1515
if (end === undefined || end === null) {endCell = board[Math.floor(Math.random() * board.length)];}
1616
startCell.type = START_TYPE;
17-
startCell.visited = true;
1817
endCell.type = GOAL_TYPE;
1918
console.log('Start: ' + startCell.id + ' (' + startCell.x + ' | ' + startCell.y + ')');
2019
console.log('End: ' + endCell.id + ' (' + endCell.x + ' | ' + endCell.y + ')');
2120

22-
21+
startCell.visited = true;
2322
carvePath(board, startCell, false);
2423

25-
_SetWalls(board);
24+
// _SetWalls(board);
2625

2726

2827
}
2928

3029

3130
function carvePath(board, cell, found) {
32-
if (found) {
33-
return;
34-
}
3531
const neighbors = _GetNeighborsCell(board, cell);
3632
if (neighbors.length === 0) {
3733
console.log('No neighbors found for cell: ' + cell.id + ' (' + cell.x + ' | ' + cell.y + ')');
@@ -41,12 +37,16 @@ function carvePath(board, cell, found) {
4137
const neighbor = neighbors[Math.floor(Math.random() * neighbors.length)];
4238
if (!neighbor.visited) {
4339
if (neighbor.type === GOAL_TYPE || found) {
40+
console.log('Found goal');
4441
found = true;
45-
return;
42+
return found;
4643
}
4744
neighbor.visited = true;
4845
neighbor.type = FLOOR_TYPE;
49-
return carvePath(board, neighbor, found);
46+
found = carvePath(board, neighbor, found);
47+
if (found) {
48+
return found;
49+
}
5050
}
5151
}
5252
}

0 commit comments

Comments
 (0)