tags:

views:

34

answers:

1

The newPos gives me the position of pacman agent (like (3,5) ).

newPos = successorGameState.getPacmanPosition()

The oldfood gives me the remaining food available for pacman in the form of grid. We can access the grid via list like if we want to know if food is available at (3,4) then we do

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


newGhostStates = successorGameState.getGhostStates()

This will give me the list for the ghosts that are present.

I need to find the surrounding ghosts i.e the ghosts that are next to newPos. How would I do that ? I am not able to implement it. I got a soln on google but I dont want to use this method. Here , radius is used, i dont know why?

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

basically , i need to find the surrrounding positions related to my current postion. Then I will check these postions with the gjost positions, if they match, then there is a ghost.

+2  A: 

Well, imagine this:

...
.P.
.G.

P is the pacman an G is a ghost. Checking with "radius = 1" is ghosts adjacent to the pacman. This check will find the ghost. But:

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

But here a ghost won't be found with radius of 1, so radius of 2 is required.

Eli Bendersky
is there any other method by which i can do this? without using the radius?
Shilpa
basically , i need to find the surrrounding positions related to my current postion. Then I will check these postions with the gjost positions, if they match, then there is a ghost.
Shilpa
@Shilpa: if you only need the adjacent positions (i.e. "right next" to the Pacman), check `x,y+1`, `x,y-1`, `x+1,y`, `x-1,y` and you're done. This can be extended to longer ranges with code that's probably a bit simpler than what you pasted above.
Eli Bendersky
but how do i gonna implement it? Like I have "numpos" which is(3,5) but I cant use this number. I can use only "numpos" as my keyword. Could you just explain a lil bit for x,y+1, x,y-1 case.
Shilpa
@Shilpa: I'm not sure what you mean. Your `newpos` is probably a tuple of two elements. the first (`newpos[0]`) is `x`, the second (`newpos[1]`) `y`. Maybe you have a deeper / more fundamental Python question here. If so, I recommend you to simplify it as much as you can and create a new SO question for it, asking exactly what you don't understand. In such a case, try to give a minimal simple code sample
Eli Bendersky
I posted a new question, plz check it
Shilpa
Your new question is almost the same as this. Why did you post a separate one?
Noufal Ibrahim