Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2011
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 3.70 KB | None | 0 0
  1. import random
  2. import sys
  3. import numpy
  4.  
  5. X = 1000
  6. Y = 1000
  7.  
  8. # the following creats a two dimensional array
  9. # of indexes. Each cell now has a number, and we can convert co-ordinates
  10. # into numbers via this array
  11. indexes = numpy.arange(X*Y).reshape(X,Y)
  12. # These 1d arrays correspond to the data previously inside the cell class
  13. right_walls = numpy.ones(X*Y, bool)
  14. down_walls = numpy.ones(X*Y, bool)
  15.  
  16.  
  17.  
  18. def gen_walls():
  19.     # create an array which holds information about walls
  20.     # There will be three columns
  21.     # First column: parent id
  22.     # Second collumn: neighbor id
  23.     # Third column: True if direction of wall is to the right
  24.     total_right_walls = (X - 1) * Y
  25.     total_down_walls =  (Y - 1) * X
  26.     total_wall_count = total_right_walls + total_down_walls
  27.     walls = numpy.empty( (total_wall_count, 3), int)
  28.  
  29.     # fill up the walls array with right walls
  30.     walls[:total_right_walls,0] = indexes[:-1,:].flatten()
  31.     walls[:total_right_walls,1] = indexes[1:,:].flatten()
  32.     walls[:total_right_walls,2] = True
  33.  
  34.     # fill up the walls array with down walls
  35.     walls[total_right_walls:,0] = indexes[:,:-1].flatten()
  36.     walls[total_right_walls:,1] = indexes[:,1:].flatten()
  37.     walls[total_right_walls:,2] = False
  38.  
  39.     return walls
  40.  
  41.  
  42. class UnionFind(object):
  43.     def __init__(self, sections):
  44.         self.parent = numpy.arange(sections)
  45.         self.rank = numpy.ones(sections)
  46.         self.final = numpy.ones(sections, bool)
  47.  
  48.     def find(self, a):
  49.         # in python, local variable access is much faster then attributes on classes
  50.         # when speed is really really neccesary, its worthwhile to assign any attributes
  51.         # accessed multiple times to local variabled
  52.         parents = self.parent
  53.         parent = parents[a]
  54.         if not self.final[parent]:
  55.             parent = self.find(parent)
  56.             parents[a] = parent
  57.         return parent
  58.  
  59.     def union(self, a, b):
  60.         find = self.find
  61.         a_parent = find(a)
  62.         b_parent = find(b)
  63.         if a_parent != b_parent:
  64.             rank = self.rank
  65.             parent = self.parent
  66.             final = self.final
  67.             a_rank = rank[a_parent]
  68.             b_rank = rank[b_parent]
  69.             if a_rank < b_rank:
  70.                 parent[a_parent] = b_parent
  71.                 final[a_parent] = False
  72.             elif b_rank < a_rank:
  73.                 parent[b_parent] = a_parent
  74.                 final[b_parent] = False
  75.             else:
  76.                 parent[b_parent] = a_parent
  77.                 final[b_parent] = False
  78.                 rank[a_parent] += 1
  79.             return True
  80.         else:
  81.             return False
  82.  
  83.  
  84.  
  85. def break_walls():
  86.     walls = gen_walls()
  87.     numpy.random.shuffle(walls)
  88.  
  89.     # Smash some walls
  90.     uf = UnionFind( X*Y )
  91.  
  92.     for parent, neighbour, right in walls:
  93.         # If the cells on either side of the wall aren't already connected,
  94.         # destroy the wall
  95.         if uf.union(parent, neighbour):
  96.             if right:
  97.                 right_walls[parent] = False
  98.             else:
  99.                 down_walls[parent] = False
  100.  
  101.  
  102. def output(blocked):
  103.     sys.stdout.write(" X"[blocked])
  104.    
  105. def draw_maze():
  106.     # Draw the maze
  107.     for x in range(2*X+1):
  108.         output(True)
  109.     sys.stdout.write('\n')
  110.  
  111.     for y in xrange(Y):
  112.         output(True)
  113.         for x in xrange(X):
  114.             output(False)
  115.             output(right_walls[indexes[x,y]])
  116.         sys.stdout.write('\n')
  117.         output(True)
  118.         for x in xrange(X):
  119.             output(down_walls[indexes[x,y]])
  120.             output(True)
  121.         sys.stdout.write('\n')
  122.  
  123. def main():
  124.     break_walls()
  125.     draw_maze()
  126.        
  127. if __name__ == '__main__':
  128.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement