1
0
Fork 0
mornie.org/content/blog/generating-maze-using-python.md
Daniele Tricoli 1716d2f6cc
All checks were successful
continuous-integration/drone/push Build is passing
Migrate to zola
2021-02-01 01:14:09 +01:00

1.6 KiB

+++ date = 2007-08-02T03:11:49+01:00 title = "Generating Maze using Python" aliases = [ "blog/2007/08/02/Generating-Maze-using-Python", "blog/2007/08/02/generating-maze-using-python" ] [taxonomies] tags = ["python"] +++

Do you like mazes?

eriol@mornie:~$ python maze.py
+--+--+--+--+--+--+--+--+--+--+
|  |           |        |     |
+  +  +  +--+  +  +--+  +  +  +
|  |  |  |     |     |     |  |
+  +  +  +--+--+--+  +--+--+  +
|  |  |     |        |     |  |
+  +  +--+  +  +--+--+  +  +  +
|  |  |  |              |  |  |
+  +  +  +--+--+--+--+--+--+  +
|  |     |     |              |
+  +--+--+  +  +  +--+--+--+  +
|           |     |           |
+--+--+--+--+--+--+--+--+--+--+

After reading this good article, I decided to wrote some code to generate perfect maze. :D

The result is here: maze.py.

As you can see, Depth-First Search is not a complex algorithm:

def create(self):
    cell_stack = []
    while self.visited_cells < self.total_cells:
        neighbors = self.find_neighbors_with_walls(self.current_cell)
        if neighbors:
            new_cell = random.choice(neighbors)
            self.current_cell.destroy_wall(cell=new_cell)
            cell_stack.append(self.current_cell)
            self.current_cell = new_cell
            self.visited_cells += 1
        else:
            self.current_cell = cell_stack.pop()