2010-12-21 34 views
0

嘿,夥計們,你們怎麼了。爲此,我必須創建一個BFS函數,以找到稱爲Quoridor的遊戲的目標和源之間的最短路徑,我相信一些人以前會玩這個遊戲。當我運行它時,沒有發現錯誤,但我希望最短路徑函數返回路徑,就像我在searchBFS函數中一樣。另外,對於馬虎的帖子感到抱歉,這是我的第一個。BFS功能沒有返回正確的路徑

下面的代碼....

from interface import * 
import engine 

#public routines 

def init(gameFile, playerId, numPlayers, playerHomes, wallsRemaining): 

""" 
    For all parts the engine calls this method so the player can initialize their data. 
     gameFile - a string which is the file containing the initial board state. 
     playerId - the numeric Id of the player (0-4). 
     numPlayers - the number of players (2 or 4). 
     playerHomes - a list of coordinates for each players starting cell (PO-P4). 
     wallsRemaining - the number of walls remaining (same for all players at start).""" 

    # log any message string to standard output and the current log file 
    engine.log_msg("player.init called for player " + str(playerId) + " File=" + gameFile + 
        ', playerId=' + str(playerId) + ', numPlayers=' + str(numPlayers) + ', playerHomes=' + 
        str(playerHomes) + ', wallsRemaining=' + str(wallsRemaining)) 

    playerData = None 

    # initialize your data structure here, and then return it. It will be 
    # passed back to you as 'playerData' in the shortest_path function. 

    return playerData 

def shortest_path(playerData, source, dest): 

"""Returns a list of coordinates representing the shortest path on the 
    board between the start and finish coordinates. 
     playerData - The player's data 
     source - the start coordinate 
     dest - the end coordinate""" 

    engine.log_msg("player.shortest_path called. Source: " + str(source) + 
        ", Dest: " + str(dest)) 

    path = [] 

def searchBFS(graph, source, dest): 

"""Performs a BFS search between source and destination nodes using a queue. 
    It returns the path as a list of graph nodes (or an empty list if no path exists).""" 

    dispenser = myQueue.Queue() 
    myQueue.enqueue(source, dispenser) 

    #construct the visited list. 

    visited = set() 
    visited.add(source) 

    #loop untill either the destination is found or the dispenser 
    #is empty (no path) 

    while not myQueue.empty(dispenser): 
     current = myQueue.front(dispenser) 
     myQueue.dequeue(dispenser) 
     if current == dest: 
      break 
     # loop over all neighbors of current 
     for 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 the path as a list of nodes from source to destination""" 

    #construct the path using a stack and traversing from the destination 
    #node back to the 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 
+0

您可以選擇您的代碼並單擊編輯窗口中的花括號按鈕,以便將其格式化爲可讀代碼。 – 2010-12-21 00:07:26

回答

0
stack = myStack.Stack() 
if dest in visited: 
    temp = dest 
    while tmp != source: 
     myStack.push(tmp, stack) 
     tmp = tmp.previous 

我覺得tmptemp變量這裏應該是一樣的嗎?雖然我會預期一個錯誤,因爲tmp在使用之前未分配。

+0

似乎引擎只調用shortest_path函數,但它應該返回BFS路徑,但它沒有... – 2010-12-21 01:08:04