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:

    Wise Draco comes, deep in the midnight roll
    Of black artillery; he comes, though late;
    In code corroborating Calvin’s creed
    And cynic tyrannies of honest kings;
    He comes, nor parlies; and the Town, redeemed,
    Gives thanks devout; nor, being thankful, heeds
    The grimy slur on the Republic’s faith implied,
    Which holds that Man is naturally good,
    And—more—is Nature’s Roman, never to be
    scourged.
    Herman Melville (1819–1891)