@@ -11,7 +11,10 @@ function AStar(s, e, row, col, inputGrid) {
11
11
const end = e ;
12
12
const path = [ ] ;
13
13
14
- if ( end . i >= inputGrid . length || end . j >= inputGrid [ 0 ] . length ) {
14
+ const isValid = ( i , j ) => i >= 0 && j >= 0 && i < Row && j < Col ;
15
+
16
+
17
+ if ( ! isValid ( start . i , start . j ) || ! isValid ( end . i , end . j ) ) {
15
18
throw new Error ( 'Error: Endpoint outside grid bounds' ) ;
16
19
}
17
20
@@ -44,9 +47,6 @@ function AStar(s, e, row, col, inputGrid) {
44
47
}
45
48
}
46
49
47
-
48
- const isValid = ( i , j ) => i >= 0 && j >= 0 && i < Row && j < Col ;
49
-
50
50
const isDestination = ( i , j ) => end . i === i && end . j === j ;
51
51
52
52
const isBlocked = ( i , j ) => grid [ i ] [ j ] . cellValue === 0 ;
@@ -124,10 +124,6 @@ function AStar(s, e, row, col, inputGrid) {
124
124
} ;
125
125
126
126
const search = ( ) => {
127
- if ( ! isValid ( start . i , start . j ) || ! isValid ( end . i , end . j ) ) {
128
- return false ;
129
- }
130
-
131
127
let i = start . i ;
132
128
let j = start . j ;
133
129
const openList = [ ] ;
@@ -185,36 +181,66 @@ function AStar(s, e, row, col, inputGrid) {
185
181
186
182
187
183
// const inputGrid = [
188
- // [1, 0, 1, 1, 1, 1, 0, 1, 1, 1],
189
- // [1, 1, 1, 0, 1, 1, 1, 0, 1, 1],
190
- // [1, 1, 1, 0, 1, 1, 0, 1, 0, 1],
191
- // [0, 0, 1, 0, 1, 0, 0, 0, 0, 1],
192
- // [1, 1, 1, 0, 1, 1, 1, 0, 1, 0],
193
- // [1, 0, 1, 1, 1, 1, 0, 1, 0, 0],
194
- // [1, 0, 0, 0, 0, 1, 0, 0, 0, 1],
195
- // [1, 0, 1, 1, 1, 1, 0, 1, 1, 1],
196
- // [1, 1, 1, 0, 0, 0, 1, 0, 0, 1],
184
+ // [1, 1, 1, 1, 1],
185
+ // [1, 1, 1, 1, 1],
186
+ // [1, 1, 1, 1, 1],
187
+ // [1, 1, 1, 1, 1],
188
+ // [1, 1, 1, 1, 1],
197
189
// ];
190
+ // const ROW = inputGrid.length;
191
+ // const COL = inputGrid[0].length;
192
+ // const start = {
193
+ // i: 2,
194
+ // j: 2,
195
+ // };
196
+ // const end1 = {
197
+ // i: 0,
198
+ // j: 0,
199
+ // };
200
+ // console.log(AStar(start, end1, ROW, COL, inputGrid));
201
+
202
+ // const end2 = {
203
+ // i: 0,
204
+ // j: 2,
205
+ // };
206
+ // console.log(AStar(start, end2, ROW, COL, inputGrid));
207
+
208
+ // const end3 = {
209
+ // i: 0,
210
+ // j: 4,
211
+ // };
212
+ // console.log(AStar(start, end3, ROW, COL, inputGrid));
213
+
214
+ // const end4 = {
215
+ // i: 2,
216
+ // j: 4,
217
+ // };
218
+ // console.log(AStar(start, end4, ROW, COL, inputGrid));
219
+
220
+ // const end5 = {
221
+ // i: 4,
222
+ // j: 4,
223
+ // };
224
+ // console.log(AStar(start, end5, ROW, COL, inputGrid));
225
+
226
+ // const end6 = {
227
+ // i: 4,
228
+ // j: 2,
229
+ // };
230
+ // console.log(AStar(start, end6, ROW, COL, inputGrid));
231
+
232
+ // const end7 = {
233
+ // i: 4,
234
+ // j: 0,
235
+ // };
236
+ // console.log(AStar(start, end7, ROW, COL, inputGrid));
237
+
238
+ // const end8 = {
239
+ // i: 2,
240
+ // j: 0,
241
+ // };
242
+ // console.log(AStar(start, end8, ROW, COL, inputGrid));
198
243
199
- const inputGrid = [
200
- [ 1 , 0 , 0 , 0 , 0 , 0 ] ,
201
- [ 1 , 1 , 1 , 1 , 1 , 1 ] ,
202
- [ 1 , 0 , 0 , 0 , 0 , 0 ] ,
203
- [ 1 , 1 , 1 , 1 , 1 , 1 ] ,
204
- ] ;
205
-
206
- const ROW = inputGrid . length ;
207
- const COL = inputGrid [ 0 ] . length ;
208
- const start = {
209
- i : 0 ,
210
- j : 0 ,
211
- } ;
212
- const end = {
213
- i : 3 ,
214
- j : 5 ,
215
- } ;
216
-
217
- AStar ( start , end , ROW , COL , inputGrid ) ;
218
244
219
245
220
246
module . exports = {
0 commit comments