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:

    Acknowledge your will and speak to us all, “This alone is what I will to be!” Hang your own penal code up above you: we want to be its enforcers!
    Friedrich Nietzsche (1844–1900)