99 * @version 2.1, 2017-02-23
1010 */
1111public class AStar {
12- private static int DEFAULT_HV_COST = 10 ; // Horizontal - Vertical Cost
13- private static int DEFAULT_DIAGONAL_COST = 14 ;
14- private int hvCost ;
15- private int diagonalCost ;
16- private Node [][] searchArea ;
17- private PriorityQueue <Node > openList ;
18- private Set <Node > closedSet ;
12+ private static final int DEFAULT_HV_COST = 10 ; // Horizontal - Vertical Cost
13+ private static final int DEFAULT_DIAGONAL_COST = 14 ;
14+ private final int hvCost ;
15+ private final int diagonalCost ;
16+ private final Node [][] searchArea ;
17+ private final PriorityQueue <Node > openList ;
18+ private final Set <Node > closedSet ;
1919 private Node initialNode ;
2020 private Node finalNode ;
2121
@@ -25,12 +25,7 @@ public AStar(int rows, int cols, Node initialNode, Node finalNode, int hvCost, i
2525 setInitialNode (initialNode );
2626 setFinalNode (finalNode );
2727 this .searchArea = new Node [rows ][cols ];
28- this .openList = new PriorityQueue <Node >(new Comparator <Node >() {
29- @ Override
30- public int compare (Node node0 , Node node1 ) {
31- return Integer .compare (node0 .getF (), node1 .getF ());
32- }
33- });
28+ this .openList = new PriorityQueue <>(Comparator .comparingInt (Node ::getF ));
3429 setNodes ();
3530 this .closedSet = new HashSet <>();
3631 }
@@ -50,9 +45,9 @@ private void setNodes() {
5045 }
5146
5247 public void setBlocks (int [][] blocksArray ) {
53- for (int i = 0 ; i < blocksArray . length ; i ++ ) {
54- int row = blocksArray [ i ] [0 ];
55- int col = blocksArray [ i ] [1 ];
48+ for (int [] ints : blocksArray ) {
49+ int row = ints [0 ];
50+ int col = ints [1 ];
5651 setBlock (row , col );
5752 }
5853 }
@@ -68,11 +63,11 @@ public List<Node> findPath() {
6863 addAdjacentNodes (currentNode );
6964 }
7065 }
71- return new ArrayList <Node >();
66+ return new ArrayList <>();
7267 }
7368
7469 private List <Node > getPath (Node currentNode ) {
75- List <Node > path = new ArrayList <Node >();
70+ List <Node > path = new ArrayList <>();
7671 path .add (currentNode );
7772 Node parent ;
7873 while ((parent = currentNode .getParent ()) != null ) {
@@ -106,12 +101,11 @@ private void addAdjacentLowerRow(Node currentNode) {
106101 private void addAdjacentMiddleRow (Node currentNode ) {
107102 int row = currentNode .getRow ();
108103 int col = currentNode .getCol ();
109- int middleRow = row ;
110104 if (col - 1 >= 0 ) {
111- checkNode (currentNode , col - 1 , middleRow , getHvCost ());
105+ checkNode (currentNode , col - 1 , row , getHvCost ());
112106 }
113107 if (col + 1 < getSearchArea ()[0 ].length ) {
114- checkNode (currentNode , col + 1 , middleRow , getHvCost ());
108+ checkNode (currentNode , col + 1 , row , getHvCost ());
115109 }
116110 }
117111
@@ -153,17 +147,13 @@ private boolean isFinalNode(Node currentNode) {
153147 }
154148
155149 private boolean isEmpty (PriorityQueue <Node > openList ) {
156- return openList .size () == 0 ;
150+ return openList .isEmpty () ;
157151 }
158152
159153 private void setBlock (int row , int col ) {
160154 this .searchArea [row ][col ].setBlock (true );
161155 }
162156
163- public Node getInitialNode () {
164- return initialNode ;
165- }
166-
167157 public void setInitialNode (Node initialNode ) {
168158 this .initialNode = initialNode ;
169159 }
@@ -180,40 +170,21 @@ public Node[][] getSearchArea() {
180170 return searchArea ;
181171 }
182172
183- public void setSearchArea (Node [][] searchArea ) {
184- this .searchArea = searchArea ;
185- }
186-
187173 public PriorityQueue <Node > getOpenList () {
188174 return openList ;
189175 }
190176
191- public void setOpenList (PriorityQueue <Node > openList ) {
192- this .openList = openList ;
193- }
194-
195177 public Set <Node > getClosedSet () {
196178 return closedSet ;
197179 }
198180
199- public void setClosedSet (Set <Node > closedSet ) {
200- this .closedSet = closedSet ;
201- }
202-
203181 public int getHvCost () {
204182 return hvCost ;
205183 }
206184
207- public void setHvCost (int hvCost ) {
208- this .hvCost = hvCost ;
209- }
210-
211185 private int getDiagonalCost () {
212186 return diagonalCost ;
213187 }
214188
215- private void setDiagonalCost (int diagonalCost ) {
216- this .diagonalCost = diagonalCost ;
217- }
218189}
219190
0 commit comments