Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0e1038b

Browse files
committed
--fix : code coverage 100%
1 parent 8fb5183 commit 0e1038b

File tree

2 files changed

+134
-36
lines changed

2 files changed

+134
-36
lines changed

‎src/_PathFinder_/AStar/AStar.test.js‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,76 @@ describe('A*', () => {
119119
expect(() => { AStar(start, end, ROW, COL, inputGrid); }).toThrowError('Error: Endpoint is unreachable');
120120
});
121121
});
122+
describe('Completes grid successfully when no block', () => {
123+
it('A*', () => {
124+
const inputGrid = [
125+
[1, 1, 1, 1, 1],
126+
[1, 1, 1, 1, 1],
127+
[1, 1, 1, 1, 1],
128+
[1, 1, 1, 1, 1],
129+
[1, 1, 1, 1, 1],
130+
];
131+
const ROW = inputGrid.length;
132+
const COL = inputGrid[0].length;
133+
const start = {
134+
i: 2,
135+
j: 2,
136+
};
137+
const end1 = {
138+
i: 0,
139+
j: 0,
140+
};
141+
const completedPath1 = [[0, 0], [1, 1], [2, 2]];
142+
expect(AStar(start, end1, ROW, COL, inputGrid)).toEqual(completedPath1);
143+
144+
const end2 = {
145+
i: 0,
146+
j: 2,
147+
};
148+
const completedPath2 = [[0, 2], [1, 2], [2, 2]];
149+
expect(AStar(start, end2, ROW, COL, inputGrid)).toEqual(completedPath2);
150+
151+
const end3 = {
152+
i: 0,
153+
j: 4,
154+
};
155+
const completedPath3 = [[0, 4], [1, 3], [2, 2]];
156+
expect(AStar(start, end3, ROW, COL, inputGrid)).toEqual(completedPath3);
157+
158+
const end4 = {
159+
i: 2,
160+
j: 4,
161+
};
162+
const completedPath4 = [[2, 4], [2, 3], [2, 2]];
163+
expect(AStar(start, end4, ROW, COL, inputGrid)).toEqual(completedPath4);
164+
165+
const end5 = {
166+
i: 4,
167+
j: 4,
168+
};
169+
const completedPath5 = [[4, 4], [3, 3], [2, 2]];
170+
expect(AStar(start, end5, ROW, COL, inputGrid)).toEqual(completedPath5);
171+
172+
const end6 = {
173+
i: 4,
174+
j: 2,
175+
};
176+
const completedPath6 = [[4, 2], [3, 2], [2, 2]];
177+
expect(AStar(start, end6, ROW, COL, inputGrid)).toEqual(completedPath6);
178+
179+
const end7 = {
180+
i: 4,
181+
j: 0,
182+
};
183+
const completedPath7 = [[4, 0], [3, 1], [2, 2]];
184+
expect(AStar(start, end7, ROW, COL, inputGrid)).toEqual(completedPath7);
185+
186+
const end8 = {
187+
i: 2,
188+
j: 0,
189+
};
190+
const completedPath8 = [[2, 0], [2, 1], [2, 2]];
191+
expect(AStar(start, end8, ROW, COL, inputGrid)).toEqual(completedPath8);
192+
});
193+
});
122194
});

‎src/_PathFinder_/AStar/index.js‎

Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ function AStar(s, e, row, col, inputGrid) {
1111
const end = e;
1212
const path = [];
1313

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)) {
1518
throw new Error('Error: Endpoint outside grid bounds');
1619
}
1720

@@ -44,9 +47,6 @@ function AStar(s, e, row, col, inputGrid) {
4447
}
4548
}
4649

47-
48-
const isValid = (i, j) => i >= 0 && j >= 0 && i < Row && j < Col;
49-
5050
const isDestination = (i, j) => end.i === i && end.j === j;
5151

5252
const isBlocked = (i, j) => grid[i][j].cellValue === 0;
@@ -124,10 +124,6 @@ function AStar(s, e, row, col, inputGrid) {
124124
};
125125

126126
const search = () => {
127-
if (!isValid(start.i, start.j) || !isValid(end.i, end.j)) {
128-
return false;
129-
}
130-
131127
let i = start.i;
132128
let j = start.j;
133129
const openList = [];
@@ -185,36 +181,66 @@ function AStar(s, e, row, col, inputGrid) {
185181

186182

187183
// 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],
197189
// ];
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));
198243

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);
218244

219245

220246
module.exports = {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /