-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathfinding.java
171 lines (139 loc) · 5.96 KB
/
Pathfinding.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
public class Pathfinding {
public static void BreathFirstSearch(Tile start, Tile goal, Tile[][] tiles) {
Queue<Tile> frontier = new LinkedList<>(); // Stores the discovered nodes
frontier.add(start);
HashMap<Tile, Tile> cameFrom = new HashMap<>(); // Maps from which node we came from
cameFrom.put(start, null);
while (!frontier.isEmpty()) {
Tile current = frontier.poll(); // Gets nodes first in, first out
current.SetType(NodeType.VISITIED);
if (current == goal)
break;
for (Tile next : GetNeighbors(current, tiles)) {
if (!cameFrom.containsKey(next)) {
next.SetType(NodeType.FRONTIER);
frontier.add(next);
cameFrom.put(next, current);
}
}
// 1/10 sec sleep for animation to be visible
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Reconstructs the path
Tile current = goal;
while (current != null) {
current.SetType(NodeType.PATH);
current = cameFrom.get(current);
}
}
public static void GreedyBestFirstSearch(Tile start, Tile goal, Tile[][] tiles) {
PriorityQueue<Tile> frontier = new PriorityQueue<>(new Comparator<Tile>() { // Min-heap the uses the cost of each to node for its comparision
@Override
public int compare(Tile o1, Tile o2) {
return Integer.compare(o1.GetCost(), o2.GetCost());
}
});
frontier.add(start);
HashMap<Tile, Tile> cameFrom = new HashMap<>(); // Maps from which node we came from
cameFrom.put(start, null);
while (!frontier.isEmpty()) {
Tile current = frontier.poll(); // Gets the node with the lowest cost
current.SetType(NodeType.VISITIED);
if (current == goal)
break;
for (Tile next : GetNeighbors(current, tiles)) {
if (!cameFrom.containsKey(next)) {
int priority = Heuristic(goal, next); // Uses Manhatten distance to set priority
next.SetCost(priority);
next.SetType(NodeType.FRONTIER);
frontier.add(next);
cameFrom.put(next, current);
}
}
// 1/10 sec sleep for animation to be visible
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Reconstructs the path
Tile current = goal;
while (current != null) {
current.SetType(NodeType.PATH);
current = cameFrom.get(current);
}
}
public static void AStar(Tile start, Tile goal, Tile[][] tiles) {
PriorityQueue<Tile> frontier = new PriorityQueue<>(new Comparator<Tile>() { // Min-heap the uses the cost of each to node for its comparision
@Override
public int compare(Tile o1, Tile o2) {
return Integer.compare(o1.GetCost(), o2.GetCost());
}
});
HashMap<Tile, Tile> cameFrom = new HashMap<>(); // Maps from which node we came from
HashMap<Tile, Integer> costSoFar = new HashMap<>(); // Stores the cost it takes to get from the start to the current node
frontier.add(start);
cameFrom.put(start, null);
costSoFar.put(start, 0);
while (!frontier.isEmpty()) {
Tile current = frontier.poll(); // Gets the node with the lowest cost
current.SetType(NodeType.VISITIED);
if (current == goal)
break;
for (Tile next : GetNeighbors(current, tiles)) {
int newCost = costSoFar.get(current) + 2; // The cost to get to this node + a uniform movement cost
if (!costSoFar.containsKey(next) || newCost < costSoFar.get(next)) { // It is possible to visit the same node more than once so we update the cost if it is lower
costSoFar.put(next, newCost);
int priority = newCost + Heuristic(goal, next);
next.SetCost(priority);
next.SetType(NodeType.FRONTIER);
frontier.add(next);
cameFrom.put(next, current);
}
}
// 1/10 sec sleep for animation to be visible
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Reconstructs the path
Tile current = goal;
while (current != null) {
current.SetType(NodeType.PATH);
current = cameFrom.get(current);
}
}
// Manhattan distance
private static int Heuristic(Tile a, Tile b) {
return Math.abs(a.GetX() - b.GetX()) + Math.abs(a.GetY() - b.GetY());
}
// Returns all the adjacent neighbors (not the diagonals)
private static List<Tile> GetNeighbors(Tile current, Tile[][] tiles) {
List<Tile> neighbors = new ArrayList<>();
int x = current.GetX();
int y = current.GetY();
if (x > 0 && tiles[x - 1][y].GetType() != NodeType.BARRIER)
neighbors.add(tiles[x - 1][y]);
if (x < tiles[0].length - 1 && tiles[x + 1][y].GetType() != NodeType.BARRIER)
neighbors.add(tiles[x + 1][y]);
if (y > 0 && tiles[x][y - 1].GetType() != NodeType.BARRIER)
neighbors.add(tiles[x][y - 1]);
if (y < tiles.length - 1 && tiles[x][y + 1].GetType() != NodeType.BARRIER)
neighbors.add(tiles[x][y + 1]);
return neighbors;
}
}