Maze Generation Algorithm - Python Code Example

Python Code Example

import numpy from numpy.random import random_integers as rand import matplotlib.pyplot as pyplot def maze(width=81, height=51, complexity=.75, density=.75): # Only odd shapes shape = ((height // 2) * 2 + 1, (width // 2) * 2 + 1) # Adjust complexity and density relative to maze size complexity = int(complexity * (5 * (shape + shape))) density = int(density * (shape // 2 * shape // 2)) # Build actual maze Z = numpy.zeros(shape, dtype=bool) # Fill borders Z = Z = 1 Z = Z = 1 # Make isles for i in range(density): x, y = rand(0, shape // 2) * 2, rand(0, shape // 2) * 2 Z = 1 for j in range(complexity): neighbours = if x > 1: neighbours.append((y, x - 2)) if x < shape - 2: neighbours.append((y, x + 2)) if y > 1: neighbours.append((y - 2, x)) if y < shape - 2: neighbours.append((y + 2, x)) if len(neighbours): y_,x_ = neighbours if Z == 0: Z = 1 Z = 1 x, y = x_, y_ return Z pyplot.figure(figsize=(10, 5)) pyplot.imshow(maze(80, 40), cmap=pyplot.cm.binary, interpolation='nearest') pyplot.xticks, pyplot.yticks pyplot.show

Read more about this topic:  Maze Generation Algorithm

Famous quotes containing the word code:

    Many people will say to working mothers, in effect, “I don’t think you can have it all.” The phrase for “have it all” is code for “have your cake and eat it too.” What these people really mean is that achievement in the workplace has always come at a price—usually a significant personal price; conversely, women who stayed home with their children were seen as having sacrificed a great deal of their own ambition for their families.
    Anne C. Weisberg (20th century)