-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSokoMapPoint.java
68 lines (55 loc) · 1.76 KB
/
SokoMapPoint.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
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* @author Hieu Dao Trung, Antonia Schmalstieg, Joschka Fischer, August 2011
*/
public class SokoMapPoint {
static final byte RED = 0, GREEN = 1, VIOLET = 2;
public boolean vertex[];
public byte color;
public SokoMapPoint(boolean[] vertex, byte color) {
this.vertex = vertex;
this.color = color;
}
/**
* Converts a SlimMapPoint-Matrix to a map made from Nodes. The second
* parameter (nodeMap) will be filled with the Nodes that this function
* finds in the SlimMapPoints[]. It will be cleared at the beginning.
*/
public static GameState convertMap(SokoMapPoint[][] mp,
Map<Point, Node> nodeMap) {
nodeMap.clear();
List<Point> diamonds = new LinkedList<Point>();
Point start = null;
// go through the map points and create all corresponding nodes first
for (int x = 0; x < mp.length; x++) {
for (int y = 0; y < mp[x].length; y++) {
if (mp[x] != null && mp[x][y] != null) {
Point p = new Point(x, y);
nodeMap.put(p, new Node(p));
if (mp[x][y].color == GREEN) {
diamonds.add(p);
} else if (mp[x][y].color == VIOLET) {
start = p;
}
}
}
}
// set edges to all nodes
Iterator<Node> nodes = nodeMap.values().iterator();
while (nodes.hasNext()) {
Node node = nodes.next();
for (int dir = 0; dir < node.e.length; dir++) {
if (mp[node.p.x][node.p.y].vertex[dir]) {
// there is an edge to the direct neighbor
node.e[dir] = nodeMap.get(node.p.pointInDir(dir));
}
}
}
if (diamonds.size() != 3 || start == null)
throw new RuntimeException("Map was invalid. Can not convert."); // invalid map
return new GameState(diamonds.toArray(new Point[0]), start, start, null);
}
}