Simple Recursive Maze

July 26th, 2010

Here is a simple recursive maze generator I wrote to help a friend learn a little more about how recursion works. I figured I’d post it here as well.

#! /usr/bin/env python

import random

# Width and height of maze needs to be an even number because of walls
WIDTH = 60
HEIGHT = 20

# The change in x and y for all four directions
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)

def recursive_maze(maze, x=0, y=0, dirx=0, diry=0):
	# Check if current position is inside maze boundry
    if 0 <= y < len(maze):
        if 0 <= x < len(maze[0]):
			# Check if this position can have a new hallway going to it
            if maze[y][x] == "#":
				# Make this position into a hallway
                maze[y][x] = " "

                # Connect this position with previous position
                maze[y-diry][x-dirx] = " "

                # Create direction list and randomize it's order
                directions = [UP, DOWN, LEFT, RIGHT]
                random.shuffle(directions)

                # Follows each direction when the callstack returns here
                for dx, dy in directions:
					# Go down this current direction
                    recursive_maze(maze, x + dx*2, y + dy*2, dx, dy)

def draw_maze(maze):
	# Draw top wall
    print "#" * (len(maze[0])+1)
    # Draw each line with left wall added in
    for line in maze:
        print "#" + "".join(line)

# Initialize the maze as am array of arrays filled with wall spaces
maze = [["#" for j in xrange(WIDTH)] for i in xrange(HEIGHT)]

# Start making the maze
recursive_maze(maze)

# Finally, draw it
draw_maze(maze)

The output of this program will look similar to this:

#############################################################
# #       #     #           #           #               #   #
# ##### # # ### # ######### ####### ### # ########### # # ###
#   #   # #   # #   #     #   #   # # # # #           # #   #
### # ### ### # ### ### # ### # # # # # ### ########### ### #
#   #   #     #   # #   # #   # #   # #   # #     #   # #   #
# ##### ######### # # ### # ### ##### ### # # ### # # # # ###
# #     # #     #   #   # # #   #     #   #     # # # # #   #
# # ##### # # ######### ### # ### ### # ######### # ### ### #
# # #       #         #   #   #     # #         # # #   #   #
# # ### ########### # ### ######### ########### # # # ### ###
# #   #   #       # #     #         #           # # # #     #
# # # ### # ####### ####### ### ### # ########### # # # ### #
# # # # #   #       #     #   #   # # #           # # # #   #
# # # # # ### ##### # ### ### ### ### ########### # # ### # #
# # # #   #   #     # # #     #   #   #         # #   #   # #
# ### # ### ##### ### # ####### ### ### ####### # # ### ### #
# #   # # # #   # #   #       #     #   #     #   #     #   #
# # ### # # # # ### ####### # ######### # ### ########### ###
#   #     #   #             #           #   #               #
#############################################################