Skip to content

ferhat-ramdani/magic-grid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

84 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Magic Grid: A Study in Pathfinding and Isometric Rendering

magic-grid

A maze with walls of varying thickness, three pathfinding algorithms racing to cross it, and an isometric 3D look — built entirely on Python's turtle module, which was never meant to do any of this. The pathfinding half is the "expected" part of the assignment; the rendering half turned into its own small research project, since turtle has no concept of depth, layering, or anything beyond flat 2D lines.

The Algorithmic Core

The project implements several routing algorithms to find optimal paths under different constraints.

Dijkstra's Algorithm

This is used to find the absolute shortest path in a graph with non-negative edge weights. In our grid, the "cost" of moving from one cell to another is determined by the thickness of the wall separating them. Dijkstra's algorithm systematically expands outwards to find the optimal path that requires the least effort to break through.

Bellman-Ford Algorithm

Bellman-Ford is provided as an alternative algorithm. It iteratively relaxes all edges in the graph, ensuring that the optimal path is discovered. This is particularly useful as a benchmark for accuracy and for graphs where different weight structures might be tested.

Greedy Best-First Search

We implemented a Greedy Best-First Search algorithm using the Manhattan Distance to the target node as the heuristic. This algorithm dynamically explores its neighbors in all four directions. It provides a fast, locally optimal path that contrasts with the systematic expansion of Dijkstra's algorithm.

Rendering and Visual Techniques

Visualizing the algorithms presented specific challenges, particularly when relying on Python's native turtle module to achieve an isometric aesthetic.

Fractal Topography and Continuous Walls

Instead of drawing standard straight grid lines, we implemented a fractal generation technique (get_fractal_points) that subdivides edges to give the walls a textured appearance.

  • The Challenge: Drawing walls continuously without creating overlapping areas or gaps at intersections.
  • The Solution: We rendered the floors and cutouts rather than the walls themselves. By generating the fractal polygons for the interior of each cell (the floor) and placing them over a solid background block, the walls naturally emerge as the negative space between the rooms.

Isometric Depth Illusion

To give the grid a 3D block appearance, the fractal boundaries of the rooms are mathematically projected downward and to the right (x + depth_x, y - depth_y). Drawing these projected polygons in darker, shaded colors before drawing the top canvas simulates isometric shadows.

Z-Index Management

  • The Challenge: We needed the path-routing animations to appear on the floor of the maze. Because Turtle draws sequentially, drawing the path last results in the line rendering over the 3D walls, breaking the depth effect.
  • The Solution: Turtle's API does not natively support Z-indexing. To resolve this, we accessed its underlying GUI framework, Tkinter (cv = t.Screen().getcanvas()), and tagged the Z-order position of the grid's top canvas (z_marker). During the path animation, we execute cv.tag_raise(path_item, z_marker) for every line segment. This correctly layers the path segment underneath the walls during the real-time drawing phase.

Getting Started

To get started with the Magic Grid project:

  1. Clone the project repository from GitHub:
    git clone https://github.com/ferhat-ramdani/The-Magic-Grid.git
  2. Install the required dependencies. Make sure you have Python installed on your machine.
    pip install turtle
  3. Navigate to the project directory:
    cd magic-grid
  4. Run the main Python file to execute the grid modeling program:
    python main.py

Usage

Once the program is running, interact with the grid through the graphical interface:

  • Drag and Drop: Click and drag the Start (Green) and End (Red) markers to position them anywhere on the grid.
  • Find Best Path: Click the "Find best path" button to trigger the algorithms.
  • Observe: Watch as the paths are simultaneously animated beneath the walls.
  • Regenerate: Click the "Regenerate Grid" button that appears below the dashboards to wipe the board and start a new layout.

Authors

About

An interactive maze solver and pathfinding visualizer built in Python, featuring 3D isometric rendering and dynamic graph algorithms.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages