-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.js
More file actions
227 lines (182 loc) · 5.52 KB
/
grid.js
File metadata and controls
227 lines (182 loc) · 5.52 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { createPath, addConsole} from "./board.js";
var debugMsg = 0;
var allowDiag = 0;
//TODO: all diagonal functionality.
/**
* Grid Memory structure
* @param {Array} array Array that is converted into a grid
* @param {bool} debug Debug messages on or off
* @param {bool} diagonal Allow diagonal moves
*
* Example 2x2 grid
* y[(0,0), (1,0)]
* y[(0,1), (1,1)]
* x x
* Where (x,y)
*/
export class Grid {
//constructor
constructor(array, debug) {
this.grid = [];
// debugMsg = debug;
if (debugMsg) console.log(`debug messages enabled`);
//creates a GridNode for each array element
for (let y = 0; y < array.length; y++) {
let row = [];
for (let x = 0; x < array[y].length; x++) {
let node = new GridNode(x, y, array[y][x]);
row.push(node);
//assigns the start and end nodes
if (array[y][x] == 2) {
this.start = node;
} else if (array[y][x] == 3) {
this.end = node;
}
}
this.grid.push(row);
}
if (debugMsg) console.log(this.start.toString(1));
}
//Prints Grid
toString() {
for (let i = 0; i < this.grid.length; i++) {
let output = ``;
for (let j = 0; j < this.grid[i].length; j++) {
output += this.grid[i][j].toString(0) + ``;
}
}
}
/**
* List of Neighbours of node. Diagonal nodes are added if allowDiag is true.
* @param {GridNode} node Node to get neighbours of
* @param {bool} allowDiag Allow diagonal nodes
* @returns list of neighbours of node.
*/
getNeighbours(node, allowDiag) {
let neighbours = [];
let x = node.x;
let y = node.y;
let grid = this.grid;
//loops through immediate neighbours of the node
//adds to list if neighbour exists.
if (allowDiag) {
for (let row = -1; row <= 1; row++) {
for (let col = -1; col <= 1; col++) {
if (grid[y + row] && grid[y + row][x + col] && !(row == 0 && col == 0)) {
neighbours.push(grid[y + row][x + col]);
}
}
}
} else { //just get North South East West nodes
//North node
if (grid[y - 1] && grid[y - 1][x]) {
neighbours.push(grid[y - 1][x]);
}
//East node
if (grid[y][x + 1]) {
neighbours.push(grid[y][x + 1]);
}
//South node
if (grid[y + 1] && grid[y + 1][x]) {
neighbours.push(grid[y + 1][x]);
}
//West node
if (grid[y][x - 1]) {
neighbours.push(grid[y][x - 1]);
}
}
if (debugMsg) {
let output = `Neighbours: `;
for (let i = 0; i < neighbours.length; i++) {
output += neighbours[i].toString(1);
}
console.log(output);
}
return neighbours;
}
getHCost(node) {
let d1 = Math.abs(this.end.x - node.x);
let d2 = Math.abs(this.end.y - node.y);
return d1 + d2;
}
}
/**
* Singular Grid Nodes
* @param {number} x x coordinate
* @param {number} y y coordinate
* @param {number} isWall 0 = path, 1 = wall, 2 = start, 3 = end
*
* g cost = distance from starting node
* h cost = distance from end node
* f cost = g cost + h cost
*/
class GridNode {
//constructor
constructor(x, y, isWall) {
this.x = x;
this.y = y;
//Walls are 1, others are walkable, including 2,3 which are start,end
if (isWall == 1) {
this.isWall = true;
this.weight = 0;
} else {
this.isWall = false;
this.weight = 1;
}
this.f = 0;
this.h = 0;
this.g = 0;
this.visited = false;
this.closed = false;
this.parent = null;
if (debugMsg) console.log(`New GridNode Created`);
}
/**
* prints out singular Node as string
* @param {bool} coord include coordinates
*
* @returns {`[visited, isWall, isClosed, fCost, weight]`} if coord = 0
* @returns {`[(x,y), visited, isWall, isClosed, fCost, weight]`} if coord = 1
*/
toString(coord) {
let output = ``;
let params = [this.visited, this.isWall, this.closed];
if (coord) {
output = `(${this.x},${this.y}),`
}
for (let i = 0; i < params.length; i++) {
if (params[i]) {
output += `t,`
} else {
output += `f,`
}
}
output += `${this.f.toFixed(1)},${this.weight.toFixed(1)}]`;
return `[` + output;
}
}
/**
* Returns the path from end to start.
* @param {GridNode} node
* @returns Path of nodes from end to start
*/
export function printPath(node, algorithm, calls, runtime) {
let curr = node;
let path = [];
path.push(curr);
while (curr.parent != null) {
path.push(curr.parent);
curr = curr.parent;
}
addConsole(algorithm, calls, runtime, path.length);
createPath(path);
return path;
}
/**
* just adds a delay for set amount of time
* @param {int} seconds in miliseconds
* @returns
*/
export async function sleep(seconds) {
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
}