This repository contains the final project for the “An Introduction to Programming using Python” course from Coursera, completed as part of my B.Tech in Computer Science and Engineering curriculum.
The goal of this project is to demonstrate a robust, from-scratch implementation of a complex pathfinding algorithm. The script builds a "brain" for a robot to find the shortest possible path from a starting point ('R') to a goal ('G') in a 2D text-based grid, navigating around obstacles ('X').
The project's two primary objectives are:
- 
To implement the A (A-Star) search algorithm* using foundational Python data structures. 
- 
To provide a step-by-step console animation of the robot's movement along the calculated optimal path. 
robot-pathfinding-using-python/
│
├── README.md
└── robot.py
The repository is built around a single, self-contained Python script (robot.py) that includes all helper functions, the A* algorithm logic, and the final visualization.
git clone https://github.com/Vishwajit1610/robot-pathfinding-using-python
cd robot-pathfinding-using-python
python -m venv venv
source venv/bin/activate    # For Linux/Mac
venv\Scripts\activate       # For Windows
This project uses only Python's built-in libraries (heapq and time), so no requirements.txt file or external package installation is necessary.
Simply run the robot.py file to see the pathfinding and animation in your terminal:
python robot.py
The "brain" of the robot is a complete implementation of the A* search algorithm. It efficiently finds the shortest path by always exploring the most promising square on the grid. It makes this decision using a heuristic formula:
f(n) = g(n) + h(n)
- 
g(n): The true cost (number of steps) from the start to the current squaren.
- 
h(n): The heuristic, or estimated cost, fromnto the goal. This project uses the Manhattan distance for a fast and effective estimate.
- 
heapq(Priority Queue): Theopen_setlist is managed as a priority queue to ensure the algorithm always processes the square with the lowestf_scorefirst.
- 
g_score(Dictionary): Tracks the "pedometer" cost (theg(n)value) for each explored square.
- 
came_from(Dictionary): Stores the "breadcrumb trail" used to reconstruct the final path by tracing it backward from the goal.
The "legs" of the robot. Once the A* algorithm finds the optimal path, a dedicated animation loop takes over to provide a step-by-step visualization:
- 
The initial grid state is displayed. 
- 
The loop iterates through the calculated path. 
- 
In each step, the robot's previous position is updated (leaving a ●trail), and the robot'R'is drawn in its new position.
- 
The grid is re-printed, and time.sleep()is called to create a visible, step-by-step movement.
- 
Practical, from-scratch implementation of the A pathfinding algorithm*. 
- 
Proficient use of heapqto manage a priority queue for an efficient search.
- 
Advanced use of dictionaries for cost-tracking ( g_score) and path reconstruction (came_from).
- 
Strong procedural programming practices by separating logic into helper functions ( is_move_valid,reconstruct__path, etc.).
- 
Creating dynamic console-based animations and visualizations using printandtime.sleep.
This project serves as a strong foundation for more advanced work:
- 
Graphical Interface: Rebuild the visualization using a library like Pygame or Tkinter to create a graphical, real-time representation (as seen in the project's inspiration image). 
- 
Weighted Grids: Introduce "difficult terrain" (e.g., 'water' or 'mud') that costs more than 1 step to move through, testing the full power of the A* algorithm. 
- 
Dynamic Obstacles: Add logic for the robot to re-calculate its path if a new 'X' appears in its way. 
Vishwajit Mohol
B.Tech CSE | 2nd Year Student
Course: An Introduction to Programming using Python -> Coursera (University Curriculum)
Building mastery in algorithms and Python through first principles and structured learning.
This project is licensed under the MIT License. Feel free to use and adapt the material with proper attribution.
MIT License
Copyright (c) 2025 Vishwajit Mohol
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.