@@ -16,61 +16,61 @@ public class AStar {
1616 private final Node [][] searchArea ;
1717 private final PriorityQueue <Node > openList ;
1818 private final Set <Node > closedSet ;
19- private Node initialNode ;
20- private Node finalNode ;
19+ private final Node initialNode ;
20+ private final Node finalNode ;
2121
2222 public AStar (int rows , int cols , Node initialNode , Node finalNode , int hvCost , int diagonalCost ) {
2323 this .hvCost = hvCost ;
2424 this .diagonalCost = diagonalCost ;
25- setInitialNode ( initialNode ) ;
26- setFinalNode ( finalNode ) ;
25+ this . initialNode = initialNode ;
26+ this . finalNode = finalNode ;
2727 this .searchArea = new Node [rows ][cols ];
28- this .openList = new PriorityQueue <>(Comparator .comparingInt (Node ::getF ));
29- setNodes ();
28+ this .openList = new PriorityQueue <>(Comparator .comparingInt (Node ::f ));
29+ initNodes ();
3030 this .closedSet = new HashSet <>();
3131 }
3232
3333 public AStar (int rows , int cols , Node initialNode , Node finalNode ) {
3434 this (rows , cols , initialNode , finalNode , DEFAULT_HV_COST , DEFAULT_DIAGONAL_COST );
3535 }
3636
37- private void setNodes () {
37+ private void initNodes () {
3838 for (int i = 0 ; i < searchArea .length ; i ++) {
3939 for (int j = 0 ; j < searchArea [0 ].length ; j ++) {
4040 Node node = new Node (i , j );
41- node .calculateHeuristic (getFinalNode () );
41+ node .calculateHeuristic (finalNode );
4242 this .searchArea [i ][j ] = node ;
4343 }
4444 }
4545 }
4646
47- public void setBlocks (int [][] blocksArray ) {
47+ public void initBlocks (int [][] blocksArray ) {
4848 for (int [] ints : blocksArray ) {
4949 int row = ints [0 ];
5050 int col = ints [1 ];
51- setBlock ( row , col );
51+ this . searchArea [ row ][ col ]. setAsBlocked ( );
5252 }
5353 }
5454
5555 public List <Node > findPath () {
5656 openList .add (initialNode );
57- while (!isEmpty (openList )) {
57+ while (!openList . isEmpty ()) {
5858 Node currentNode = openList .poll ();
5959 closedSet .add (currentNode );
6060 if (isFinalNode (currentNode )) {
61- return getPath (currentNode );
61+ return generatePath (currentNode );
6262 } else {
6363 addAdjacentNodes (currentNode );
6464 }
6565 }
6666 return new ArrayList <>();
6767 }
6868
69- private List <Node > getPath (Node currentNode ) {
69+ private List <Node > generatePath (Node currentNode ) {
7070 List <Node > path = new ArrayList <>();
7171 path .add (currentNode );
7272 Node parent ;
73- while ((parent = currentNode .getParent ()) != null ) {
73+ while ((parent = currentNode .parent ()) != null ) {
7474 path .add (0 , parent );
7575 currentNode = parent ;
7676 }
@@ -84,60 +84,63 @@ private void addAdjacentNodes(Node currentNode) {
8484 }
8585
8686 private void addAdjacentLowerRow (Node currentNode ) {
87- int row = currentNode .getRow ();
88- int col = currentNode .getCol ();
87+ int row = currentNode .row ();
88+ int col = currentNode .col ();
8989 int lowerRow = row + 1 ;
90- if (lowerRow < getSearchArea ().length ) {
91- if (col - 1 >= 0 ) {
92- checkNode (currentNode , col - 1 , lowerRow , getDiagonalCost ()); // Comment this line if diagonal movements are not allowed
93- }
94- if (col + 1 < getSearchArea ()[0 ].length ) {
95- checkNode (currentNode , col + 1 , lowerRow , getDiagonalCost ()); // Comment this line if diagonal movements are not allowed
96- }
97- checkNode (currentNode , col , lowerRow , getHvCost ());
90+ if (lowerRow >= searchArea .length ) {
91+ return ;
9892 }
93+ if (col - 1 >= 0 ) {
94+ checkNode (currentNode , col - 1 , lowerRow , diagonalCost ); // Comment this line if diagonal movements are not allowed
95+ }
96+ if (col + 1 < searchArea [0 ].length ) {
97+ checkNode (currentNode , col + 1 , lowerRow , diagonalCost ); // Comment this line if diagonal movements are not allowed
98+ }
99+ checkNode (currentNode , col , lowerRow , hvCost );
99100 }
100101
101102 private void addAdjacentMiddleRow (Node currentNode ) {
102- int row = currentNode .getRow ();
103- int col = currentNode .getCol ();
103+ int row = currentNode .row ();
104+ int col = currentNode .col ();
104105 if (col - 1 >= 0 ) {
105- checkNode (currentNode , col - 1 , row , getHvCost () );
106+ checkNode (currentNode , col - 1 , row , hvCost );
106107 }
107- if (col + 1 < getSearchArea () [0 ].length ) {
108- checkNode (currentNode , col + 1 , row , getHvCost () );
108+ if (col + 1 < searchArea [0 ].length ) {
109+ checkNode (currentNode , col + 1 , row , hvCost );
109110 }
110111 }
111112
112113 private void addAdjacentUpperRow (Node currentNode ) {
113- int row = currentNode .getRow ();
114- int col = currentNode .getCol ();
114+ int row = currentNode .row ();
115+ int col = currentNode .col ();
115116 int upperRow = row - 1 ;
116- if (upperRow >= 0 ) {
117- if (col - 1 >= 0 ) {
118- checkNode (currentNode , col - 1 , upperRow , getDiagonalCost ()); // Comment this if diagonal movements are not allowed
119- }
120- if (col + 1 < getSearchArea ()[0 ].length ) {
121- checkNode (currentNode , col + 1 , upperRow , getDiagonalCost ()); // Comment this if diagonal movements are not allowed
122- }
123- checkNode (currentNode , col , upperRow , getHvCost ());
117+ if (upperRow < 0 ) {
118+ return ;
124119 }
120+ if (col - 1 >= 0 ) {
121+ checkNode (currentNode , col - 1 , upperRow , diagonalCost ); // Comment this if diagonal movements are not allowed
122+ }
123+ if (col + 1 < searchArea [0 ].length ) {
124+ checkNode (currentNode , col + 1 , upperRow , diagonalCost ); // Comment this if diagonal movements are not allowed
125+ }
126+ checkNode (currentNode , col , upperRow , hvCost );
125127 }
126128
127129 private void checkNode (Node currentNode , int col , int row , int cost ) {
128- Node adjacentNode = getSearchArea ()[row ][col ];
129- if (!adjacentNode .isBlock () && !getClosedSet ().contains (adjacentNode )) {
130- if (!getOpenList ().contains (adjacentNode )) {
131- adjacentNode .setNodeData (currentNode , cost );
132- getOpenList ().add (adjacentNode );
133- } else {
134- boolean changed = adjacentNode .checkBetterPath (currentNode , cost );
135- if (changed ) {
136- // Remove and Add the changed node, so that the PriorityQueue can sort again its
137- // contents with the modified "finalCost" value of the modified node
138- getOpenList ().remove (adjacentNode );
139- getOpenList ().add (adjacentNode );
140- }
130+ Node adjacentNode = searchArea [row ][col ];
131+ if (adjacentNode .isBlocked () || closedSet .contains (adjacentNode )) {
132+ return ;
133+ }
134+ if (!openList .contains (adjacentNode )) {
135+ adjacentNode .setNodeData (currentNode , cost );
136+ openList .add (adjacentNode );
137+ } else {
138+ boolean changed = adjacentNode .checkBetterPath (currentNode , cost );
139+ if (changed ) {
140+ // Remove and Add the changed node, so that the PriorityQueue can sort again its
141+ // contents with the modified "finalCost" value of the modified node
142+ openList .remove (adjacentNode );
143+ openList .add (adjacentNode );
141144 }
142145 }
143146 }
@@ -146,45 +149,5 @@ private boolean isFinalNode(Node currentNode) {
146149 return currentNode .equals (finalNode );
147150 }
148151
149- private boolean isEmpty (PriorityQueue <Node > openList ) {
150- return openList .isEmpty ();
151- }
152-
153- private void setBlock (int row , int col ) {
154- this .searchArea [row ][col ].setBlock (true );
155- }
156-
157- public void setInitialNode (Node initialNode ) {
158- this .initialNode = initialNode ;
159- }
160-
161- public Node getFinalNode () {
162- return finalNode ;
163- }
164-
165- public void setFinalNode (Node finalNode ) {
166- this .finalNode = finalNode ;
167- }
168-
169- public Node [][] getSearchArea () {
170- return searchArea ;
171- }
172-
173- public PriorityQueue <Node > getOpenList () {
174- return openList ;
175- }
176-
177- public Set <Node > getClosedSet () {
178- return closedSet ;
179- }
180-
181- public int getHvCost () {
182- return hvCost ;
183- }
184-
185- private int getDiagonalCost () {
186- return diagonalCost ;
187- }
188-
189152}
190153
0 commit comments