import sys import getopt import random random.seed() def main(): try: opts, args = getopt.getopt(sys.argv[1:], "hs:w:o:p") except getopt.error, msg: print msg sys.exit(1) worldSize = 600.0; mazeSize = 20 printAscii = False out = sys.stdout wallWidth = -1.0 for o, a in opts: if o == "-s": mazeSize = int(a) if o == "-w": wallWidth = int(a) if o == "-p": printAscii = True if o == "-o": try: out = open(a, "w") except: sys.exit(2) step = worldSize / float(mazeSize) if wallWidth < 0.0: wallWidth = worldSize / mazeSize * .2 walls = genMaze(mazeSize) if (printAscii): printMaze(walls, mazeSize) out.write("%d\n\n" % (worldSize,)) halfWall = wallWidth * .5 offset = halfWall * .5 sz = worldSize out.write("(%d,%d)(%d,%d)(%d,%d)(%d,%d)\n" % (0, -offset, sz, -offset, sz, offset, 0, offset)) out.write("(%d,%d)(%d,%d)(%d,%d)(%d,%d)\n" % (-offset, -offset, offset, -offset, offset, sz + offset, -offset, sz + offset)) out.write("(%d,%d)(%d,%d)(%d,%d)(%d,%d)\n" % (0, sz - offset, sz, sz - offset, sz, sz + offset, 0, sz + offset)) out.write("(%d,%d)(%d,%d)(%d,%d)(%d,%d)\n" % (sz - offset, -offset, sz + offset, -offset, sz + offset, sz + offset, sz - offset, sz + offset)) for i,j in walls: if (i / mazeSize == j / mazeSize): x = (i % mazeSize + j % mazeSize + 1) * 0.5 * step - wallWidth * 0.5 y = (i / mazeSize) * step out.write("(%d,%d)(%d,%d)(%d,%d)(%d,%d)\n" % (x, y - halfWall, x + wallWidth, y - halfWall, x + wallWidth, y + step + halfWall, x, y + step + halfWall)) else: x = (j % mazeSize) * step y = (i / mazeSize + j / mazeSize + 1) * 0.5 * step - wallWidth * .5 l, r = (x, x + step) out.write("(%d,%d)(%d,%d)(%d,%d)(%d,%d)\n" % (l, y, r, y, r, y + wallWidth, l, y + wallWidth)) def genMaze(mazeSize): """ Generates a maze and returns the maze as a list of walls. Each wall is represented as a 2-tuple whose elements are the cells the wall is between. Each element in the tuple is an index into the flattened 2D array repr. the mazeSize x mazeSize grid (if mazeSize = 20 and index = 36, then row = 36 / 20 = 1, col = 36 % 20 = 16). """ walls = [(i, i+1) for i in range(0, mazeSize * mazeSize) if (i + 1) % mazeSize != 0] walls.extend([(i, i+mazeSize) for i in range(0, mazeSize * mazeSize) if (i + mazeSize) / mazeSize < mazeSize]) open = [] c = random.randrange(0, mazeSize * mazeSize) explored = [] while (len(explored) < mazeSize * mazeSize): nbrs = (c + 1, c - mazeSize, c - 1, c + mazeSize) validWalls = [n for n in nbrs if n not in explored and (min(n, c), max(n, c)) in walls] if (len(validWalls) > 0): n = random.choice(validWalls) walls.remove((min(c, n), max(c, n))) open.append(c) c = n if (c not in explored): explored.append(c) else: c = open.pop() return walls def printMaze(walls, mazeSize): maze = "" maze += "_" for col in range(mazeSize): maze += "__" maze += "_\n" for row in range(mazeSize): maze += "|" for col in range(mazeSize): cell = row * mazeSize + col if (cell,cell+mazeSize) in walls or row == mazeSize - 1: maze += "_" else: maze += " " if (cell,cell+1) in walls: maze += "|" else: if (cell,cell+mazeSize) in walls or row == mazeSize - 1: maze += "_" else: maze += " " maze += "|\n" print maze if __name__ == "__main__": main()