hey guys, what's ya'll. this, have create bfs function finds shortest path between destination , source game called quoridor, i'm sure of ya'll played before. when run it, no errors found want shortest path function return path 1 did in searchbfs function. also, sorry sloppy post, it's first one.
here's code....
from interface import * import engine #public routines def init(gamefile, playerid, numplayers, playerhomes, wallsremaining): """ parts engine calls method player can initialize data. gamefile - string file containing initial board state. playerid - numeric id of player (0-4). numplayers - number of players (2 or 4). playerhomes - list of coordinates each players starting cell (po-p4). wallsremaining - number of walls remaining (same players @ start).""" # log message string standard output , current log file engine.log_msg("player.init called player " + str(playerid) + " file=" + gamefile + ', playerid=' + str(playerid) + ', numplayers=' + str(numplayers) + ', playerhomes=' + str(playerhomes) + ', wallsremaining=' + str(wallsremaining)) playerdata = none # initialize data structure here, , return it. # passed 'playerdata' in shortest_path function. return playerdata def shortest_path(playerdata, source, dest): """returns list of coordinates representing shortest path on board between start , finish coordinates. playerdata - player's data source - start coordinate dest - end coordinate""" engine.log_msg("player.shortest_path called. source: " + str(source) + ", dest: " + str(dest)) path = [] def searchbfs(graph, source, dest): """performs bfs search between source , destination nodes using queue. returns path list of graph nodes (or empty list if no path exists).""" dispenser = myqueue.queue() myqueue.enqueue(source, dispenser) #construct visited list. visited = set() visited.add(source) #loop untill either destination found or dispenser #is empty (no path) while not myqueue.empty(dispenser): current = myqueue.front(dispenser) myqueue.dequeue(dispenser) if current == dest: break # loop on neighbors of current neighbor in current.neighbors: # process unvisited neighbors if neighbor not in visited: neighbor.previous = current visited.add(neighbor) myqueue.enqueue(neighbor, dispenser) return constructpath(visited, source, dest) def constructpath(visited, source, dest): """returns path list of nodes source destination""" #construct path using stack , traversing destination #node source node using node's previous stack = mystack.stack() if dest in visited: temp = dest while tmp != source: mystack.push(tmp, stack) tmp = tmp.previous mystack.push(source, stack) path = [] while not mystack.empty(stack): path.append(mystack.top(stack)) mystack.pop(stack) return path
stack = mystack.stack() if dest in visited: temp = dest while tmp != source: mystack.push(tmp, stack) tmp = tmp.previous
i think tmp
, temp
variables here should same? although expect error since tmp
isn't assigned before use it.
Comments
Post a Comment