This is a sandbox for you to implement and visualize anything and everything that requires a grid of squares. Already implemented features: Pathfinding visualization, maze generation and the game of life.
These instructions will help you set up the project on your local machine.
- Python 3.x
- Required Python packages (install them using
pip install <package-name>
):pygame
for visualization
-
Clone the repository to your local machine:
git clone https://github.com/your-username/pathfinding-algorithms.git
-
Navigate to the project directory:
cd pathfinding-algorithms
-
Install the required Python packages:
pip install pygame
-
Run the main script:
python main.py
-
Follow on-screen instructions to draw obstacles and choose the start and end points.
-
Choose the algorithm you want to visualize.
-
Watch the visualization of the selected pathfinding algorithm.
BFS is an algorithm for traversing or searching tree or graph data structures. It explores all the vertices at the present depth prior to moving on to vertices at the next depth level.
A* is a popular pathfinding algorithm that uses a combination of the cost to reach the node (known as g-score
) and the estimated cost to the goal from the current node (known as h-score
). It uses a priority queue to explore nodes in order of their total cost (f-score
).
DFS is an algorithm for traversing or searching tree or graph data structures. It explores as far as possible along each branch before backtracking.
DFS for weighted graphs. It looks at child nodes with connected edges to the parent node and picks the child node with the lowest Euclidean distance to the end position. If a parent node has no valid child nodes, it returns to the previous node.
DFS for unweighted graphs. It looks at child nodes with connected edges to the parent node and picks the child node with the lowest Euclidean distance to the end position. If a parent node has no valid child nodes, it returns to the previous node.
Represents a square in the grid.
Attributes:
row
andcol
: Row and column indices of the square.x
andy
: Coordinates of the square.width
: Width of the square.total_rows
: Total number of rows in the grid.colour
: Color of the square.value
: A numerical value associated with the square.neighbours
: List of neighboring squares.root
: The root node of the square (used in pathfinding algorithms).
Represents a button for user interaction.
Attributes:
pressed
: Indicates whether the button is pressed.elevation
anddynamic_elevation
: Control button elevation.original_y_pos
: Original y-position of the button.top_rect
andbottom_rect
: Rectangles representing the button.top_color
andbottom_color
: Colors of the button.text_surf
andtext_rect
: Surface and rectangle for the button's text.
Methods:
draw()
: Draws the button.return_click()
: Returns whether the button is clicked.check_click()
: Checks if the button is clicked.
Returns a list of neighboring coordinates for a given position (i, j)
.
Checks if a given coordinate (i, j)
is within the grid boundaries.
Prints the neighbors of each square in the grid.
Save and load the grid state using Pickle.
Draws the grid lines on the game window.
Draws the entire grid, including squares and grid lines.
Converts mouse click position to grid coordinates.
Initializes the game by randomly drawing walls on the grid.
Resets the grid by clearing previously drawn paths and walls.
Handles button interactions and returns whether a button is clicked.
The main function that sets up the game, handles user input, and runs pathfinding algorithms based on user interactions.
-
find_neighbours_maze(i, j): Returns a list of neighboring coordinates for a given position (i, j).
-
is_valid(i, j, m_row): Checks if a given coordinate (i, j) is within the grid boundaries.
- Reset all squares in the grid to walls.
- Pick a random starting square (currently set at a fixed position for reproducibility).
- Make the starting square a passage.
- Create a list called frontier to store frontier cells.
- Create a list called path and add the starting square to it.
- Loop until the frontier list is empty:
- Pick a random square from the path.
- Find the neighbors of the square.
- Extend the frontier list with the neighbors.
- If there are frontier squares:
- Pick a random frontier square.
- Turn the square into a passage and remove it from the frontier.
- Find the neighbors of the chosen square and add them to the frontier.
- Add the chosen square to the path.
- Clear the neighbors list for each square in the grid.
The function returns the modified grid with the generated maze.