Repository navigation
Expand file tree
/
Copy pathTools.js
More file actions
131 lines (102 loc) · 2.99 KB
/
Copy pathTools.js
File metadata and controls
131 lines (102 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Quick selection test
*/
import {
FLOOR_TYPE, FLOOR_COLOR, WALL_TYPE, WALL_COLOR,
getColor, GOAL_TYPE, START_TYPE, SELECTED_COLOR,
BURN_COLOR, PATH_TYPE, PATH_COLOR, DEFAULT_COLOR,
DEFAULT_TYPE,
} from '../../../Utility/Colors';
import {directions} from './Directions';
export const _FindCellVisitState = (board, x, y) => {
for (let i = 0; i < board.length; i++) {
if (Math.floor(board[i].x) === Math.floor(x) && Math.floor(board[i].y) === Math.floor(y)) {
return board[i].visited;
}
}
};
export const _FindCellTypeState = (board, x, y) => {
for (let i = 0; i < board.length; i++) {
if (Math.floor(board[i].x) === Math.floor(x) && Math.floor(board[i].y) === Math.floor(y)) {
return board[i].visited;
}
}
};
export function _FindCell(board, x, y) {
for (let i = 0; i < board.length; i++) {
// console.log(board[i].x, x, board[i].y, y);
if (board[i].x === x && board[i].y === y) {
// console.log("Board x: " + board[i].x + "\nCelll x: " + x + "\nBoard y: "+ board[i].y + "\nCell y: " + y);
return board[i];
}
}
return null;
};
export function _GetNeighborsUnvisited(board, cell) {
let neighbors = [];
for (let i = 0; i < directions.length; i++) {
let neighbor = _FindCell(board, cell.x + directions[i].x, cell.y + directions[i].y);
if (neighbor !== null && neighbor.visited === false && neighbor.type !== WALL_TYPE) {
neighbors.push(neighbor);
}
}
return neighbors;
}
export function _GetNeighbors(board, x, y) {
let neighbors = [];
for (let i = 0; i < 4; i++) {
let xcoord = x + directions[i].x;
let ycoord = y + directions[i].y;
let neighbor = _FindCell(board, xcoord, ycoord);
if (neighbor) {
if (neighbor.type !== WALL_TYPE) {
neighbors.push(neighbor);
}
}
}
return neighbors;
}
export function _GetNeighborsCell(board, cell) {
let neighbors = [];
for (let i = 0; i < 4; i++) {
let xcoord = cell.x + directions[i].x;
let ycoord = cell.y + directions[i].y;
let neighbor = _FindCell(board, xcoord, ycoord);
if (neighbor) {
neighbors.push(neighbor);
}
}
return neighbors;
}
export function shuffle(array) {
array.sort(() => Math.random() - 0.5);
}
export function _BoardReset(board) {
for (let i = 0; i < board.length; i++) {
board[i].visited = false;
board[i].type = DEFAULT_TYPE;
}
}
/*
* Walled board reset with the intent of creating a maze
* through the use of the recursive backtracking algorithm.
*/
export function _BoardResetWalled(board) {
for (let i = 0; i < board.length; i++) {
board[i].visited = false;
board[i].type = WALL_TYPE;
}
}
export function _VisitReset(board) {
for (let i = 0; i < board.length; i++) {
board[i].visited = false;
}
}
export function _SetWalls(board) {
for (var x = 0; x < board.length; x++) {
let node = board[x];
if (!node.visited && node.type !== GOAL_TYPE && node.type !== START_TYPE) {
node.type = WALL_TYPE;
}
}
}