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:

    Hollywood keeps before its child audiences a string of glorified young heroes, everyone of whom is an unhesitating and violent Anarchist. His one answer to everything that annoys him or disparages his country or his parents or his young lady or his personal code of manly conduct is to give the offender a “sock” in the jaw.... My observation leads me to believe that it is not the virtuous people who are good at socking jaws.
    George Bernard Shaw (1856–1950)