-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenerator.java
More file actions
188 lines (154 loc) · 4.86 KB
/
Generator.java
File metadata and controls
188 lines (154 loc) · 4.86 KB
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
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
/**
*
* Last Edited 24/05/2017 : Antony.J
*
*/
public class Generator implements IGenerator {
public static Random random = new Random();
private State start;
private State goal;
private State curr;
private List<State> next;
private int movedBox;
private long seed;
private Vector2 playerPosition;
private Set<Vector2> floor;
private Set<Vector2> edge;
private EventHandler<ActionEvent> positionHandler;
private EventHandler<ActionEvent> boxHandler;
private EventHandler<ActionEvent> winHandler;
private EventHandler<ActionEvent> loseHandler;
public void moveCharacter(int dir) {
Vector2 newPos = Vector2.add(playerPosition, Vector2.directions[dir]);
for(int i=0; i<curr.getBoxLocations().length; i++) {
if(curr.getBoxLocation(i).equals(newPos)){
for(State s : next) {
if(s.getMovedBox() == i && s.getMovedDirection() == dir && floor.contains(s.getMovedBoxLocation())) {
curr = s;
next = curr.getStates(false);
playerPosition = newPos;
movedBox = curr.getMovedBox();
positionHandler.handle(null);
boxHandler.handle(null);
if(!s.boxHasNextState(false, floor, i) && !hasGoal(s.getMovedBoxLocation())) {
//TODO sometimes box is only blocked by other boxes -> no lose
loseHandler.handle(null);
} else if(hasGoal(s.getMovedBoxLocation())) {
for(Vector2 box : curr.getBoxLocations()) if(!hasGoal(box)) return;
winHandler.handle(null);
}
return;
}
}
return;
}
}
if(floor.contains(newPos)) {
playerPosition = newPos;
System.out.println("Player Location: " + playerPosition.toString());
positionHandler.handle(null);
}
}
public void generateLevel(int numBoxes, int numIterations, int difficulty) {
generateLevel(numBoxes, numIterations, difficulty, System.currentTimeMillis());
}
public void generateLevel(int numBoxes, int numIterations, int difficulty, long seed) {
this.seed = seed;
random.setSeed(seed);
floor = new HashSet<Vector2>();
edge = new HashSet<Vector2>();
goal = new State(randomPositions(numBoxes));
start = goal;
for(int i=0; i<numIterations; i++) {
State temp = start.getRandomState(floor, true, difficulty);
if(temp == null) break;
start = temp;
}
floor.addAll(Arrays.asList(goal.getBoxLocations()));
for(Vector2 v : floor) {
for(Vector2 dir : Vector2.directions) {
Vector2 adjacent = Vector2.add(v, dir);
if(!floor.contains(adjacent)) edge.add(adjacent);
}
}
curr = start;
next = start.getStates(false);
playerPosition = start.getPlayerLocation();
movedBox = curr.getMovedBox();
}
public int getBoxMovedBox() {
return movedBox;
}
public Vector2[] getBoxLocations() {
return curr.getBoxLocations();
}
public Vector2 getPlayerLocation() {
return playerPosition;
}
public Set<Vector2> getFloor() {
return floor;
}
public Vector2[] getGoals() {
return goal.getBoxLocations();
}
public boolean hasGoal(Vector2 g) {
for(Vector2 v : goal.getBoxLocations()) if(v.equals(g)) return true;
return false;
}
public Set<Vector2> getEdges() {
return edge;
}
private Vector2[] randomPositions(int num) {
Vector2[] positions = new Vector2[num];
int[] xRandom = randomSequence(1, num + 1);
int[] yRandom = randomSequence(1, num + 1);
for(int i=0; i<positions.length; i++) positions[i] = new Vector2(xRandom[i], yRandom[i]);
return positions;
}
static public int[] randomSequence(int start, int length) {
int[] array = new int[length];
for(int i=0; i<array.length; i++) array[i] = start + i;
for (int i = array.length - 1; i > 0; i--) {
int index = Generator.random.nextInt(i + 1);
int a = array[index];
array[index] = array[i];
array[i] = a;
}
return array;
}
public void setOnPlayerMove(EventHandler<ActionEvent> eventHandler) {
positionHandler = eventHandler;
}
public void setOnBoxMove(EventHandler<ActionEvent> eventHandler) {
boxHandler = eventHandler;
}
public void setOnWin(EventHandler<ActionEvent> eventHandler) {
winHandler = eventHandler;
}
public void setOnLose(EventHandler<ActionEvent> eventHandler) {
loseHandler = eventHandler;
}
public long getSeed() {
return seed;
}
public void undo() {
if(!curr.equals(start)) {
movedBox = curr.getMovedBox();
curr = curr.getParent();
next = curr.getStates(false);
playerPosition = curr.getPlayerLocation();
positionHandler.handle(null);
boxHandler.handle(null);
}
}
@Override
public void generateLevel() {
}
}