Skip to content

Commit e90f07e

Browse files
author
Pedro Geadas
committed
Cleaned code a bit
1 parent b1ce44e commit e90f07e

File tree

2 files changed

+28
-58
lines changed

2 files changed

+28
-58
lines changed

src/com/ai/astar/AStar.java

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
* @version 2.1, 2017-02-23
1010
*/
1111
public 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

src/com/ai/astar/Node.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public class Node {
1111
private int g;
1212
private int f;
1313
private int h;
14-
private int row;
15-
private int col;
14+
private final int row;
15+
private final int col;
1616
private boolean isBlock;
1717
private Node parent;
1818

@@ -47,8 +47,18 @@ private void calculateFinalCost() {
4747
setF(finalCost);
4848
}
4949

50+
@Override
51+
public int hashCode() {
52+
int result = row;
53+
result = 31 * result + col;
54+
return result;
55+
}
56+
5057
@Override
5158
public boolean equals(Object arg0) {
59+
if (arg0 == null) {
60+
return false;
61+
}
5262
Node other = (Node) arg0;
5363
return this.getRow() == other.getRow() && this.getCol() == other.getCol();
5464
}
@@ -62,10 +72,6 @@ public int getH() {
6272
return h;
6373
}
6474

65-
public void setH(int h) {
66-
this.h = h;
67-
}
68-
6975
public int getG() {
7076
return g;
7177
}
@@ -102,15 +108,8 @@ public int getRow() {
102108
return row;
103109
}
104110

105-
public void setRow(int row) {
106-
this.row = row;
107-
}
108-
109111
public int getCol() {
110112
return col;
111113
}
112114

113-
public void setCol(int col) {
114-
this.col = col;
115-
}
116115
}

0 commit comments

Comments
 (0)