Skip to content

Commit bd6a4a9

Browse files
committed
fixes #4. Crashes when changed start without resetting.
1 parent 1ee8ebd commit bd6a4a9

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

src/AStarSearch.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ public class AStarSearch extends JPanel {
99
private Tile start;
1010
private Tile goal;
1111
private Ant ant;
12-
private LinkedList<LinkedList<Tile>> allPath2D = new LinkedList<LinkedList<Tile>>();
12+
private LinkedList<LinkedList<Tile>> allPath2D;
1313

1414
public AStarSearch(Tile[][] tiles, Ant ant, Tile start, Tile goal) {
1515
this.tiles = tiles;
1616
this.start = start;
1717
this.goal = goal;
1818
this.ant = ant;
19+
allPath2D = new LinkedList<LinkedList<Tile>>();
1920
}
2021

2122
public LinkedList<Tile> search() {
22-
2323
LinkedList<Tile> output = new LinkedList<>();
2424
LinkedList<Tile> closedSet = new LinkedList<>();
2525

@@ -29,6 +29,8 @@ public LinkedList<Tile> search() {
2929
// add start to openSet
3030
openSet.add(start);
3131

32+
33+
3234
boolean foundGoal = false;
3335
boolean noPath = false;
3436

@@ -106,7 +108,7 @@ public LinkedList<Tile> search() {
106108
} // end else(not goal)
107109

108110
// if openSet is empty, then no path
109-
if (openSet.size() == 0) {
111+
if (openSet.size() == 0 && foundGoal == false) {
110112
System.out.println("No path found");
111113
noPath = true;
112114
}
@@ -178,6 +180,7 @@ public LinkedList<Tile> reconstructPath(Tile current) {
178180
LinkedList<Tile> path = new LinkedList<>();
179181
while (current != null) {
180182
path.add(current);
183+
System.out.println(current);
181184

182185
// move to the previous node
183186
current = current.getCameFrom();

src/Game.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ public Game(JFrame frame) {
152152
setTimerSolving();
153153
}
154154

155-
156-
157155
private void resetButton() {
158156
JButton result = new JButton("Reset");
159157
result.addActionListener(new ActionListener() {
@@ -184,7 +182,6 @@ public void actionPerformed(ActionEvent e) {
184182
setTimerMageCreation();
185183
setTimerSolving();
186184
repaint();
187-
188185
}
189186
});
190187
buttonPanel.add(result);
@@ -257,25 +254,33 @@ public void actionPerformed(ActionEvent e) {
257254
if (startTile == null) {
258255
System.out.println("Please select start location");
259256
}
257+
260258
if (goalTile == null) {
261259
System.out.println("Please select goal location");
262260
}
263261

264262
if (startTile != null && goalTile != null) {
263+
resetCameFrom(tiles); // useful if the user wants to search for the second time or more
265264
ant = new Ant(startTile, goalTile, TILE_SIZE, tiles);
266265
startClicked = true;
267266
startTimeBeforeAnimation = System.currentTimeMillis();
268267
ant.search();
269268
delayPaint();
270269
}
271-
272-
273270
}
274271
});
275272
buttonPanel.add(result);
276273
return result;
277274
}
278275

276+
private void resetCameFrom(Tile[][] tiles) {
277+
for (int i = 0; i < tiles.length; i++) {
278+
for (int j = 0; j < tiles[i].length; j++) {
279+
tiles[i][j].resetCameFrom();
280+
}
281+
}
282+
}
283+
279284
private void selectGoalLocation() {
280285
JButton result = new JButton("Goal Location");
281286
result.addActionListener(new ActionListener() {
@@ -342,6 +347,19 @@ private void handleStartLocationSelection(Tile clickedTile) {
342347
if(startTile != null){
343348
startTile.resetStart();
344349
}
350+
351+
startClicked = false;
352+
startMovingAnt = false;
353+
noPath = false;
354+
ant = null;
355+
tobeDrawn = new LinkedList<Tile>();
356+
357+
obstacleCount = 0;
358+
swamplandCount = 0;
359+
grasslandCount = 0;
360+
openTerrainCount = 0;
361+
362+
345363
// Set the clicked tile as the start location
346364
clickedTile.setStart();
347365
startTile = clickedTile;

src/Tile.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ public void setCameFrom(Tile current) {
292292
cameFrom = current;
293293
}
294294

295+
public void resetCameFrom() {
296+
cameFrom = null;
297+
}
298+
295299
public Tile getCameFrom() {
296300
return cameFrom;
297301
}

0 commit comments

Comments
 (0)