-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pathfinder.java
122 lines (98 loc) · 3.68 KB
/
Pathfinder.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
package PacmanPack;
import java.util.ArrayList;
public class Pathfinder {
private static ArrayList<Cell> openSet = new ArrayList<Cell>();
private static ArrayList<Cell> closedSet = new ArrayList<Cell>();
private static ArrayList<Cell> neighbors = new ArrayList<>();
static Cell findPath(Cell start, Cell prev, Cell end, int spriteLevel, boolean canMoveBack) {
emptySets();
if (prev != null && !canMoveBack) {
prev.setG_cost(9999, spriteLevel);
}
openSet.add(start);
start.setParent(null, spriteLevel);
while (!openSet.isEmpty()) {
int winner = 0;
for (int i = 0; i < openSet.size(); ++i) {
if (openSet.get(i).getF_cost(spriteLevel) < openSet.get(winner).getF_cost(spriteLevel)) {
winner = i;
}
}
Cell current = openSet.get(winner);
openSet.remove(current);
closedSet.add(current);
if (current == end) {
openSet.removeAll(openSet);
}
ArrayList<Cell> neighbors = getCellNeighbors(current);
for (Cell n : neighbors) {
if (closedSet.contains(n)) {
continue;
}
int tempG_cost = n.getG_cost(spriteLevel) + 1;
n.setG_cost(tempG_cost, spriteLevel);
n.setParent(current, spriteLevel);
n.setH_cost(calcHCost(n, end), spriteLevel);
n.setF_cost(n.getG_cost(spriteLevel) + n.getH_cost(spriteLevel), spriteLevel);
if (openSet.contains(n)) {
if (n.getG_cost(spriteLevel) > openSet.get(openSet.indexOf(n)).getG_cost(spriteLevel)) {
continue;
}
}
openSet.add(n);
}
}
nullifyCostsClosedSetCells();
return end;
}
private static void emptySets() {
openSet.removeAll(openSet);
closedSet.removeAll(closedSet);
neighbors.removeAll(neighbors);
}
private static void nullifyCostsClosedSetCells() {
for (Cell n : closedSet) {
for (int i = 0; i < 5; ++i) {
if (n.getType() != Pacman.Type.WALL) {
n.setF_cost(0, i);
n.setH_cost(0, i);
n.setG_cost(0, i);
}
}
}
}
private static ArrayList<Cell> getCellNeighbors(Cell cell) {
neighbors.removeAll(neighbors);
if (cell.getUp() != null) {
neighbors.add(cell.getUp());
}
if (cell.getDown() != null) {
neighbors.add(cell.getDown());
}
if (cell.getLeft() != null) {
neighbors.add(cell.getLeft());
}
if (cell.getRight() != null) {
neighbors.add(cell.getRight());
}
return neighbors;
}
private static int calcHCost(Cell n, Cell end) {
int[] to = {end.getRow(), end.getCol()};
int[] L = {14, 0};
int[] R = {14, 27};
int[] from = {n.getRow(), n.getCol()};
int hueRaw = calcRawHCost(from, to);
int hueLPortal = calcRawHCost(from, L) + calcRawHCost(R, to);
int hueRPortal = calcRawHCost(from, R) + calcRawHCost(L, to);
return minHCost(hueRaw, hueLPortal, hueRPortal);
}
private static int calcRawHCost(int[] from, int[] to) {
int dRow = Math.abs(from[0] - to[0]);
int dCol = Math.abs(from[1] - to[1]);
return dRow + dCol;
}
private static int minHCost(int hueRaw, int hueLPortal, int hueRPortal) {
return Math.min(hueRaw, Math.min(hueLPortal, hueRPortal));
}
}