Skip to content

Commit f9f8d01

Browse files
committed
fixed heap error
1 parent 27cf435 commit f9f8d01

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

A2_Q1/src/AStarSearch.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import java.util.ArrayList;
2+
import java.util.LinkedList;
33
import java.util.PriorityQueue;
44
import javax.swing.JPanel;
55

@@ -9,7 +9,7 @@ public class AStarSearch extends JPanel {
99
private Tile start;
1010
private Tile goal;
1111
private Ant ant;
12-
private ArrayList<ArrayList<Tile>> allPath2D = new ArrayList<ArrayList<Tile>>();
12+
private LinkedList<LinkedList<Tile>> allPath2D = new LinkedList<LinkedList<Tile>>();
1313

1414
public AStarSearch(Tile[][] tiles, Ant ant, Tile start, Tile goal) {
1515
this.tiles = tiles;
@@ -18,10 +18,10 @@ public AStarSearch(Tile[][] tiles, Ant ant, Tile start, Tile goal) {
1818
this.ant = ant;
1919
}
2020

21-
public ArrayList<Tile> search() {
21+
public LinkedList<Tile> search() {
2222

23-
ArrayList<Tile> output = new ArrayList<>();
24-
ArrayList<Tile> closedSet = new ArrayList<>();
23+
LinkedList<Tile> output = new LinkedList<>();
24+
LinkedList<Tile> closedSet = new LinkedList<>();
2525

2626
// create openSet and add start to it
2727
PriorityQueue<Tile> openSet = new PriorityQueue<Tile>();
@@ -35,7 +35,7 @@ public ArrayList<Tile> search() {
3535
// while openSet is not empty
3636
while (openSet.size() > 0 && foundGoal == false && noPath == false) {
3737
// create a row for each iteration for drawing purposes
38-
ArrayList<Tile> row = new ArrayList<>();
38+
LinkedList<Tile> row = new LinkedList<>();
3939

4040
// check if first element in openSet is goal
4141
Tile current = openSet.poll();
@@ -172,8 +172,8 @@ private PriorityQueue<Tile> getNeighbors(Tile tile) {
172172
return neighbors;
173173
}
174174

175-
public ArrayList<Tile> reconstructPath(Tile current) {
176-
ArrayList<Tile> path = new ArrayList<>();
175+
public LinkedList<Tile> reconstructPath(Tile current) {
176+
LinkedList<Tile> path = new LinkedList<>();
177177

178178
while (current != null) {
179179
path.add(current);

A2_Q1/src/Ant.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.awt.Image;
88
import java.io.File;
99
import java.io.IOException;
10-
import java.util.ArrayList;
10+
import java.util.LinkedList;
1111

1212
import javax.imageio.ImageIO;
1313

@@ -19,11 +19,11 @@ public class Ant {
1919
private Tile goal;
2020
private Tile[][] tiles;
2121
private Image antImage;
22-
private ArrayList<Tile> path;
22+
private LinkedList<Tile> path;
2323
private Graphics g;
2424

2525
private AStarSearch aStarSearch;
26-
private ArrayList<ArrayList<Tile>> allPath2D = new ArrayList<ArrayList<Tile>>();
26+
private LinkedList<LinkedList<Tile>> allPath2D = new LinkedList<LinkedList<Tile>>();
2727

2828
public Ant(Tile start, Tile goal, int tileSize, Tile[][] tiles) {
2929
this.start = start;
@@ -52,21 +52,21 @@ public void search() {
5252
path = aStarSearch.search();
5353
}
5454

55-
public ArrayList<Tile> getPath() {
55+
public LinkedList<Tile> getPath() {
5656

57-
// find the path and store it in path arraylist
57+
// find the path and store it in path LinkedList
5858
if(path == null){
5959
this.search();
6060
}
6161
return path;
6262
}
6363

6464

65-
public void setAllPath2D(ArrayList<ArrayList<Tile>> allPath2D) {
65+
public void setAllPath2D(LinkedList<LinkedList<Tile>> allPath2D) {
6666
this.allPath2D = allPath2D;
6767
}
6868

69-
public ArrayList<ArrayList<Tile>> getAllPath2D() {
69+
public LinkedList<LinkedList<Tile>> getAllPath2D() {
7070
return allPath2D;
7171
}
7272

A2_Q1/src/Game.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The robot character (an ant) should find its way from the start point (home) to
3333
import java.awt.event.MouseListener;
3434
import java.io.File;
3535
import java.io.IOException;
36-
import java.util.ArrayList;
36+
import java.util.LinkedList;
3737
import javax.swing.Timer;
3838
import javax.imageio.ImageIO;
3939
import javax.swing.JButton;
@@ -47,7 +47,7 @@ public class Game extends JPanel implements MouseListener {
4747

4848
private Ant ant;
4949
private Tile[][] tiles;
50-
private ArrayList<Tile> tobeDrawn;
50+
private LinkedList<Tile> tobeDrawn;
5151
private boolean startMovingAnt;
5252

5353
private Tile startTile;
@@ -91,7 +91,7 @@ public Game() {
9191
startTile = null;
9292
goalTile = null;
9393
startMovingAnt = false;
94-
tobeDrawn = new ArrayList<Tile>();
94+
tobeDrawn = new LinkedList<Tile>();
9595
}
9696

9797
private void loadFoodImg() {
@@ -335,7 +335,7 @@ private Tile fetchTile(int mouseX, int mouseY) {
335335
public void delayPaint() {
336336
int delay = 100; // 1 second delay
337337
Timer timer = new Timer(delay, new ActionListener() {
338-
ArrayList<ArrayList<Tile>> allPath2D = ant.getAllPath2D();
338+
LinkedList<LinkedList<Tile>> allPath2D = ant.getAllPath2D();
339339
Tile subNodeToBeDrawn = null;
340340
Tile mainNode;
341341

@@ -357,7 +357,7 @@ public void actionPerformed(ActionEvent e) {
357357
}
358358

359359
// add main node which will stay there parmanently to be drawn.
360-
ArrayList<Tile> oneIterationArray = allPath2D.get(0);
360+
LinkedList<Tile> oneIterationArray = allPath2D.get(0);
361361

362362
if (oneIterationArray.size() > 0 && addMainNode) {
363363
mainNode = oneIterationArray.get(0);
@@ -398,7 +398,7 @@ public void animateAnt() {
398398
if (startMovingAnt) {
399399
double speed = 1.5;
400400
int delay = 10; // 0.1 second delay
401-
ArrayList<Tile> path = ant.getPath();
401+
LinkedList<Tile> path = ant.getPath();
402402

403403
// set the ant to the start location
404404
ant.setX(path.get(path.size() - 1).getX() * TILE_SIZE);
@@ -473,16 +473,16 @@ protected void paintComponent(Graphics g) {
473473
drawPath(g, TILE_SIZE, tobeDrawn);
474474
} // end paintComponent
475475

476-
public void drawPath(Graphics g, int tileSize, ArrayList<Tile> arrayList) {
476+
public void drawPath(Graphics g, int tileSize, LinkedList<Tile> LinkedList) {
477477

478-
if (arrayList != null && arrayList.size() > 1) {
478+
if (LinkedList != null && LinkedList.size() > 1) {
479479
g.setColor(Color.RED);
480480

481-
for (int i = 0; i < arrayList.size() - 1; i++) {
481+
for (int i = 0; i < LinkedList.size() - 1; i++) {
482482

483-
Tile current = arrayList.get(i);
483+
Tile current = LinkedList.get(i);
484484

485-
Tile next = arrayList.get(i + 1);
485+
Tile next = LinkedList.get(i + 1);
486486

487487
int x1 = (int) current.getX() * tileSize + tileSize / 2;
488488
int y1 = (int) current.getY() * tileSize + tileSize / 2;

0 commit comments

Comments
 (0)