-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJumpPointSearch.java
191 lines (179 loc) · 7.43 KB
/
JumpPointSearch.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import java.util.ArrayList;
public class JumpPointSearch extends MazeSearch {
private ArrayList<Cell> open_list = new ArrayList<Cell>();
private ArrayList<Integer> fcosts_list = new ArrayList<Integer>();
private Cell path_cell = null;
public boolean path_drawn = false;
private String distance_substring = " estimated path distance";
public JumpPointSearch(int[][] generated_maze) {
super(generated_maze);
prepareSearch();
}
@Override
public boolean isDone() {
return path_drawn;
}
@Override
protected void prepareSearch() {
start_cell.parent = start_cell;
open_list.add(start_cell);
fcosts_list.add(0);
}
@Override
protected void expandSearch() {
if (end_cell == null || open_list.size() <= 0) {
distance = -1;
maze[start_cell.y][start_cell.x] = END;
path_drawn = true;
}
else if (path_cell != null) { // Backtrack
steps--;
do {
path_cell = super.backtrackPath(path_cell);
if (maze[path_cell.y][path_cell.x] == START) path_drawn = true;
} while (maze[path_cell.y][path_cell.x] == PASSAGE && !path_drawn);
if (!path_drawn)
path_cell = super.backtrackPath(path_cell);
}
else { // Search
Cell cell = open_list.remove(0);
fcosts_list.remove(0);
computeNeighbors(cell);
if (maze[cell.y][cell.x] != START) maze[cell.y][cell.x] = CLOSED;
distance = cell.getFunctionalCost();
}
}
private void computeNeighbors(Cell c) {
int y = c.y, x = c.x;
boolean[] directions = new boolean[4]; // up, down, left, right
final int UP = 1, DOWN = 2, LEFT = 3, RIGHT = 4;
if (y > 1 && (maze[y-1][x] == PASSAGE || maze[y-1][x] == END) && c.parent.y >= y)
directions[UP - 1] = true;
if (y < maze.length-2 && (maze[y+1][x] == PASSAGE || maze[y+1][x] == END) && c.parent.y <= y)
directions[DOWN - 1] = true;
if (x > 1 && (maze[y][x-1] == PASSAGE || maze[y][x-1] == END) && c.parent.x >= x)
directions[LEFT - 1] = true;
if (x < maze[0].length-2 && (maze[y][x+1] == PASSAGE || maze[y][x+1] == END) && c.parent.x <= x)
directions[RIGHT - 1] = true;
if (directions[UP - 1] == true) {
Cell current = c;
while (current.y > 1 && maze[current.y-1][current.x] == PASSAGE &&
(maze[current.y-2][current.x] == PASSAGE || (maze[current.y-2][current.x] == END))) {
Cell connection_cell = new Cell(current.y - 1, current.x, current.getCost() + 1);
Cell jump_cell = new Cell(current.y - 2, current.x, current.getCost() + 2);
connection_cell.parent = current;
jump_cell.parent = connection_cell;
current = jump_cell;
if (current.y == end_cell.y && current.x == end_cell.x) {
path_cell = current;
path_cell = super.backtrackPath(path_cell);
distance_substring = " path distance";
break;
}
if ((maze[current.y][current.x] == PASSAGE) && // Cell is a passage
((current.x > 1 && (maze[current.y][current.x-1] == PASSAGE || maze[current.y][current.x-1] == END) && // Passage on the left AND...
(maze[current.y-1][current.x-1] == WALL || maze[current.y+1][current.x-1] == WALL)) || // walls on either side of said passsage
(current.x < maze[0].length-2 && (maze[current.y][current.x+1] == PASSAGE || maze[current.y][current.x+1] == END) &&
(maze[current.y-1][current.x+1] == WALL || maze[current.y+1][current.x+1] == WALL)))) {
addOpenCell(current);
maze[current.y][current.x] = OPEN;
break;
}
}
}
if (directions[DOWN - 1] == true) {
Cell current = c;
while (current.y < maze.length-2 && maze[current.y+1][current.x] == PASSAGE &&
(maze[current.y+2][current.x] == PASSAGE || (maze[current.y+2][current.x] == END))) {
Cell connection_cell = new Cell(current.y + 1, current.x, current.getCost() + 1);
Cell jump_cell = new Cell(current.y + 2, current.x, current.getCost() + 2);
connection_cell.parent = current;
jump_cell.parent = connection_cell;
current = jump_cell;
if (current.y == end_cell.y && current.x == end_cell.x) {
path_cell = current;
path_cell = super.backtrackPath(path_cell);
distance_substring = " path distance";
break;
}
if ((maze[current.y][current.x] == PASSAGE) &&
((current.x > 1 && (maze[current.y][current.x-1] == PASSAGE || maze[current.y][current.x-1] == END) &&
(maze[current.y-1][current.x-1] == WALL || maze[current.y+1][current.x-1] == WALL)) ||
(current.x < maze[0].length-2 && (maze[current.y][current.x+1] == PASSAGE || maze[current.y][current.x+1] == END) &&
(maze[current.y-1][current.x+1] == WALL || maze[current.y+1][current.x+1] == WALL)))) {
addOpenCell(current);
maze[current.y][current.x] = OPEN;
break;
}
}
}
if (directions[LEFT - 1] == true) {
Cell current = c;
while (current.x > 1 && maze[current.y][current.x-1] == PASSAGE &&
(maze[current.y][current.x-2] == PASSAGE || (maze[current.y][current.x-2] == END))) {
Cell connection_cell = new Cell(current.y, current.x - 1, current.getCost() + 1);
Cell jump_cell = new Cell(current.y, current.x - 2, current.getCost() + 2);
connection_cell.parent = current;
jump_cell.parent = connection_cell;
current = jump_cell;
if (current.y == end_cell.y && current.x == end_cell.x) {
path_cell = current;
path_cell = super.backtrackPath(path_cell);
distance_substring = " path distance";
break;
}
if ((maze[current.y][current.x] == PASSAGE) &&
((current.y > 1 && (maze[current.y-1][current.x] == PASSAGE || maze[current.y-1][current.x] == END) &&
(maze[current.y-1][current.x-1] == WALL || maze[current.y-1][current.x+1] == WALL)) ||
(current.y < maze.length-2 && (maze[current.y+1][current.x] == PASSAGE || maze[current.y+1][current.x] == END) &&
(maze[current.y+1][current.x-1] == WALL || maze[current.y+1][current.x+1] == WALL)))) {
addOpenCell(current);
maze[current.y][current.x] = OPEN;
break;
}
}
}
if (directions[RIGHT - 1] == true) {
Cell current = c;
while (current.x < maze[0].length-2 && maze[current.y][current.x+1] == PASSAGE &&
(maze[current.y][current.x+2] == PASSAGE || (maze[current.y][current.x+2] == END))) {
Cell connection_cell = new Cell(current.y, current.x + 1, current.getCost() + 1);
Cell jump_cell = new Cell(current.y, current.x + 2, current.getCost() + 2);
connection_cell.parent = current;
jump_cell.parent = connection_cell;
current = jump_cell;
if (current.y == end_cell.y && current.x == end_cell.x) {
path_cell = current;
path_cell = super.backtrackPath(path_cell);
distance_substring = " path distance";
break;
}
if ((maze[current.y][current.x] == PASSAGE) &&
((current.y > 1 && (maze[current.y-1][current.x] == PASSAGE || maze[current.y-1][current.x] == END) &&
(maze[current.y-1][current.x-1] == WALL || maze[current.y-1][current.x+1] == WALL)) ||
(current.y < maze.length-2 && (maze[current.y+1][current.x] == PASSAGE || maze[current.y+1][current.x] == END) &&
(maze[current.y+1][current.x-1] == WALL || maze[current.y+1][current.x+1] == WALL)))) {
addOpenCell(current);
maze[current.y][current.x] = OPEN;
break;
}
}
}
}
private void addOpenCell(Cell c) {
int functional_cost = c.getFunctionalCost();
for (int i = 0; i < open_list.size(); i++) {
if (functional_cost < fcosts_list.get(i)) {
open_list.add(i, c);
fcosts_list.add(i, functional_cost);
return;
}
}
open_list.add(c);
fcosts_list.add(functional_cost);
}
@Override
public String getDistanceString() {
return distance + distance_substring;
}
}