anubhav_c

ASCII mazes

Mar 12th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.50 KB | None | 0 0
  1. # Grid format:
  2. #   0 = Navigable space
  3. #   1 = Occupied space
  4.  
  5. import random
  6.  
  7. deltas = [[-1, 0 ], # go up
  8.         [ 0, -1], # go left
  9.         [ 1, 0 ], # go down
  10.         [ 0, 1 ]] # go right
  11.  
  12. delta_names = ['^', '<', 'v', '>']
  13.  
  14. cost = 1
  15.  
  16. def search(grid, init, goal, cost):
  17.     current = [0] + init
  18.     open = [current]
  19.     closed = []
  20.     action = [[ '' for col in range(len(grid[0]))] for row in range(len(grid))]
  21.     resign = False
  22.     while current[1:] != goal:
  23.         if not open: #no open nodes left
  24.             resign = True
  25.             current[0] = -1
  26.             break
  27.         current = min(open, key= lambda pos: pos[0]) #Find open node with smallest g
  28.         #first coordinate is y, not x
  29.         gcur, ycur, xcur = current
  30.         for d in zip(deltas, delta_names):
  31.             (dy, dx), direction = d
  32.             g, y, x = gcur + cost, ycur + dy, xcur + dx
  33.             if x<0 or y<0 or y>=len(grid) or x>=len(grid[0]):
  34.                 continue
  35.             elif [y,x] in closed or grid[y][x]==1: #already visited, or a wall
  36.                 continue
  37.             elif [y,x] in map(lambda node: node[1:], open): #already planning to visit
  38.                 continue
  39.             else:
  40.                 open.append([g, y, x])
  41.                 action[y][x] = direction
  42.        
  43.         open.remove([gcur,ycur,xcur])
  44.         closed.append([ycur, xcur])
  45.        
  46.     current_g, _, _ = current #cost of getting to goal
  47.    
  48.     #ASCII art for path
  49.     dir_dict = {}
  50.     for d in zip(deltas, delta_names):
  51.         delta, delta_name = d
  52.         dir_dict[delta_name] = delta
  53.    
  54.     roadmap = [[' ' for col in range (len(grid[0]))] for row in range(len(grid))]
  55.    
  56.     #add walls
  57.     for i in range(len(grid)):
  58.         for j in range(len(grid[0])):
  59.             if grid[i][j] == 1:
  60.                 roadmap[i][j] = 'H'
  61.    
  62.     #add path
  63.     path_y, path_x = ycur, xcur
  64.     if resign: #nowhere left to go
  65.         roadmap[path_y][path_x] = '?'
  66.     goal_y, goal_x = goal
  67.     roadmap[goal_y][goal_x] = '*'
  68.     while [path_y, path_x] != init:
  69.         motion = action[path_y][path_x]
  70.         path_dy, path_dx = dir_dict[motion]
  71.         path_y, path_x = path_y - path_dy, path_x - path_dx
  72.         roadmap[path_y][path_x] = motion
  73.    
  74.     #beautify
  75.     roadmap = map(lambda x: ' '.join(x), roadmap)
  76.     roadmap = map(lambda x: '| '+ x + ' |', roadmap)
  77.     roadmap = ['='*len(roadmap[0])] + roadmap + ['='*len(roadmap[0])]
  78.     return current_g, roadmap
  79.  
  80. def generate_grid(maxsize):
  81.     no_of_columns = random.randint(2, maxsize)
  82.     no_of_rows = random.randint(2,maxsize)
  83.     max_wall_percent = 25
  84.     cells_per_wall = 100/max_wall_percent
  85.  
  86.     grid = [ [0] * no_of_columns ] * no_of_rows
  87.     grid = map(lambda row: map(lambda entry: 1 if random.randint(1, cells_per_wall)%int(cells_per_wall)==0\
  88.         else 0, row), grid)
  89.  
  90.     while True:
  91.         init_y, init_x = random.randint(0, len(grid)-1), random.randint(0, len(grid[0])-1)
  92.         if grid[init_y][init_x] == 0: #initial position isn't a wall
  93.             break
  94.     init = [init_y, init_x]
  95.     while True:
  96.         goal_y, goal_x = random.randint(0, len(grid)-1), random.randint(0, len(grid[0])-1)
  97.         if grid[goal_y][goal_x] == 0 and [goal_y,goal_x] != init: #goal shouldn't be a wall, or same as start point
  98.             break
  99.     goal = [goal_y, goal_x]
  100.    
  101.     return grid, init, goal
  102.  
  103. grid, init, goal = generate_grid(20)
  104. steps, path= search(grid, init, goal, cost)
  105. for row in path:
  106.     print row
  107. print "cost:", steps
Advertisement
Add Comment
Please, Sign In to add comment