@@ -4,56 +4,55 @@ import { minQueue } from '../utils/collections';
44import type { AlgorithmOptions , Node } from '$lib/types' ;
55
66const getHeuristic = ( node : Node , endNode : Node ) => {
7- return Math . abs ( endNode . row - node . row ) + Math . abs ( endNode . col - node . col ) ;
7+ return Math . abs ( endNode . row - node . row ) + Math . abs ( endNode . col - node . col ) ;
88} ;
99
1010export const aStar = async ( {
11- startNode,
12- endNode,
13- isWall,
14- isEndNode,
15- getNode,
16- getWeight,
17- screen,
18- hitBoundary,
19- intercept
11+ startNode,
12+ endNode,
13+ isWall,
14+ isEndNode,
15+ getNode,
16+ getWeight,
17+ screen,
18+ intercept
2019} : AlgorithmOptions ) => {
21- const q = minQueue ( ) ;
20+ const q = minQueue ( ) ;
2221
23- q . enqueue ( { node : startNode , weight : getHeuristic ( startNode , endNode ) } ) ;
22+ q . enqueue ( { node : startNode , weight : getHeuristic ( startNode , endNode ) } ) ;
2423
25- while ( ! q . isEmpty ( ) ) {
26- const current = q . dequeue ( ) ;
27- if ( ! current ) {
28- return ;
29- }
24+ while ( ! q . isEmpty ( ) ) {
25+ const current = q . dequeue ( ) ;
26+ if ( ! current ) {
27+ return ;
28+ }
3029
31- const { node : currentNode } = current ;
30+ const { node : currentNode } = current ;
3231
33- if ( isEndNode ( currentNode ) ) {
34- return ;
35- }
32+ if ( isEndNode ( currentNode ) ) {
33+ return ;
34+ }
3635
37- await intercept ( ) ;
36+ await intercept ( ) ;
3837
39- const neibhours = getGridNeibhours ( currentNode , screen , getNode , hitBoundary ) ;
38+ const neibhours = getGridNeibhours ( currentNode , screen , getNode ) ;
4039
41- for ( let nextNode of neibhours ) {
42- if ( ! nextNode || nextNode . visited ) {
43- continue ;
44- }
40+ for ( let nextNode of neibhours ) {
41+ if ( ! nextNode || nextNode . visited ) {
42+ continue ;
43+ }
4544
46- if ( isWall ( nextNode ) ) {
47- grid . visitNode ( nextNode ) ;
48- } else {
49- const neibhourWeight = getWeight ( nextNode ) + getHeuristic ( nextNode , endNode ) + 1 ;
45+ if ( isWall ( nextNode ) ) {
46+ grid . visitNode ( nextNode ) ;
47+ } else {
48+ const neibhourWeight = getWeight ( nextNode ) + getHeuristic ( nextNode , endNode ) + 1 ;
5049
51- grid . visitNode ( nextNode , currentNode ) ;
50+ grid . visitNode ( nextNode , currentNode ) ;
5251
53- q . enqueue ( { node : getNode ( nextNode ) , weight : neibhourWeight } ) ;
54- }
55- }
56- }
52+ q . enqueue ( { node : getNode ( nextNode ) , weight : neibhourWeight } ) ;
53+ }
54+ }
55+ }
5756
58- return ;
57+ return ;
5958} ;
0 commit comments