Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import Queue
- file = open("map.txt",'r')
- Lines = file.readlines()
- #initializing
- map = []
- S = [(0,0,'S')]
- G = [(0,0,'G')]
- #end initializing
- n = 0
- for line in Lines:
- Lines[n] = line.strip('\n') #trust me on this one
- splitLine = list(Lines[n]) #separate each line into a list of chars
- coordLine = [tuple([n,0,'X'])] #initializing with left-most column
- m = 1 #n for row, m for column
- for point in splitLine:
- if point == 'S':
- S = [tuple([n,m,point])]
- if point == 'G':
- G = [tuple([n,m,point])]
- coordLine += [tuple([n,m,point])] #important line
- m += 1
- #end first for loop
- map += [coordLine]
- n += 1
- #end second for loop
- #print map
- def printMap(map):
- print "start printing"
- n = 0
- print "in loop:"
- for line in map:
- for point in map[n]:
- x,y,c = point
- print c, #c is 'X', '_', 'S', or 'G'
- print ''
- n += 1
- def heuristic(goal,next): #this is a placeholder; you might need more parameters
- return 0
- def neighbors(map, current):
- print current
- [(x0,y0,c0)] = current
- #I don't have to do index safety checking, there's a border of X's
- result = [];
- for n in range(-1,2): #range(-1,2) is -1,0,1 and it does not include 2
- for m in range(-1,2):
- if (n == 0 and m == 0): #it is not its own neighbor. But... maybe it is? Be careful about that.
- continue
- x,y,c = map[x0+n][y0+m]
- if c == 'X': #'X' are obstacles, not neighbors
- continue
- result += [[x,y,c]] #this must be a neighbor
- print "result:", result
- return result
- def greedyAlg(map, start, goal): #I've done some stuff to make this work but it's not fully working yet
- frontier = Queue.PriorityQueue()
- frontier.put(start, 0)
- came_from = {}
- came_from[start] = None
- while not frontier.empty():
- current = frontier.get()
- if current == goal:
- break
- for next in neighbors(map,current):
- if next not in came_from:
- priority = heuristic(goal, next)
- frontier.put(next, priority)
- came_from[next] = current
- def aStar(map): #almost entirely unmodified
- frontier = Queue.PriorityQueue()
- frontier.put(start, 0)
- came_from = {}
- came_from[start] = None
- while not frontier.empty():
- current = frontier.get()
- if current == goal:
- break
- for next in graph.neighbors(current):
- if next not in came_from:
- priority = heuristic(goal, next)
- frontier.put(next, priority)
- came_from[next] = current
- def someFunc():
- current = goalpath= [current]
- while current!= start:
- current = came_from[current]
- path.append(current)
- path.append(start)
- path.reverse()
- printMap(map)
- greedyAlg(map,S,G)
- #neighbors(map,G)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement