簡體   English   中英

如何在python中檢查相鄰狀態

[英]how to check the neighbouring states in python

newPos給了我pacman agent的位置(例如(3,5))。

newPos = successorGameState.getPacmanPosition()

老式食物以網格的形式給了我剩余的可吃的食物。 我們可以通過列表訪問網格,例如,如果我們想知道(3,4)是否有食物,那么我們可以

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


newGhostStates = successorGameState.getGhostStates()

這將給我列出存在的幽靈的清單。

我需要找到周圍的幽靈,即newPos旁邊的幽靈。 我該怎么辦? 我無法實現它。 我在Google上找到了解決方案,但我不想使用此方法。 在這里,使用半徑,我不知道為什么?

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位置檢查這些位置,如果它們匹配,則有一個鬼影。

好吧,想象一下:

...
.P.
.G.

P是吃豆人,G是鬼。 用“半徑= 1”檢查是與吃豆人相鄰的重影。 此檢查將找到幻影。 但:

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

但是在這里找不到半徑為1的幻影,因此需要半徑2。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM