Skip to content

Commit 79a896b

Browse files
committed
closes vmsaif#7, closes vmsaif#8, changed the positions of timer and counter
1 parent dfcf4a9 commit 79a896b

File tree

3 files changed

+87
-20
lines changed

3 files changed

+87
-20
lines changed

README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
[![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/)
44

5+
# About the Game
6+
7+
Dive into a world where you're not just a player, but a creator. In this interactive game, **you** design the maze, but here's the twist: Can you design a maze swiftly, using the least time possible, yet make our AI take the maximum time to find its way? The challenge? Make the AI ponder over your crafty creation. While our AI boasts of solving most mazes in a mere moment, we've added a graphic representation of the ant's journey to allow you to savor the AI's decision-making, one step at a time. The stakes? Your creativity versus AI's intelligence. Don your creator's cap, and let the games begin!
8+
9+
## Your Journey as a Creator
10+
11+
At the start of the game, you're presented with a blank canvas. Here, you get to wear the hat of a creator:
12+
13+
1. **Designate Start and Finish:** Decide where the ant starts its journey and where its food (goal) lies.
14+
2. **Craft the Terrain:** Mold the landscape by placing obstacles, open terrains, grasslands, or swamplands. Each terrain type presents a unique challenge to the AI. Grasslands make the ant take longer to cross than open terrains, and swamplands prolong its journey even more. Obstacles? They're the ultimate roadblock, making paths impassable.
15+
3. **The Ant's Quest:** Once you've crafted your masterpiece, sit back and watch the ant embark on its quest to find the goal. Behind the scenes is the formidable A* algorithm, aided by the Manhattan distance heuristic function. But you don't need to dive into these complexities; your main course is the visual spectacle of the ant navigating its way through the challenges you've set.
16+
17+
Note: You can even make it further challenging by limiting the count of each terrain type. For example, you can limit yourself from using the number of obstacles, grasslandsand, swamplands you can use. This will make the mage creation more challenging.
18+
519
## Logic and Design of Program
620

721
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.
@@ -10,7 +24,7 @@ When the game starts, the user is asked to select:
1024
- The start and the goal cells.
1125
- The obstacle cells.
1226
- The nature of the terrain for each cell between:
13-
- Open Terrain
27+
- Open Terrain
1428
- Grassland
1529
- Swampland
1630
- Obstacles

src/App.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,27 @@
55
*/
66

77
import javax.swing.JFrame;
8+
import javax.swing.UIManager;
9+
10+
import com.formdev.flatlaf.FlatLightLaf;
811

912
public class App {
1013
public static void main(String[] args) throws Exception {
11-
14+
15+
try {
16+
UIManager.setLookAndFeel(new FlatLightLaf());
17+
} catch (Exception ex) {
18+
System.err.println("Failed to initialize theme. Using fallback.");
19+
}
20+
1221
JFrame frame = new JFrame("Ant Path Finding with A* Algorithm");
1322
frame.setSize(1400, 800);
1423

1524
Game game = new Game(frame);
16-
25+
1726
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
1827
frame.add(game);
19-
28+
2029
frame.setVisible(true);
2130
}
2231
}

src/Game.java

+60-16
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ public class Game extends JPanel implements MouseListener, MouseMotionListener {
8989
private boolean antReached;
9090
protected boolean startTimer;
9191

92+
int iconSize; // Define the size of your icons
93+
int iconTextSpacing; // Spacing between icon and text
94+
int wordSpacing; // Spacing between each icon-count pair
95+
double iconScaling;
96+
97+
9298
// images
9399
private static Image antImage;
94100
private static Image foodImg;
@@ -108,7 +114,7 @@ public Game(JFrame frame) {
108114

109115
// set tile offset
110116
int xOffset = (int) ((frame.getWidth() - TILE_SIZE * NUM_COLS) / 2);
111-
int yOffset = (int) ((frame.getHeight() - TILE_SIZE * NUM_ROWS) / 2.5);
117+
int yOffset = (int) ((frame.getHeight() - TILE_SIZE * NUM_ROWS) / 1.8);
112118

113119
Tile.setxOffset(xOffset);
114120
Tile.setyOffset(yOffset);
@@ -136,6 +142,14 @@ public Game(JFrame frame) {
136142
add(buttonPanel, BorderLayout.NORTH);
137143
setPreferredSize(new Dimension(getPreferredSize().width + 200, getPreferredSize().height));
138144

145+
iconSize = 20; // Define the size of your icons
146+
iconTextSpacing = 5; // Spacing between icon and text
147+
wordSpacing = 25; // Spacing between each icon-count pair
148+
149+
terrainCountX = (int)(frame.getWidth()/2.4);
150+
terrainCountY = frame.getHeight()/15;
151+
iconScaling = 2.5;
152+
139153
startTimer = false;
140154
startClicked = false;
141155
startTile = null;
@@ -154,9 +168,7 @@ public Game(JFrame frame) {
154168
timerX = 100;
155169
timerY = frame.getHeight() - 100;
156170

157-
terrainCountX = frame.getWidth() - 300;
158-
terrainCountY = frame.getHeight() - 150;
159-
171+
160172
setTimerMageCreation();
161173
setTimerSolving();
162174
}
@@ -639,24 +651,56 @@ protected void paintComponent(Graphics g) {
639651
printIfNoPath(g);
640652

641653
// Draw the elapsed time
642-
g.setColor(Color.RED); // Sets the color to red.
643-
g.setFont(new Font("Courier New", Font.PLAIN, 20));
644-
g.drawString("Mage Create: " + elapsedTimeStringBeforeSearch, timerX, timerY);
645-
g.drawString("Solved Time: " + elapsedTimeStringAfterAnimation, timerX, timerY + 20);
646654

647-
// draw the counts of each terrain
648-
g.setColor(Color.BLACK);
649-
g.setFont(new Font("Courier New", Font.PLAIN, 20));
650-
g.drawString("Grid Settings: " + NUM_ROWS + " x " + NUM_COLS, terrainCountX, terrainCountY-20);
651-
g.drawString("Open Terrain: " + openTerrainCount, terrainCountX, terrainCountY + 0);
652-
g.drawString("Obstacle: "+ obstacleCount, terrainCountX, terrainCountY + 20);
653-
g.drawString("Swampland: " + swamplandCount, terrainCountX, terrainCountY + 40);
654-
g.drawString("Grassland: " + grasslandCount, terrainCountX, terrainCountY + 60);
655+
printTimer(g);
655656

657+
drawTileCount(g);
656658

657659

658660
} // end paintComponent
659661

662+
private void drawTileCount(Graphics g) {
663+
664+
// Draw the icons with their counts
665+
try {
666+
g.drawImage(obstacleImg, terrainCountX, terrainCountY, iconSize, iconSize, null);
667+
g.drawString("" + obstacleCount, terrainCountX + iconSize + iconTextSpacing, terrainCountY + iconSize-3);
668+
669+
g.drawImage(grasslandImg, terrainCountX + wordSpacing*3, terrainCountY, iconSize, iconSize, null);
670+
g.drawString("" + grasslandCount, terrainCountX + wordSpacing*3 + iconSize + iconTextSpacing, terrainCountY + iconSize-3);
671+
672+
g.drawImage(swamplandImg, terrainCountX + wordSpacing*6, terrainCountY, iconSize, iconSize, null);
673+
g.drawString("" + swamplandCount, terrainCountX + wordSpacing*6 + iconSize + iconTextSpacing, terrainCountY + iconSize-3);
674+
675+
//open terrain
676+
g.setColor(Color.WHITE);
677+
g.fillRect(terrainCountX + (int)(wordSpacing*8.5)+ iconSize, terrainCountY+3, (int) (Game.getTileSize()/iconScaling), (int) (Game.getTileSize()/iconScaling));
678+
g.setColor(Color.BLACK);
679+
g.drawRect(terrainCountX + (int)(wordSpacing*8.5) + iconSize, terrainCountY+3, (int) (Game.getTileSize()/iconScaling), (int) (Game.getTileSize()/iconScaling));
680+
g.setColor(Color.RED);
681+
g.drawString("" + openTerrainCount, terrainCountX + (int)(wordSpacing*9.25) + iconSize + iconTextSpacing, terrainCountY + iconSize-2);
682+
683+
} catch (NullPointerException e) {
684+
System.out.println("Image not found");
685+
}
686+
}
687+
688+
private void printTimer(Graphics g) {
689+
// draw a footer for the timer
690+
g.setColor(Color.WHITE);
691+
g.fillRect(0, frame.getHeight() - 70, frame.getWidth(), 100);
692+
693+
timerX = frame.getWidth() - 620;
694+
timerY = frame.getHeight() - 47;
695+
g.setColor(Color.RED); // Sets the color to red.
696+
g.setFont(new Font("Courier New", Font.PLAIN, 20));
697+
g.drawString("Creation Time: " + elapsedTimeStringBeforeSearch, timerX, timerY);
698+
g.setColor(Color.BLACK);
699+
g.drawString("|", timerX + 283, timerY);
700+
g.setColor(Color.RED);
701+
g.drawString("AI Solving Time: " + elapsedTimeStringAfterAnimation, timerX + 300, timerY);
702+
}
703+
660704
private void printIfNoPath(Graphics g) {
661705
if(noPath){
662706
// write no path found on the screen at the center with increased font size

0 commit comments

Comments
 (0)