-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.java
311 lines (263 loc) · 6.9 KB
/
main.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
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.TreeSet;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import javax.imageio.ImageIO;
public class main {
static Board board;
static Logger logger;
static Handler handler;
static int finalCost;
public static void main(String[] args) {
board = new Board(new State(100, 100), new State(400, 400));
logger = Logger.getLogger(main.class.getName());
finalCost = 0;
try {
handler = new FileHandler("logfile%g.txt");
handler.setFormatter(new SimpleFormatter());
logger.addHandler(handler);
}
catch (IOException Err) {
System.out.println(Err.getMessage());
logger.log(Level.WARNING, "Error creating log file.");
}
try {
board.setBoard("terrain.png");
logger.log(Level.INFO, "Loaded terrain image.");
}
catch (IOException NoFile) {
logger.log(Level.SEVERE,"Error loading terrain image.");
logger.log(Level.SEVERE, NoFile.getMessage());
return;
}
board.createOutImage();
logger.log(Level.INFO, "Starting.");
finalCost = board.findPath(logger);
if (finalCost == -1) {
logger.log(Level.INFO, "No path found.");
}
else {
logger.log(Level.INFO, "Cost was " + Integer.toString(finalCost));
}
board.drawPath();
logger.log(Level.INFO, "Creating output image.");
try {
board.setOutImage("path.png");
}
catch (IOException FileErr) {
logger.log(Level.SEVERE, "Error creating path image.");
}
logger.log(Level.INFO, "Complete.");
}
}
class Board {
State startState;
State goalState;
BufferedImage board;
BufferedImage newImage;
PriorityQueue<State> stateQueue;
TreeSet<State> beenThere;
int height;
int width;
public Board(State start, State goal) {
stateQueue = new PriorityQueue<State>(10, new CostComparator());
beenThere = new TreeSet<State>(new StateComparator());
startState = start;
startState.setCost(0);
startState.parent = null;
goalState = goal;
}
public void setBoard(String file) throws IOException {
board = ImageIO.read(new File(file));
height = board.getHeight();
width = board.getWidth();
}
public void createOutImage() {
newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}
public void setOutImage(String file) throws IOException {
ImageIO.write(newImage, "png", new File(file));
}
public void drawPath() {
State currentState = goalState;
while (currentState.parent != null) {
newImage.setRGB(currentState.getX(), currentState.getY(), 0xFFFF0000);
currentState = currentState.parent;
};
}
public int findPath(Logger logger) {
int x;
int y;
int cost = 0;
Color c;
State tempState;
int iterations = 0;
stateQueue.add(startState);
beenThere.add(startState);
while (stateQueue.size() > 0) {
State currentState = stateQueue.remove();
if (currentState.isEquals(goalState)) {
goalState.parent = currentState.parent;
return currentState.getCost();
}
x = currentState.getX();
y = currentState.getY();
if (iterations++ % 5000 < 1000) {
newImage.setRGB(x, y, 0xff00ff00);
}
else {
newImage.setRGB(x, y, board.getRGB(x, y));
}
// look up
if (x != 0) {
State upState = new State(x-1, y);
c = new Color(board.getRGB(x-1, y));
cost = c.getGreen();
if (beenThere.contains(upState)) {
tempState = beenThere.floor(upState);
if (currentState.getCost() + cost < tempState.getCost()) {
tempState.setCost(currentState.getCost() + cost);
tempState.parent = currentState;
}
}
else {
upState.setCost(currentState.getCost() + cost);
upState.parent = currentState;
stateQueue.add(upState);
beenThere.add(upState);
}
}
// look down
if (x < (height - 1)) {
State downState = new State(x+1, y);
c = new Color(board.getRGB(x+1, y));
cost = c.getGreen();
if (beenThere.contains(downState)) {
tempState = beenThere.floor(downState);
if (currentState.getCost() + cost < tempState.getCost()) {
tempState.setCost(currentState.getCost() + cost);
tempState.parent = currentState;
}
}
else {
downState.setCost(currentState.getCost() + cost);
downState.parent = currentState;
stateQueue.add(downState);
beenThere.add(downState);
}
}
// look left
if (y != 0) {
State leftState = new State(x, y-1);
c = new Color(board.getRGB(x, y-1));
cost = c.getGreen();
if (beenThere.contains(leftState)) {
tempState = beenThere.floor(leftState);
if (currentState.getCost() + cost < tempState.getCost()) {
tempState.setCost(currentState.getCost() + cost);
tempState.parent = currentState;
}
}
else {
leftState.setCost(currentState.getCost() + cost);
leftState.parent = currentState;
stateQueue.add(leftState);
beenThere.add(leftState);
}
}
if (y < (width - 1)) {
State rightState = new State(x, y+1);
c = new Color(board.getRGB(x, y+1));
cost = c.getGreen();
if (beenThere.contains(rightState)) {
tempState = beenThere.floor(rightState);
if (currentState.getCost() + cost < tempState.getCost()) {
tempState.setCost(currentState.getCost() + cost);
tempState.parent = currentState;
}
}
else {
rightState.setCost(currentState.getCost() + cost);
rightState.parent = currentState;
stateQueue.add(rightState);
beenThere.add(rightState);
}
}
}
return -1;
}
}
class State {
private int x;
private int y;
private int cost;
State parent;
State() {
}
State(int newX, int newY) {
x = newX;
y = newY;
}
public boolean isEquals(State test) {
if ((x == test.getX()) && (y == test.getY())) {
return true;
} else {
return false;
}
}
public int getX() {
return x;
}
public void setX(int newX) {
x = newX;
}
public int getY() {
return y;
}
public void setY(int newY) {
y = newY;
}
public int getCost() {
return cost;
}
public void setCost(int newCost) {
cost = newCost;
}
public String getPosition() {
return new String("(" + Integer.toString(x) + "," + Integer.toString(y) + ")");
}
}
class CostComparator implements Comparator<State> {
public int compare(State x, State y) {
if (x.getCost() < y.getCost()) {
return -1;
} else if(x.getCost() > y.getCost()) {
return 1;
} else {
return 0;
}
}
}
class StateComparator implements Comparator<State> {
public int compare(State left, State right) {
if (left.getX() < right.getX()) {
return -1;
} else if (left.getX() > right.getX()) {
return 1;
} else if (left.getY() < right.getY()) {
return -1;
} else if (left.getY() > right.getY()) {
return 1;
} else {
return 0;
}
}
}