2010-07-31 46 views
0

newPos給我pacman代理的位置(如(3,5))。如何檢查python中的鄰居狀態

newPos = successorGameState.getPacmanPosition() 

舊食品給我剩餘的食物可用於網格形式的pacman。我們可以通過一樣,如果我們想知道,如果食物可在(3,4)列表訪問網格然後我們做

oldFood = currentGameState.getFood() 
    if oldFood[x][y] == true.... 


newGhostStates = successorGameState.getGhostStates() 

這會給我的列表中所存在的鬼。

我需要找到周圍的幽靈,即與newPos相鄰的幽靈。我會怎麼做?我無法實現它。我有一個谷歌soln,但我不想使用這種方法。在這裏,使用半徑,我不知道爲什麼?

def surroundingGhosts(gList,(x,y), radius=1): 
     allGhosts = {} 
     sGhosts = [] 
     for g in gList: 
     ### map ghosts positions to ghost obj 
     allGhosts[g.getPosition()] = g 

     checkPos = [] 
     for xx in range(x-radius,x+radius+1): 
     if xx == x: 
      for yy in range(y-radius, y+radius+1): 
      checkPos += [(xx,yy)] 
     else: 
      checkPos += [(xx,y)] 
     for p in checkPos: 
     if p[0] and p[1] >= 0: 
      if p in allGhosts: 
      sGhosts += [allGhosts[p]] 

     return sGhosts 

基本上,我需要找到與我當前位置相關的周邊位置。然後,我會檢查這些位置與gjost位置,如果他們匹配,那麼有一個鬼。

回答

2

那麼,想象這樣:

... 
.P. 
.G. 

P是G是鬼pacman的。用「半徑= 1」檢查是pacman附近的幽靈。這個檢查會發現鬼。但是:

.... 
.P.G 
.... 
.... 

但是在這裏找不到半徑爲1的幽靈,所以半徑爲2是必需的。

+0

有沒有其他的方法可以做到這一點?不使用半徑? – Shilpa 2010-07-31 06:45:38

+0

基本上,我需要找到與我當前位置相關的周邊位置。然後,我會檢查這些位置與gjost位置,如果他們匹配,那麼有一個鬼。 – Shilpa 2010-07-31 06:48:44

+0

@Shilpa:如果你只需要相鄰的位置(比如Pacman的右下角),檢查x,y + 1,x,y-1,x + 1,y, 1,你就完成了。這可以擴展到更長的範圍,代碼可能比上面粘貼的代碼簡單一些。 – 2010-07-31 06:50:30