Skip to content

Commit 3c230b7

Browse files
committed
updated gitignore cache
1 parent fd32604 commit 3c230b7

File tree

9 files changed

+1083
-0
lines changed

9 files changed

+1083
-0
lines changed

.gitignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Compiled class file
2+
*.class
3+
4+
"java.codeGeneration.generateJavadocCustomTagValueValueValueValueTag": true,
5+
# Log file
6+
*.log
7+
8+
# BlueJ files
9+
*.ctxt
10+
11+
"java.codeGeneration.generateJavadocCustomTagValueValueValueValueDocRoot": true,
12+
# Mobile Tools for Java (J2ME)
13+
.mtj.tmp/
14+
15+
# Package Files #
16+
*.jar
17+
*.war
18+
*.nar
19+
*.ear
20+
21+
"java.codeGeneration.generateJavadocCustomTagValueValueValueValueInheritDoc": true,
22+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23+
hs_err_pid*
24+
25+
# Visual Studio Code files
26+
.vscode/*

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Ant Path Finding using A* Algorithm
2+
3+
[![Hits](https://hits.sh/github.com/vmsaif/ant-path-finding-using-A-Star-algorithm.svg?label=Visits&color=100b75)](https://hits.sh/github.com/vmsaif/ant-path-finding-using-A-Star-algorithm/)
4+
5+
## Logic and Design of Program
6+
7+
The program uses the A* algorithm to find the shortest path from the ant to the food. The ant is the start and the food is the goal. The ant can move in 8 directions.
8+
9+
When the game starts, the user is asked to select
10+
11+
-the start and the goal cells
12+
-the obstacle cells
13+
-the nature of the terrain for each cell between
14+
- Open Terrain
15+
- Grassland
16+
- Swampland
17+
- Obstacles
18+
19+
After the user has selected the cells, the program calculates the shortest path from the ant to the food. The program uses the A* algorithm to find the shortest path. The A* algorithm uses a heuristic function to find the shortest path. The heuristic function used in this program is the Manhattan distance. The program shows the search evaluation of the A* algorithm. When the path is found, the ant starts moving from the start cell to the goal cell.
20+
21+
## Compiling and Running
22+
23+
### Compiling
24+
The game uses a simple state machine to manage the different states of the game.
25+
26+
- Run the command `javac -d bin src/*.java` to compile the Java files in the `src` directory to `bin/` directory.
27+
- Run the command `java -cp bin/ App` to run the main game.
28+
29+
## Resources
30+
31+
### Images
32+
- [Ant Image](https://www.pngegg.com/en/png-zblks)
33+
- [Food Image](https://www.pngegg.com/en/png-medpx)
34+
35+
### Bugs:
36+
I have not found any bugs in the program yet. If you find any, please let me know.
37+
38+
39+

ant.png

123 KB
Loading

food.png

4.27 KB
Loading

src/AStarSearch.java

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

src/Ant.java

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

src/App.java

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

0 commit comments

Comments
 (0)