[ create a new paste ] login | about

Link: http://codepad.org/JMt9E13T    [ raw code | output | fork ]

Python, pasted on Sep 9:
from random import shuffle

def generate(maze, x=1, y=1):
    dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    w, h = len(maze[0]), len(maze)

    maze[y][x] = ' '; shuffle(dirs)
    for dx, dy in dirs:
        nx, ny = (x + 2 * dx, y + 2 * dy)
        if (0 < nx < w-1 and 0 < ny < h-1) and maze[ny][nx] == '#':
            maze[y + dy][x + dx] = ' '
            generate(maze, nx, ny)
    return maze

def makemaze(width, height):
    maze = []
    for y in range(2 * height + 1):
        row = []; maze.append(row)
        for x in range(2 * width + 1):
            row.append('#')
    maze[-2][-1] = maze[1][0] = '>'
    return generate(maze)

for i in makemaze(25, 15): print ''.join(i)


Output:
###################################################
> #     #   #         #   #         #     #       #
# ### # # # ####### # # # ##### ### # ### # # ### #
#     #   #       # #   #     # # #   # # # #   # #
################# # ######### # # ##### # ##### # #
#             #   #     #   #   #     #   #     # #
# ########### # ### ### # ####### # ### ### ##### #
# #     #     # #     # # #       # #   #       # #
# ### # # # ### ####### # # ### ### # ######### ###
#     # # # #   #       #     #   # # #       #   #
######### # # ### ########### ### ### ### ### ### #
#   #     # #   # #         #   # #   #   #       #
# # # ### ##### # # ####### ##### # ### ######### #
# #   #   #     #   #   # #   #   # #           # #
# ######### ##### ### # # ### # ### # ######### # #
# #   #     # #   #   # #   # #     # #     #   # #
# # # # ##### # ### ### ### # ### ### # ##### ### #
#   # # #     #   # #       #   # #     #     #   #
# ### # # ####### # ####### ### # ####### #########
# #   # #     #   #     #     # #       #         #
### # # ##### # ##### # ####### ####### # ####### #
#   # #     #       # # #       #     # #     #   #
# ######### # ##### ### # # ##### ##### ##### # ###
#   #       # #   # #   # # #   #       #     #   #
### # ####### # # # # # # ### # ### ############# #
#   # #       # # #   # #     #   #     #         #
# ### ### ##### # ##### ##### ### ##### # ####### #
#   #   #   #   # #   # #   #   #   # # # # #   # #
# # ### ##### ### # # ### # ####### # # # # # # # #
# #           #     #     #         #     #   #   >
###################################################


Create a new paste based on this one


Comments: