Skip to content

Commit 7cd46dc

Browse files
committed
Recursive backtracking working
1 parent df6a97d commit 7cd46dc

2 files changed

Lines changed: 33 additions & 22 deletions

File tree

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import {directions} from '../Directions';
2-
import {_FindCellVisitState, _FindCellTypeState, _FindCell, _BoardReset, _GetNeighbors, _SetWalls, _GetNeighborsCell, _BoardResetWalled} from '../Tools';
3-
import exit from 'exit';
4-
import {PATH_TYPE, DEFAULT_TYPE, FLOOR_TYPE, WALL_TYPE, START_TYPE, GOAL_TYPE} from '../../../../Utility/Colors';
5-
import {PriorityQueue} from '../../../../Utility/PriorityQueue.js';
1+
import {directions} from '../Directions';
2+
import {_FindCellVisitState, _FindCellTypeState, _FindCell, _BoardReset, _GetNeighbors, _SetWalls, _GetNeighborsCell, _BoardResetWalled, shuffle, _VisitReset} from '../Tools';
3+
import exit from 'exit';
4+
import {PATH_TYPE, DEFAULT_TYPE, FLOOR_TYPE, WALL_TYPE, START_TYPE, GOAL_TYPE} from '../../../../Utility/Colors';
5+
import {PriorityQueue} from '../../../../Utility/PriorityQueue.js';
66

77

88

@@ -19,11 +19,11 @@ export function RecursiveBacktrackCreation(board, start, end) {
1919
console.log('End: ' + endCell.id + ' (' + endCell.x + ' | ' + endCell.y + ')');
2020

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

25-
// _SetWalls(board);
26-
25+
_SetWalls(board);
26+
_VisitReset(board);
2727

2828
}
2929

@@ -34,7 +34,8 @@ function carvePath(board, cell, found) {
3434
console.log('No neighbors found for cell: ' + cell.id + ' (' + cell.x + ' | ' + cell.y + ')');
3535
return;
3636
}
37-
shuffle( neighbors);
37+
38+
shuffle(neighbors);
3839
for (let i = 0; i < neighbors.length; i++) {
3940
const neighbor = neighbors[i];
4041
if (!neighbor.visited) {
@@ -51,9 +52,8 @@ function carvePath(board, cell, found) {
5152
}
5253
}
5354
}
55+
return false;
5456
}
5557

5658

57-
function shuffle(array) {
58-
array.sort(() => Math.random() - 0.5);
59-
}
59+

‎src/components/Algorithms/Maze/Tools.js‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/*
22
* Quick selection test
33
*/
4-
import {FLOOR_TYPE, FLOOR_COLOR, WALL_TYPE, WALL_COLOR,
4+
import {
5+
FLOOR_TYPE, FLOOR_COLOR, WALL_TYPE, WALL_COLOR,
56
getColor, GOAL_TYPE, START_TYPE, SELECTED_COLOR,
67
BURN_COLOR, PATH_TYPE, PATH_COLOR, DEFAULT_COLOR,
7-
DEFAULT_TYPE} from '../../../Utility/Colors';
8-
import {directions} from './Directions';
8+
DEFAULT_TYPE,
9+
} from '../../../Utility/Colors';
10+
import {directions} from './Directions';
911

1012

1113

@@ -41,6 +43,7 @@ export function _FindCell(board, x, y) {
4143
return null;
4244
};
4345

46+
4447
export function _GetNeighborsUnvisited(board, cell) {
4548
let neighbors = [];
4649
for (let i = 0; i < directions.length; i++) {
@@ -56,8 +59,8 @@ export function _GetNeighborsUnvisited(board, cell) {
5659
export function _GetNeighbors(board, x, y) {
5760
let neighbors = [];
5861
for (let i = 0; i < 4; i++) {
59-
let xcoord = x + directions[i].x;
60-
let ycoord = y + directions[i].y;
62+
let xcoord = x + directions[i].x;
63+
let ycoord = y + directions[i].y;
6164
let neighbor = _FindCell(board, xcoord, ycoord);
6265
if (neighbor) {
6366
if (neighbor.type !== WALL_TYPE) {
@@ -68,39 +71,47 @@ export function _GetNeighbors(board, x, y) {
6871
return neighbors;
6972
}
7073

74+
7175
export function _GetNeighborsCell(board, cell) {
7276
let neighbors = [];
7377
for (let i = 0; i < 4; i++) {
74-
let xcoord = cell.x + directions[i].x;
75-
let ycoord = cell.y + directions[i].y;
78+
let xcoord = cell.x + directions[i].x;
79+
let ycoord = cell.y + directions[i].y;
7680
let neighbor = _FindCell(board, xcoord, ycoord);
7781
if (neighbor) {
78-
neighbors.push(neighbor);
82+
neighbors.push(neighbor);
7983

8084
}
8185
}
8286
return neighbors;
8387
}
8488

8589

90+
export function shuffle(array) {
91+
array.sort(() => Math.random() - 0.5);
92+
}
93+
94+
8695
export function _BoardReset(board) {
8796
for (let i = 0; i < board.length; i++) {
8897
board[i].visited = false;
89-
board[i].type = DEFAULT_TYPE;
98+
board[i].type = DEFAULT_TYPE;
9099
}
91100
}
92101

102+
93103
/*
94104
* Walled board reset with the intent of creating a maze
95105
* through the use of the recursive backtracking algorithm.
96106
*/
97107
export function _BoardResetWalled(board) {
98108
for (let i = 0; i < board.length; i++) {
99109
board[i].visited = false;
100-
board[i].type = WALL_TYPE;
110+
board[i].type = WALL_TYPE;
101111
}
102112
}
103113

114+
104115
export function _VisitReset(board) {
105116
for (let i = 0; i < board.length; i++) {
106117
board[i].visited = false;

0 commit comments

Comments
 (0)