Skip to content

Commit 4631c9f

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

File tree

3 files changed

+91
-149
lines changed

3 files changed

+91
-149
lines changed

src/com/ai/astar/AStar.java

Lines changed: 55 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/com/ai/astar/AStarTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static void main(String[] args) {
1111
int cols = 7;
1212
AStar aStar = new AStar(rows, cols, initialNode, finalNode);
1313
int[][] blocksArray = new int[][]{{1, 3}, {2, 3}, {3, 3}};
14-
aStar.setBlocks(blocksArray);
14+
aStar.initBlocks(blocksArray);
1515
List<Node> path = aStar.findPath();
1616
for (Node node : path) {
1717
System.out.println(node);

src/com/ai/astar/Node.java

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class Node {
1313
private int h;
1414
private final int row;
1515
private final int col;
16-
private boolean isBlock;
16+
private boolean isBlocked;
1717
private Node parent;
1818

1919
public Node(int row, int col) {
@@ -23,28 +23,51 @@ public Node(int row, int col) {
2323
}
2424

2525
public void calculateHeuristic(Node finalNode) {
26-
this.h = Math.abs(finalNode.getRow() - getRow()) + Math.abs(finalNode.getCol() - getCol());
26+
this.h = Math.abs(finalNode.row() - row) + Math.abs(finalNode.col() - col);
2727
}
2828

2929
public void setNodeData(Node currentNode, int cost) {
30-
int gCost = currentNode.getG() + cost;
31-
setParent(currentNode);
32-
setG(gCost);
33-
calculateFinalCost();
30+
int gCost = currentNode.g() + cost;
31+
this.parent = currentNode;
32+
this.g = gCost;
33+
this.f = g + h;
3434
}
3535

3636
public boolean checkBetterPath(Node currentNode, int cost) {
37-
int gCost = currentNode.getG() + cost;
38-
if (gCost < getG()) {
37+
int gCost = currentNode.g() + cost;
38+
if (gCost < g()) {
3939
setNodeData(currentNode, cost);
4040
return true;
4141
}
4242
return false;
4343
}
4444

45-
private void calculateFinalCost() {
46-
int finalCost = getG() + getH();
47-
setF(finalCost);
45+
public int g() {
46+
return g;
47+
}
48+
49+
public int f() {
50+
return f;
51+
}
52+
53+
public Node parent() {
54+
return parent;
55+
}
56+
57+
public boolean isBlocked() {
58+
return isBlocked;
59+
}
60+
61+
public void setAsBlocked() {
62+
this.isBlocked = true;
63+
}
64+
65+
public int row() {
66+
return row;
67+
}
68+
69+
public int col() {
70+
return col;
4871
}
4972

5073
@Override
@@ -60,56 +83,12 @@ public boolean equals(Object arg0) {
6083
return false;
6184
}
6285
Node other = (Node) arg0;
63-
return this.getRow() == other.getRow() && this.getCol() == other.getCol();
86+
return this.row() == other.row() && this.col() == other.col();
6487
}
6588

6689
@Override
6790
public String toString() {
6891
return "Node [row=" + row + ", col=" + col + "]";
6992
}
7093

71-
public int getH() {
72-
return h;
73-
}
74-
75-
public int getG() {
76-
return g;
77-
}
78-
79-
public void setG(int g) {
80-
this.g = g;
81-
}
82-
83-
public int getF() {
84-
return f;
85-
}
86-
87-
public void setF(int f) {
88-
this.f = f;
89-
}
90-
91-
public Node getParent() {
92-
return parent;
93-
}
94-
95-
public void setParent(Node parent) {
96-
this.parent = parent;
97-
}
98-
99-
public boolean isBlock() {
100-
return isBlock;
101-
}
102-
103-
public void setBlock(boolean isBlock) {
104-
this.isBlock = isBlock;
105-
}
106-
107-
public int getRow() {
108-
return row;
109-
}
110-
111-
public int getCol() {
112-
return col;
113-
}
114-
11594
}

0 commit comments

Comments
 (0)