This Java project visualizes the pathfinding process of the A* algorithm on a grid. The program finds the shortest path from a starting point to a goal point, marking each step in the path and displaying the result. The grid layout and path progression are printed to the console, and an image representation is generated for easier visualization.
- A* Algorithm Implementation: Efficient pathfinding using the A* algorithm, balancing cost and distance to find the shortest path.
- Console Visualization: Outputs the grid to the console, with symbols indicating path (
-
), obstacles (*
), and free space. - Image Representation: Generates an image that visualizes the path from the starting node to the goal.
- Clone the repository:
git clone https://github.com/yourusername/AStarVisualization.git
- Open the project in a Java IDE (e.g., IntelliJ IDEA, Eclipse).
- Ensure Java Development Kit (JDK) 11 or higher is installed.
-
Initialize a 2D array
arr
, where:0
represents open paths,1
represents obstacles, and- other values can be customized as needed.
-
Run the following main code block to calculate the path and display the visualization:
java
Copy code
`AStar route = new ArrayToAStar(arr).route; Node res = route.calculate();
// Outputs the path length and grid with the path marked System.out.println("Result path length: " + res.pathLength); new Image(arr);`
-
The program will output the grid with the path marked by
-
characters and obstacles by*
.
- Purpose: Implements the A* pathfinding algorithm.
- Key Attributes:
OPENED
andCLOSED
- Lists tracking nodes to evaluate and those already processed.goal
,start
- Starting and goal coordinates.
- Key Methods:
calculate()
- Main function that finds the shortest path, updating theOPENED
andCLOSED
lists until the goal is reached.
- Purpose: Represents each position in the grid, with properties like coordinates, path length, and parent nodes for backtracking.
- Key Methods:
findNeighbors()
- Identifies adjacent nodes for movement.traversable()
- Checks if a node is accessible.setParent()
- Links nodes to allow backtracking.
- Purpose: Converts a 2D array grid into the necessary AStar data structure, setting up initial nodes.
After running the program, the console will display the grid. Each symbol represents:
-
: Part of the calculated path*
: Obstacles0
: Free spaces
Sample output:
Copy code
0 0 0 * 0 0 - - - 0 0 * * - 0 0 - - - 0 0 0 0 0 0
- Modify the
arr
array to set up different grid configurations. - Change start and goal positions in
AStar
to explore different paths.
Distributed under the MIT License. See LICENSE
for more information.