-
Notifications
You must be signed in to change notification settings - Fork 0
/
Help.java
453 lines (372 loc) · 12 KB
/
Help.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import java.awt.Point;
import java.util.*;
//class for helpful methods
public class Help {
//int count of bombs (walls count as 1, ground nothing)
//heuristic is 0
//will require some kind of convert to action sequence
//note that it uses the java.awt.Point class which contains 2 ints
// public static LinkedList<Point> aStar(Point start, Point dest, int maxWeight, LinkedList<Character> avoid) {
// PriorityQueue<QueueNode<Point>> pq = new PriorityQueue<QueueNode<Point>>();
//
// HashSet<QueueNode<Point>> seen = new HashSet<QueueNode<Point>>();
//
// QueueNode<Point> popped = null;
// Point curPoint = null;
//
// pq.add(new QueueNode<Point>(null, start, 0+0));
// while (!pq.isEmpty()) {
// popped = pq.poll();
// curPoint = popped.e;
//
// if (!seen.contains(popped)) {
// seen.add(popped);
// }
//
// if (curPoint.equals(dest)) { //we are done here
// break;
// } else {
//
// for (Point p : getNeighbours(curPoint)) {
// if (seen.contains(p)) {
// continue;
// }
//
// pq.add(new QueueNode<Point>(popped, p, 0+0));
// }
// }
// }
//
// LinkedList<Point> trail = new LinkedList<Point>();
// QueueNode<Point> cur = popped;
// while(cur != null) {
// trail.addFirst(cur.e);
// cur = cur.parent;
// }
//
// return trail;
// }
public static LinkedList<Point> bfs4CharThroughWall(Point start, char in, int maxWeight, LinkedList<Character> avoid) {
if (avoid == null) {
avoid = new LinkedList<Character>(); //just so its searchable but useless (so you can give it null)
}
LinkedList<Point> Q = new LinkedList<Point>();
HashMap<Point, Point> parentMap = new HashMap<Point, Point>();
Point v = null;
boolean foundSolution = false;
Q.add(start); //enqueue
while (!Q.isEmpty()) {
v = Q.poll(); //dequeue
//process v
char array[] = {in};
if (isCharWithinRange(v, array, 0)) {
foundSolution = true;
break;
}
for (Point p : getNeighbours(v)) {
if (avoid.contains(Agent.grid[p.y][p.x])) {
continue;
}
LinkedList<Character> temp = getPath(parentMap, start, p);
int occurrences = Collections.frequency(temp, '*');
if (occurrences > maxWeight) {
continue;
}
if (!parentMap.containsKey(p)) {
Q.add(p);
parentMap.put(p, v);
}
}
}
if (!foundSolution) { //then we may have a problem
return new LinkedList<Point>();
}
LinkedList<Point> trail = new LinkedList<Point>();
trail.add(v);
while(!v.equals(start)) {
trail.addFirst(parentMap.get(v));
v = parentMap.get(v);
}
return trail;
}
private static LinkedList<Character> getPath(HashMap<Point, Point> map, Point start, Point v) {
LinkedList<Character> trail = new LinkedList<Character>();
trail.add(Agent.grid[v.y][v.x]);
while(!v.equals(start) && map.containsKey(v)) {
trail.add(Agent.grid[map.get(v).y][map.get(v).x]);
v = map.get(v);
}
return trail;
}
// private static int getWeightofPath(HashMap<Point, Point> map, Point start, Point currentP) {
// int weight = 0;
//
// while(currentP != null && !currentP.equals(start)) {
// if (Agent.grid[currentP.y][currentP.x] == '*') {
// weight++;
// }
// currentP = map.get(currentP);
// }
// if (weight > 1) System.exit(0);
// return weight;
// }
//special case of bfs that just looks for the closest given char, and returns the first move Point
/**
* @param in char to search to
* @param offset distance to char that is acceptable
* @param avoid list of chars that act as walls in the search
* @return
*/
public static LinkedList<Point> bfs4Chars(Point start, char in[], int offset, LinkedList<Character> avoid) {
if (avoid == null) {
avoid = new LinkedList<Character>(); //just so its search able but useless
}
LinkedList<Point> Q = new LinkedList<Point>();
HashMap<Point, Point> parentMap = new HashMap<Point, Point>();
Point v = null;
boolean foundSolution = false;
Q.add(start); //enqueue
while (!Q.isEmpty()) {
v = Q.poll(); //dequeue
//process v
if (isCharWithinRange(v, in, offset)) {
foundSolution = true;
break;
}
for (Point p : getNeighbours(v)) {
if (avoid.contains(Agent.grid[p.y][p.x])) {
continue;
}
if (!parentMap.containsKey(p)) {
Q.add(p);
parentMap.put(p, v);
}
}
//avoid.remove(new Character(' '));//so it doesn't never get off land
}
if (!foundSolution) { //then we may have a problem
return new LinkedList<Point>();
}
LinkedList<Point> trail = new LinkedList<Point>();
trail.add(v);
while(!v.equals(start)) {
trail.addFirst(parentMap.get(v));
v = parentMap.get(v);
}
return trail;
}
private static boolean isCharWithinRange(Point p, char c[], int range) {
for (int k = 0; k < c.length; k++) {
if (range < 1 && Agent.grid[p.y][p.x] == c[k]) { //so its the thing im searching for
return true;
}
}
for (int i = -range; i < range+1; i++) {
for (int j = -range; j < range+1; j++) {
for (int k = 0; k < c.length; k++) {
if (c[k] == Agent.grid[p.y+i][p.x+j]) {
return true;
}
}
}
}
return false;
}
// looks for a point
public static LinkedList<Point> bfs4Point(Point start, Point end, int offset, LinkedList<Character> avoid) {
if (avoid == null) {
avoid = new LinkedList<Character>(); //just so its search able but useless
}
LinkedList<Point> Q = new LinkedList<Point>();
HashMap<Point, Point> parentMap = new HashMap<Point, Point>();
Point v = null;
boolean foundSolution = false;
Q.add(start); //enqueue
while (!Q.isEmpty()) {
v = Q.poll(); //dequeue
if (v.x == end.x && v.y == end.y) {
foundSolution = true;
break;
}
for (Point p : getNeighbours(v)) {
if (avoid.contains(Agent.grid[p.y][p.x])) {
continue;
}
if (!parentMap.containsKey(p)) {
Q.add(p);
parentMap.put(p, v);
}
}
}
if (!foundSolution) { //then we may have a problem
return new LinkedList<Point>();
}
LinkedList<Point> trail = new LinkedList<Point>();
trail.add(v);
while(!v.equals(start)) {
trail.addFirst(parentMap.get(v));
v = parentMap.get(v);
}
return trail;
}
//gets the neighbours
public static LinkedList<Point> getNeighbours(Point in) {
LinkedList<Point> out = new LinkedList<Point>();
if (isInside(in.x+1, in.y)) out.add(new Point(in.x+1,in.y));
if (isInside(in.x-1, in.y)) out.add(new Point(in.x-1,in.y));
if (isInside(in.x, in.y+1)) out.add(new Point(in.x,in.y+1));
if (isInside(in.x, in.y-1)) out.add(new Point(in.x,in.y-1));
return out;
}
//simple grid checker
public static boolean isInside(int x, int y) {
if (x >= Agent.MAX_SIZE || x < 0) return false;
if (y >= Agent.MAX_SIZE || y < 0) return false;
return true;
}
//found on the internets
//http://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array
public static char[][] rotateMatrixRight(char[][] matrix) {
int w = matrix.length; // W and H are already swapped
int h = matrix[0].length;
char[][] ret = new char[h][w];
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
ret[i][j] = matrix[w - j - 1][i];
}
}
return ret;
}
public static char[][] rotateMatrixLeft(char[][] matrix) {
int w = matrix.length; // W and H are already swapped
int h = matrix[0].length;
char[][] ret = new char[h][w];
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
ret[i][j] = matrix[j][h - i - 1];
}
}
return ret;
}
//Priority queue node
// static class QueueNode<E> implements Comparable<QueueNode<E>> {
// QueueNode<E> parent;
// E e;
// double weight; //although its probably an int
//
// QueueNode (QueueNode<E> parent, E e, double weight) {
// this.parent = parent;
// this.e = e;
// this.weight = weight;
// }
//
// @Override
// public int compareTo(QueueNode<E> arg0) {
// if (this.weight <= arg0.weight) {
// return -1;
// }
// return 1;
// }
// }
// A star search. MaxWeight is the number of bombs the agent has
public static LinkedList<Point> aStarSearch(Point from, char to, int maxWeight, LinkedList<Character> avoid) {
Queue<State> openSet = new PriorityQueue<State>(1,
new Comparator<State>() {
@Override
public int compare(State cost1, State cost2) {
return cost1.getGVal() - cost2.getGVal();
}
});
ArrayList<State> closedSet = new ArrayList<State>();
LinkedList<Point> trail = new LinkedList<Point>();
HashMap<Point, Point> parentMap = new HashMap<Point, Point>();
State first = new State(from, 0);
openSet.add(first);
while (!openSet.isEmpty()) {
State current = new State(new Point(0, 0), 0);
current.setNode(openSet.peek().getNode());
current.setGVal(openSet.peek().getGVal());
openSet.remove();
closedSet.add(current);
char array[] = {to};
if (isCharWithinRange(current.getNode(), array, 0)) {
trail.add(current.getNode());
while(!current.getNode().equals(from)) {
trail.addFirst(parentMap.get(current.getNode()));
current.setNode(parentMap.get(current.getNode()));
}
break;
}
for (Point p : getNeighbours(current.getNode())) {
if (avoid.contains(Agent.grid[p.y][p.x])) {
continue;
}
State temp = new State(p, current.getGVal());
if (Agent.grid[p.y][p.x] == '*') {
temp.setGVal(current.getGVal() + 100);
}
if (Agent.grid[p.y][p.x] == ' ' || Agent.grid[p.y][p.x] == '~') {
temp.setGVal(current.getGVal() + 1);
}
if (checkClosedSet(closedSet, temp.getNode()) == false
&& checkOpenSet(openSet, temp.getNode(), temp.getGVal()) == false) {
if (!parentMap.containsKey(p)) {
openSet.add(temp);
parentMap.put(p, current.getNode());
}
}
}
}
int bombsNeeded = 0;
for (int j = 0; j < trail.size(); j++) {
if (Agent.grid[trail.get(j).y][trail.get(j).x] == '*') {
bombsNeeded++;
}
if (bombsNeeded > maxWeight) {
trail.clear();
}
}
return trail;
}
/**
* Checks the open set for a node, and updates it with a better value if found
* @param set the open set
* @param name name of the city
* @param gVal g value of the path
* @return true if found, false otherwise
*/
private static boolean checkOpenSet (Queue<State> set, Point name, int gVal) {
ArrayList<State> list = new ArrayList<State>();
State temp = new State(new Point(0, 0), 0);
int i = 0;
while (!set.isEmpty()) {
list.add(set.remove());
}
set.addAll(list);
for(i = 0; i < list.size(); i++) {
temp = list.get(i);
if(temp.getNode().x == name.x && temp.getNode().y == name.y && temp.getGVal() > gVal) {
set.remove(temp);
temp.setGVal(gVal);
set.add(temp);
return true;
}
}
return false;
}
/**
* Determines if a State is in the closed set
* @param set the closed set
* @param name the name of the state
* @return true if found false otherwise
*/
private static boolean checkClosedSet (ArrayList<State> set, Point name) {
int i = 0;
for (i = 0; i < set.size(); i++) {
if (set.get(i).getNode().x == name.x && set.get(i).getNode().y == name.y) {
return true;
}
}
return false;
}
}