Skip to content

Commit 6bb2279

Browse files
committed
simulation works. Check if i can fix heap crash
0 parents  commit 6bb2279

File tree

9 files changed

+1127
-0
lines changed

9 files changed

+1127
-0
lines changed

A2_Q1/.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"java.project.sourcePaths": ["src"],
3+
"java.project.outputPath": "bin",
4+
"java.project.referencedLibraries": [
5+
"lib/**/*.jar"
6+
]
7+
}

A2_Q1/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[Ant](https://www.pngegg.com/en/png-zblks)
2+
[food](https://www.pngegg.com/en/png-medpx)

A2_Q1/ant.png

123 KB
Loading

A2_Q1/food.png

4.27 KB
Loading

A2_Q1/src/AStarSearch.java

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics;
5+
import java.awt.Graphics2D;
6+
import java.util.ArrayList;
7+
8+
import java.util.PriorityQueue;
9+
10+
import javax.swing.JPanel;
11+
12+
13+
public class AStarSearch extends JPanel {
14+
15+
private Tile[][] tiles;
16+
private Tile start;
17+
private Tile goal;
18+
private Ant ant;
19+
private ArrayList<ArrayList<Tile>> allPath2D = new ArrayList<ArrayList<Tile>>();
20+
21+
public AStarSearch(Tile[][] tiles, Ant ant, Tile start, Tile goal) {
22+
this.tiles = tiles;
23+
this.start = start;
24+
this.goal = goal;
25+
this.ant = ant;
26+
}
27+
28+
public ArrayList<Tile> search() {
29+
30+
ArrayList<Tile> output = new ArrayList<>();
31+
ArrayList<Tile> closedSet = new ArrayList<>();
32+
33+
// create openSet and add start to it
34+
PriorityQueue<Tile> openSet = new PriorityQueue<Tile>();
35+
36+
// add start to openSet
37+
openSet.add(start);
38+
39+
boolean foundGoal = false;
40+
boolean noPath = false;
41+
42+
43+
// while openSet is not empty
44+
while(openSet.size() > 0 && foundGoal == false && noPath == false){
45+
46+
// check if first element in openSet is goal
47+
Tile current = openSet.poll();
48+
// System.out.println("current: " + current);
49+
ArrayList<Tile> row = new ArrayList<>();
50+
allPath2D.add(row);
51+
row.add(current);
52+
// System.out.println();
53+
// System.out.println("Removing Main Grid ["+current.getX()+"]["+current.getY()+"] F:" + current.getF() + " G:" + current.getG() + " H:" + current.getH());
54+
55+
if(current.equals(goal) && noPath == false){
56+
// reconstruct path
57+
output = reconstructPath(current);
58+
foundGoal = true;
59+
// ant.stopSearch();
60+
61+
} else {
62+
// remove current from openSet and add to closedSet
63+
// openSet.remove(current);
64+
closedSet.add(current);
65+
66+
// get neighbors of current/ outgoing connections
67+
PriorityQueue<Tile> neighbors = getNeighbors(current);
68+
69+
// printQueue(neighbors);
70+
71+
// for each neighbor of current, evaluate cost
72+
while(neighbors.size() > 0){
73+
Tile currNeighbor = neighbors.poll();
74+
75+
// System.out.println("--Polled Grid ["+currNeighbor.getX()+"]["+currNeighbor.getY()+"] F:" + currNeighbor.getF() + " G:" + currNeighbor.getG() + " H:" + currNeighbor.getH());
76+
77+
// if neighbor is not in closedSet and not obstacle, then evaluate cost
78+
if(!closedSet.contains(currNeighbor) && !currNeighbor.isObstacle()){
79+
row.add(currNeighbor);
80+
double tempCost = current.getG() + currNeighbor.getCost();
81+
82+
// System.out.println("tempCost: " + tempCost + " gCost: " + current.getG() + " current: " + current.getCost());
83+
84+
boolean betterPath = false;
85+
86+
// check for a shorter route
87+
if(!openSet.contains(currNeighbor)){
88+
// if neighbor is not in openSet, add to openSet and add g cost
89+
currNeighbor.setG(tempCost);
90+
betterPath = true;
91+
92+
// System.out.println("ADDING Grid ["+currNeighbor.getX()+"]["+currNeighbor.getY()+"] F:" + currNeighbor.getF() + " G:" + currNeighbor.getG() + " H:" + currNeighbor.getH());
93+
94+
} else {
95+
// openSet contains neighbor, check if shorter route
96+
97+
if (tempCost < currNeighbor.getG()){
98+
// found a shorter route, update g cost
99+
openSet.remove(currNeighbor);
100+
currNeighbor.setG(tempCost);
101+
openSet.add(currNeighbor);
102+
betterPath = true;
103+
104+
}
105+
}
106+
107+
// set neighbor's heuristic estimated cost to goal
108+
if(betterPath){
109+
currNeighbor.setH(getHeuristic(currNeighbor, goal));
110+
111+
// set neighbor's f
112+
currNeighbor.setF(currNeighbor.getG() + currNeighbor.getH());
113+
114+
// record previous node
115+
currNeighbor.setCameFrom(current);
116+
117+
}
118+
119+
120+
// add neighbor to openSet if not already in it
121+
if(!openSet.contains(currNeighbor)){
122+
// System.out.println("ADDING Grid ["+currNeighbor.getX()+"]["+currNeighbor.getY()+"] F:" + currNeighbor.getF() + " G:" + currNeighbor.getG() + " H:" + currNeighbor.getH());
123+
openSet.add(currNeighbor);
124+
125+
}
126+
127+
128+
// System.out.println("--Polled Grid ["+currNeighbor.getX()+"]["+currNeighbor.getY()+"] F:" + currNeighbor.getF() + " G:" + currNeighbor.getG() + " H:" + currNeighbor.getH());
129+
130+
131+
// update GUI after each iteration of the search algorithm
132+
133+
} // end if statement
134+
// System.out.println(" Grid ["+currNeighbor.getX()+"]["+currNeighbor.getY()+"] F:" + currNeighbor.getF() + " G:" + currNeighbor.getG() + " H:" + currNeighbor.getH());
135+
136+
} // end while loop
137+
138+
139+
140+
141+
} // end else(not goal)
142+
143+
// if openSet is empty, then no path
144+
if(openSet.size() == 0){
145+
System.out.println("No path found");
146+
noPath = true;
147+
}
148+
149+
150+
151+
} // end main while loop
152+
153+
ant.setAllPath2D(allPath2D);
154+
// System.out.println("Grid [1][0] F:" + tiles[1][0].getF() + " G:" + tiles[1][0].getG() + " H:" + tiles[1][0].getH());
155+
// System.out.println("Grid [1][1] F:" + tiles[1][1].getF() + " G:" + tiles[1][1].getG() + " H:" + tiles[1][1].getH());
156+
157+
return output;
158+
}
159+
160+
161+
private double getHeuristic(Tile currNeighbor, Tile goal) {
162+
163+
// manhattan distance between current neighbor and goal
164+
int dx = Math.abs(currNeighbor.getX() - goal.getX());
165+
int dy = Math.abs(currNeighbor.getY() - goal.getY());
166+
return dx + dy;
167+
}
168+
169+
private PriorityQueue<Tile> getNeighbors(Tile tile) {
170+
171+
PriorityQueue<Tile> neighbors = new PriorityQueue<>();
172+
173+
// get the indexes of the tile from the neighbors 2d array
174+
175+
int x = tile.getX();
176+
int y = tile.getY();
177+
178+
// add neighbours including diagonals
179+
// top left
180+
if(x > 0 && y > 0){
181+
neighbors.add(tiles[x-1][y-1]);
182+
}
183+
// top
184+
if(y > 0){
185+
neighbors.add(tiles[x][y-1]);
186+
}
187+
// top right
188+
if(x < tiles.length-1 && y > 0){
189+
neighbors.add(tiles[x+1][y-1]);
190+
}
191+
// right
192+
if(x < tiles.length-1){
193+
neighbors.add(tiles[x+1][y]);
194+
}
195+
// bottom right
196+
if(x < tiles.length-1 && y < tiles[0].length-1){
197+
neighbors.add(tiles[x+1][y+1]);
198+
}
199+
// bottom
200+
if(y < tiles[0].length-1){
201+
neighbors.add(tiles[x][y+1]);
202+
}
203+
// bottom left
204+
if(x > 0 && y < tiles[0].length-1){
205+
neighbors.add(tiles[x-1][y+1]);
206+
}
207+
// left
208+
if(x > 0){
209+
neighbors.add(tiles[x-1][y]);
210+
}
211+
212+
return neighbors;
213+
}
214+
215+
public ArrayList<Tile> reconstructPath(Tile current) {
216+
ArrayList<Tile> path = new ArrayList<>();
217+
218+
while (current != null) {
219+
path.add(current);
220+
221+
// move to the previous node
222+
current = current.getCameFrom();
223+
// System.out.println("Path: (" + current.getX() + ", " + current.getY()+")");
224+
}
225+
226+
return path;
227+
}
228+
229+
}

A2_Q1/src/Ant.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* -----------------------------------------------------------------------------
2+
Author: Saif Mahmud
3+
Date: 2023-22-07
4+
Course: COMP 452
5+
Student ID: 3433058
6+
Assignment: 2
7+
Part: 1
8+
*/
9+
10+
import java.awt.Graphics;
11+
import java.awt.Image;
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.util.ArrayList;
15+
16+
import javax.imageio.ImageIO;
17+
18+
public class Ant {
19+
20+
private int antX;
21+
private int antY;
22+
private Tile start;
23+
private Tile goal;
24+
private Tile[][] tiles;
25+
private Image antImage;
26+
private ArrayList<Tile> path;
27+
private Graphics g;
28+
29+
private AStarSearch aStarSearch;
30+
private ArrayList<ArrayList<Tile>> allPath2D = new ArrayList<ArrayList<Tile>>();
31+
32+
public Ant(Tile start, Tile goal, int tileSize, Tile[][] tiles) {
33+
this.start = start;
34+
this.goal = goal;
35+
this.tiles = tiles;
36+
loadAntImg();
37+
antX = start.getX()*tileSize;
38+
antY = start.getY()*tileSize;
39+
}
40+
41+
private void loadAntImg() {
42+
try {
43+
antImage = ImageIO.read(new File("ant.png"));
44+
} catch (IOException e) {
45+
e.printStackTrace();
46+
}
47+
}
48+
49+
public void draw(Graphics g, int tileSize) {
50+
g.drawImage(antImage, antX, antY, tileSize, tileSize, null);
51+
}
52+
53+
public void search() {
54+
aStarSearch = new AStarSearch(tiles, this, start, goal);
55+
path = aStarSearch.search();
56+
}
57+
58+
public ArrayList<Tile> getPath() {
59+
// find the path and store it in path arraylist
60+
61+
if(path == null){
62+
this.search();
63+
}
64+
return path;
65+
}
66+
67+
68+
public void setAllPath2D(ArrayList<ArrayList<Tile>> allPath2D) {
69+
this.allPath2D = allPath2D;
70+
}
71+
72+
public ArrayList<ArrayList<Tile>> getAllPath2D() {
73+
return allPath2D;
74+
}
75+
76+
public int getX() {
77+
return antX;
78+
}
79+
80+
public int getY() {
81+
return antY;
82+
}
83+
public void setX(int x) {
84+
antX = x;
85+
}
86+
87+
public void setY(int y) {
88+
antY = y;
89+
}
90+
91+
}// end class Ant

A2_Q1/src/App.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* -----------------------------------------------------------------------------
2+
Author: Saif Mahmud
3+
Date: 2023-22-07
4+
Course: COMP 452
5+
Student ID: 3433058
6+
Assignment: 2
7+
Part: 1
8+
Description: Main class to run the game
9+
*/
10+
11+
import javax.swing.JFrame;
12+
13+
public class App {
14+
public static void main(String[] args) throws Exception {
15+
Game game = new Game();
16+
JFrame frame = new JFrame("A2_P1");
17+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
18+
frame.setSize(900, 700);
19+
20+
frame.add(game);
21+
// frame.pack();
22+
frame.setVisible(true);
23+
}
24+
}

0 commit comments

Comments
 (0)