-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalcPusherPath.java
97 lines (76 loc) · 2.47 KB
/
CalcPusherPath.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
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* Calculates drive instructions for Pusher from solution of the SokoSolver.
*/
public class CalcPusherPath {
/**
* @param args
*/
public static void main(String[] args) {
long timeStart = Calendar.getInstance().getTimeInMillis();
SokoMapPoint[][] map = DummyMaps.map_tiny();
SokoSolver solver = new SokoSolver();
List<Node> solution = solver.solve(map);
GameState state = solver.getStartingState();
long timeElapsedSolver = Calendar.getInstance().getTimeInMillis() - timeStart;
System.out.println("Time elapsed in solver: " + timeElapsedSolver + " ms");
String s = (new CalcPusherPath(solution, state)).doConvert();
System.out.println("Instructions: " + s);
long timeElapsedTotal = Calendar.getInstance().getTimeInMillis() - timeStart;
System.out.println("Time elapsed in total: " + timeElapsedTotal + " ms");
}
private int pusherDir;
private boolean isPushing;
private Point[] diamonds;
private Point pusher;
private Iterator<Point> path;
public CalcPusherPath(List<Node> solList, GameState state) {
pusherDir = 0;
isPushing = false;
diamonds = state.diamonds;
pusher = state.pusher;
List<Point> pointPath = new LinkedList<Point>();
for (Node n : solList) {
pointPath.add(n.p);
}
path = pointPath.iterator();
}
public String doConvert() {
// init: take out first point because that is just the starting pusher
// location
StringBuilder s = new StringBuilder();
path.next();
while (path.hasNext()) {
Point nextPoint = path.next();
int nextPusherDir = pusher.directionTo(nextPoint);
if (isPushing && nextPusherDir != pusherDir) {
// go forward backward
s.append('F');
s.append('B');
pusherDir = Point.oppositeDirOf(pusherDir);
isPushing = false;
}
s.append(calcDriveInstruction(pusherDir, nextPusherDir));
pusherDir = nextPusherDir;
pusher = nextPoint;
// check if next move will be pushing
for (int i = 0; i < diamonds.length; i++) {
if (diamonds[i].equals(pusher)) {
isPushing = true;
diamonds[i] = diamonds[i].pointInDir(pusherDir);
break;
}
}
}
s.append("FBX");
return s.toString();
}
private char calcDriveInstruction(int oldDir, int newDir) {
if (oldDir == newDir)
return 'F';
return ((oldDir - newDir + 4) % 4 == 3 ? 'R' : 'L');
}
}