-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithm Sketch(script)
More file actions
100 lines (79 loc) · 10.8 KB
/
Algorithm Sketch(script)
File metadata and controls
100 lines (79 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#Taking major hits in algorithm performance, turns out adding more functions to algorithms slows the visiting process of cells (overall taking longer to find a path),
turning the algorithms to classes may help me reduce loops (make it efficient). I might do that to compare performance in future updates .
# This Code is error free!
This Python script uses the Pygame library to visualize and compare Dijkstra's algorithm and A* algorithm for pathfinding in a grid-based environment. Here's a breakdown of the code:
(Additions- BFS ,DFS)
Program uses Pygame to visualize pathfinding algorithms on a grid. It sets up the grid, draws cells, and visualizes paths.
Algorithms like Dijkstra's, A*, DFS, and BFS are implemented. Users can interactively set start and end points, add obstacles, and run algorithms through a user-friendly menu interface.
It's a fun way to explore and understand different pathfinding strategies!
Let's dive deeper into the functionality of each function:
1. `draw_grid()`: This foundational function sets up the visual representation of the grid system within the Pygame window.
It meticulously iterates through the dimensions of the screen, meticulously crafting the grid pattern by drawing horizontal and vertical lines, ensuring a clear and structured layout.
Role: Sets up the visual representation of the grid system within the Pygame window.
Implementation: Iterates through the dimensions of the screen and draws horizontal and vertical lines to create a structured grid pattern.
2. `draw_cell(color, position)`: A critical function in the visualization process, `draw_cell()` brings individual cells to life within the grid.
Given parameters for color and position, it utilizes Pygame's drawing capabilities to create a rectangular representation of the cell, ensuring each cell is distinctly identifiable with its specified color at the designated position.
Role: Draws an individual cell with a specified color at a given position within the grid.
Implementation: Utilizes Pygame's drawing capabilities to create a rectangular representation of the cell, ensuring it stands out with the designated color at the specified position.
#. `draw_cell_smooth(color, position)`: This function is an enhanced version of draw_cell() with smooth transitions.
It calculates the darkness factor based on the distance from the bottom-right corner and darkens the color accordingly to create an elevation effect.
Additionally, it implements smooth transitions by interpolating between the current color and the elevated color over a series of frames.
Role: Enhances the visual representation of individual cells by incorporating smooth transitions and an elevation effect, providing a more immersive and aesthetically pleasing experience.
Implementation: Calculates the darkness factor based on the distance from the bottom-right corner of the grid, creating an elevation effect where cells closer to the corner appear darker.
3. `visualize_path(path, color)`: This function adds an extra layer of insight into the algorithm's decision-making process by visually representing the path taken.
It meticulously traverses through each cell in the provided path, dynamically highlighting them with the specified color, offering users a real-time glimpse into the algorithm's progression.
Role: Provides a visual representation of the path taken by highlighting cells in the specified color.
Implementation: Iterates through each cell in the provided path and highlights them with the specified color, offering real-time insight into the algorithm's progress.
4. `reset()`: Serving as the reset mechanism, `reset()` ensures a clean slate for each algorithm execution. By filling the Pygame window with a black color and redrawing the grid,
it wipes away any remnants of previous visualizations, priming the environment for a fresh algorithm run or visualization.
Role: Resets the Pygame window, clearing any previous visualizations to prepare for a new algorithm run.
Implementation: Fills the Pygame window with a black color and redraws the grid, effectively wiping away any remnants of previous visualizations.
5. `dijkstra(start, end, obstacles)`: At the heart of this function lies the implementation of Dijkstra's algorithm,
meticulously navigating the grid to uncover the shortest path from the start to the end node. Leveraging a priority queue, it meticulously selects the next node to explore, ensuring an optimal path discovery process.
Role: Implements Dijkstra's algorithm to find the shortest path from the start to the end node.
Implementation: Utilizes a priority queue to explore nodes, ensuring the discovery of the optimal path while considering edge weights.
6. `astar(start, end, obstacles)`: With a fusion of Dijkstra's algorithm and heuristic estimation, `astar()` undertakes the challenge of finding the shortest path on the grid.
By employing a priority queue based on the sum of current cost and heuristic, it efficiently navigates the grid while factoring in the estimated distance to the goal.
Role: Implements the A* algorithm to find the shortest path while considering both the cost incurred and heuristic estimate to the goal.
Implementation: Utilizes a priority queue based on the sum of current cost and heuristic, ensuring an efficient exploration of the grid.
7. `dfs(start, end, obstacles)`: This function embarks on a journey through the grid using Depth-First Search (DFS). It bravely traverses as far as possible along each branch,
backtracking only when faced with a dead-end, ultimately discovering the end node or exhaustively exploring all feasible paths.
Role: Conducts Depth-First Search (DFS) to explore the grid, backtracking when necessary to uncover feasible paths.
Implementation: Utilizes a stack to traverse as far as possible along each branch, exploring unvisited nodes until reaching the end or exhausting all options.
8. `bfs(start, end, obstacles)`: Embodying the essence of Breadth-First Search (BFS), this function systematically explores the grid level by level,
emanating from the start node. Its steadfast approach guarantees the discovery of the shortest path upon reaching the end node, making it an ideal candidate for optimal solutions.
Role: Performs Breadth-First Search (BFS) to systematically explore the grid level by level, ensuring the discovery of the shortest path.
Implementation: Utilizes a queue to explore neighboring nodes level by level, guaranteeing the shortest path upon reaching the end node.
9. `heuristic(point, goal)`: As the backbone of A* algorithm's guidance system, this function calculates a heuristic value critical for navigating towards the goal node.
Through the meticulous computation of the Manhattan distance between the current point and the goal, it provides a valuable estimate of the remaining distance to the goal.
Role: Computes a heuristic estimate to guide the A* algorithm towards the goal node efficiently.
Implementation: Calculates the Manhattan distance between the current point and the goal, providing an estimate of the remaining distance to the goal.
10. `neighbors(cell)`: Acting as the gatekeeper to the grid, `neighbors()` identifies the valid neighboring cells for a given cell.
By meticulously considering adjacent cells in the four cardinal directions, it ensures the algorithm's exploration remains confined within the boundaries of the grid.
Role: Identifies valid neighboring cells for a given cell within the grid.
Implementation: Considers adjacent cells in the four cardinal directions, ensuring the exploration remains within the grid boundaries.
11. `remove_obstacle(cell, obstacles)`: When obstacles need to be vanquished from the grid, this function steps up to the challenge.
It orchestrates the removal of the obstacle from the set of obstacles, meticulously clearing the cell by redrawing it with a black color, and meticulously updating the surrounding grid lines to conceal the erstwhile obstacle.
Role: Removes an obstacle from the grid and updates the visualization accordingly.
Implementation: Clears the obstacle from the set of obstacles, redraws the cell with a black color, and updates surrounding grid lines to conceal the removed obstacle.
12. `generate_random_maze(obstacles)`: Infusing a dash of unpredictability into the grid, this function breathes life into a maze with obstacles.
It meticulously traverses each cell in the grid, making calculated random decisions on whether to add an obstacle, ultimately sculpting a maze-like structure that puts the algorithms to the test.
Role: Generates a random maze with obstacles to provide varying challenges for the algorithms.
Implementation: Traverses each cell in the grid and randomly decides whether to add an obstacle, creating a maze-like structure that tests the algorithms' capabilities.
13. `create_menu()`: This function serves as the gateway to user interaction, crafting a visually appealing menu interface using Tkinter.
It empowers users to choose their preferred algorithm and fine-tune visualization settings, providing a seamless and intuitive means to interact with the program.
Role: Constructs a graphical menu interface using Tkinter for users to interact with the program.
Implementation: Provides options for selecting algorithms, setting visualization settings, and controlling the execution flow, enhancing user engagement and experience.
14. `run_algorithm(selected_algorithm, create_maze)`: As the conductor orchestrating the symphony of algorithms,
this function takes charge of executing the selected algorithm. It adeptly manages user inputs, sets the stage for algorithmic exploration, and navigates the execution process, ensuring a fluid and immersive user experience.
Role: Executes the selected algorithm based on user inputs and manages the algorithmic exploration process.
Implementation: Orchestrates the execution of the chosen algorithm, handles user interactions, and ensures a seamless and immersive user experience throughout the exploration.
15. `main()`: Serving as the cornerstone of the program's architecture, `main()` lays the foundation for user interaction.
By initializing the menu interface through `create_menu()`, it ignites the spark of user engagement, inviting them to embark on a journey of algorithmic exploration and visualization on the grid.
Role: Acts as the entry point for the program, initializing the menu interface and facilitating user interaction.
Implementation: Invokes the create_menu() function to start the program, prompting users to select algorithms and interact with the grid for algorithmic exploration and visualization.
Learned a new word "meticulous" recently
Note: Make sure to have the Pygame library installed (pip install pygame) before running this script.
PS: I plan on adding new algos and some new functions(maze generation, grid resizing etc) that may change or make my previous codes/descs obsolete.
(I want a new GUI pls help)
# This code is all about jazzing up individual cells with colors. 🖌️🎨