@@ -50,13 +50,11 @@ private void AStarCalculator(){
50
50
Node current ;
51
51
while (true ){
52
52
current = open .poll ();
53
- if (current == null )
54
- break ;
53
+ if (current == null ) break ;
55
54
closed [current .getX ()][current .getY ()] = true ;
56
- if (current .equals (grid [endX ][endY ])) {
57
- return ;
58
- }
55
+ if (current .equals (grid [endX ][endY ])) return ;
59
56
Node t ;
57
+
60
58
if (current .getX () - 1 >= 0 ){
61
59
t = grid [current .getX () - 1 ][current .getY ()];
62
60
checkAndUpdateCost (current , t , current .getFCost () + V_H_COST );
@@ -70,15 +68,18 @@ private void AStarCalculator(){
70
68
checkAndUpdateCost (current , t , current .getFCost () + DIAGONAL_COST );
71
69
}
72
70
}
73
- }
71
+ }
72
+
74
73
if (current .getY () - 1 >= 0 ){
75
74
t = grid [current .getX ()][current .getY () - 1 ];
76
75
checkAndUpdateCost (current , t , current .getFCost () + V_H_COST );
77
76
}
77
+
78
78
if (current .getY () + 1 < grid [0 ].length ){
79
79
t = grid [current .getX ()][current .getY () + 1 ];
80
80
checkAndUpdateCost (current , t , current .getFCost () + V_H_COST );
81
81
}
82
+
82
83
if (current .getX () + 1 < grid .length ){
83
84
t = grid [current .getX () + 1 ][current .getY ()];
84
85
checkAndUpdateCost (current , t , current .getFCost () + V_H_COST );
@@ -105,16 +106,15 @@ public ArrayList<Node> calculate(int width, int height, int sx, int sy, int ex,
105
106
open = new PriorityQueue <>((Object o1 , Object o2 ) -> {
106
107
Node c1 = (Node )o1 ;
107
108
Node c2 = (Node )o2 ;
108
- return c1 .getFCost () < c2 .getFCost () ? - 1 :
109
- c1 .getFCost () > c2 .getFCost () ? 1 : 0 ;
109
+ return c1 .getFCost () < c2 .getFCost () ? -1 : c1 .getFCost () > c2 .getFCost () ? 1 : 0 ;
110
110
});
111
111
112
112
setStartCell (sx , sy );
113
113
setEndCell (ex , ey );
114
114
for (int x = 0 ; x < width ; x ++){
115
115
for (int y = 0 ; y < height ; y ++){
116
116
grid [x ][y ] = new Node (x , y );
117
- grid [x ][y ].setHCost (Math .abs (x - ex )+ Math .abs (x - ey ));
117
+ grid [x ][y ].setHCost (Math .abs (x - ex ) + Math .abs (x - ey ));
118
118
}
119
119
}
120
120
grid [sx ][sy ].setFCost (0 );
@@ -131,13 +131,13 @@ public ArrayList<Node> calculate(int width, int height, int sx, int sy, int ex,
131
131
132
132
if (closed [endX ][endY ]){
133
133
Node current = grid [endX ][endY ];
134
- int X = Integer .parseInt (current .toString ().split ("," )[0 ]);
135
- int Y = Integer .parseInt (current .toString ().split ("," )[1 ]);
134
+ int X = Integer .parseInt (current .toString ().replaceAll ( "Node(" , "" ). replaceAll ( ")" , "" ). split ("," )[0 ]);
135
+ int Y = Integer .parseInt (current .toString ().replaceAll ( "Node(" , "" ). replaceAll ( ")" , "" ). split ("," )[1 ]);
136
136
path .add (new Node (X , Y ));
137
137
138
138
while (current .getParent () != null ){
139
- X = Integer .parseInt (current .getParent ().toString ().split ("," )[0 ]);
140
- Y = Integer .parseInt (current .getParent ().toString ().split ("," )[1 ]);
139
+ X = Integer .parseInt (current .getParent ().toString ().replaceAll ( "Node(" , "" ). replaceAll ( ")" , "" ). split ("," )[0 ]);
140
+ Y = Integer .parseInt (current .getParent ().toString ().replaceAll ( "Node(" , "" ). replaceAll ( ")" , "" ). split ("," )[1 ]);
141
141
path .add (new Node (X , Y ));
142
142
current = current .getParent ();
143
143
}
0 commit comments