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[0] // 2 * shape[1] // 2)) # Build actual maze Z = numpy.zeros (shape, dtype-bool) # Fill borders Z[0, :] = 21-1 ]=1 Z[:, 0] = Z[:, -1] = 1 # Make aisles for i in range (density) : x, y = rand(0, shape[1] /| 2) * 2, rand (0, shape[0] // 2) Z[y, x] = for i in range (complexity): neighbours = [] if x> 1: neighbours.append( (y, x - 2)) if × < shape[1] - 2: neighbours.append( (y, x + 2)) if y > 1: neighbours. append ((y-2, x)) if y < shape[0] - 2: neighbours. append ((y + 2, x)) if len (neighbours): Y_, X_ _ = neighbours[rand(0, len (neighbours) - 1)] if 0 : ZIy_, x_1= Z[Y_+(y-Y_) 1/2,x_+ (x - x_) // 21 = 1 x, Y= X_I Y_ return Z pyplot.figure(figsize=(10, 5)) pyplot. imshow (maze (80, 40), cmap-pyplot.cm.binary, interpolation='nearest') pyplot.xticks([]), pyplot.yticks([])