#!/usr/bin/env python
import sys
from collections import deque #high performance queue "deck"
class Node(object):
def __init__(self,x,y,history):
self.locationx = x
self.locationy = y
self.data = None
self.history = history #previous node to go backwards
def printNodePathTrace(inNode,width,height,mapTerrain,frontier):
#travel backward through node history until history == None is reached
#print off map of path
mapPath = mapTerrain
for i in range(width): #fill map with blanks
for j in range(height):
mapPath[i][j] = '-'
#print frontier list
print "length of frontier"
print len(frontier)
for item in frontier:
#print item.locationy
#print item.locationx
mapPath[item.locationy][item.locationx] = '*'
#print path found
done = 0
count = 0
currentNode = inNode
while(done == 0 and count < 50):
mapPath[currentNode.locationy][currentNode.locationx] = '#'
if currentNode.history == None:
done = 1
currentNode = currentNode.history
count += 1
printMap(mapPath)
def printMap(mapTerrain): #horizontal right positive x, verticle down positive y
for i in mapTerrain:
for j in i:
sys.stdout.write(j)
sys.stdout.write('\n')
def printMapStartAndGoal(mapTerrain,startX,startY,goalX,goalY):
#Y is row, X is column. Y is vertical, X is horizontal
temp1 = mapTerrain[startY][startX]
temp2 = mapTerrain[goalY][goalX]
mapTerrain[startY][startX] = 'S'
mapTerrain[goalY][goalX] = 'G'
printMap(mapTerrain)
mapTerrain[startY][startX] = temp1
mapTerrain[goalY][goalX] = temp2
def main():
#Input map
#Your program should be able to read a map file in the following format.
#Width Height
#StartX StartY
#GoalX GoalY
#map
searchMode = "BFS" #options are BFS, LC, ID, A*1, A*2
logfile = open("smallmap2.txt", "r")
[width,height] = map(int,logfile.readline().split())
[startX,startY] = map(int,logfile.readline().split())
[goalX,goalY] = map(int,logfile.readline().split())
mapTerrainInput = logfile.read()
mapTerrain = map(list,mapTerrainInput.splitlines())
#map the list function to mapTerrainInput split into lines without '\n'
printMapStartAndGoal(mapTerrain,startX,startY,goalX,goalY)
print mapTerrain
printMap(mapTerrain)
closedList = [] #contains list of nodes visited already
frontier = deque([])
startNode = Node(startX,startY,None)
#check if node is a goal node
#add node to closed list
#add expansions to frontier list (not ones on closed list)
#Repeat with next node in Frontier
goalFound = 0 ### there's an error with this line's indentation???
iterationCount = 0
currentNode = startNode
while goalFound == 0 and iterationCount < 500: #stop when goal is found
if (currentNode.locationx == goalX and currentNode.locationy == goalY):
goalFound = 1
break
closedList.append(currentNode)
#expand node - currently not checking the closed list
if (currentNode.locationy > 0): #can expand up
frontier.append(Node(currentNode.locationx,currentNode.locationy - 1,currentNode))
if (currentNode.locationy < height - 1): #can expand down
frontier.append(Node(currentNode.locationx,currentNode.locationy + 1,currentNode))
if (currentNode.locationx > 0): #can expand left
frontier.append(Node(currentNode.locationx - 1,currentNode.locationy,currentNode))
if (currentNode.locationx < width -1): #can expand right
frontier.append(Node(currentNode.locationx + 1,currentNode.locationy,currentNode))
#done expanding
currentNode = frontier.popleft()
iterationCount += 1
print currentNode.history
print currentNode.locationx
print currentNode.locationy
printNodePathTrace(currentNode,width,height,mapTerrain,closedList)
if __name__ == '__main__':
main()