Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. import re
  2. import Queue
  3.  
  4. file = open("map.txt",'r')
  5. Lines = file.readlines()
  6.  
  7. #initializing
  8. map = []
  9. S = [(0,0,'S')]    
  10. G = [(0,0,'G')]
  11. #end initializing
  12.  
  13. n = 0
  14. for line in Lines:
  15.     Lines[n] = line.strip('\n')         #trust me on this one
  16.     splitLine = list(Lines[n])          #separate each line into a list of chars
  17.     coordLine = [tuple([n,0,'X'])]      #initializing with left-most column
  18.     m = 1                               #n for row, m for column
  19.     for point in splitLine:
  20.         if point == 'S':
  21.             S = [tuple([n,m,point])]
  22.         if point == 'G':
  23.             G = [tuple([n,m,point])]
  24.         coordLine += [tuple([n,m,point])]   #important line
  25.         m += 1
  26.     #end first for loop
  27.  
  28.     map += [coordLine]
  29.     n += 1
  30. #end second for loop
  31.    
  32. #print map
  33.  
  34. def printMap(map):
  35.     print "start printing"
  36.     n = 0
  37.     print "in loop:"
  38.     for line in map:
  39.         for point in map[n]:
  40.             x,y,c = point
  41.             print c,        #c is 'X', '_', 'S', or 'G'
  42.         print ''
  43.         n += 1
  44.  
  45.  
  46. def heuristic(goal,next): #this is a placeholder; you might need more parameters
  47.     return 0
  48.  
  49.  
  50. def neighbors(map, current):
  51.     print current
  52.     [(x0,y0,c0)] = current
  53.     #I don't have to do index safety checking, there's a border of X's
  54.     result = [];
  55.     for n in range(-1,2):       #range(-1,2) is -1,0,1 and it does not include 2
  56.         for m in range(-1,2):
  57.             if (n == 0 and m == 0):     #it is not its own neighbor. But... maybe it is? Be careful about that.
  58.                 continue
  59.             x,y,c = map[x0+n][y0+m]
  60.             if c == 'X':                #'X' are obstacles, not neighbors
  61.                 continue
  62.             result += [[x,y,c]]         #this must be a neighbor
  63.            
  64.     print "result:", result
  65.     return result
  66.  
  67. def greedyAlg(map, start, goal): #I've done some stuff to make this work but it's not fully working yet
  68.     frontier = Queue.PriorityQueue()
  69.     frontier.put(start, 0)
  70.     came_from = {}
  71.     came_from[start] = None
  72.     while not frontier.empty():
  73.         current = frontier.get()
  74.         if current == goal:
  75.             break
  76.         for next in neighbors(map,current):
  77.             if next not in came_from:
  78.                 priority = heuristic(goal, next)
  79.                 frontier.put(next, priority)
  80.                 came_from[next] = current
  81.  
  82. def aStar(map): #almost entirely unmodified
  83.     frontier = Queue.PriorityQueue()
  84.     frontier.put(start, 0)
  85.     came_from = {}
  86.     came_from[start] = None
  87.     while not frontier.empty():
  88.         current = frontier.get()
  89.         if current == goal:
  90.             break
  91.         for next in graph.neighbors(current):
  92.             if next not in came_from:
  93.                 priority = heuristic(goal, next)
  94.                 frontier.put(next, priority)
  95.                 came_from[next] = current
  96.  
  97. def someFunc():
  98.     current = goalpath= [current]
  99.     while current!= start:
  100.         current = came_from[current]
  101.         path.append(current)
  102.     path.append(start)
  103.     path.reverse()
  104.    
  105. printMap(map)
  106. greedyAlg(map,S,G)
  107. #neighbors(map,G)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement