Shilpa
Shilpa

Reputation: 1925

find the neighbouring states around the agent

I need to find the states that are next to my pacman agent. The current state is Tuple(3,5) which is given by numPos.

I need to check the position that are around the pacman agent. I want to find the neighbouring states so that i can check it with the ghosts states and if they matches, that means , a ghost is present in the neighbouring side. But I am not able to find the neighbouring states. Like , I need to check (x, y+1), (x,y-1) , (y,x+1), (y, x-1). How do i implement it.I cannot use the range function here.

Upvotes: 0

Views: 104

Answers (3)

Tony Veijalainen
Tony Veijalainen

Reputation: 5555

If you want to get fancy you can make neighbour tuples ready for whole grid so that the movement out of grid and to the walls is impossible also runtime action is fast as it need only lookup from ready neighbour dictionary:

neighbourghosts = [(nx,ny) 
                   for nx,ny in neighbours_of[numPos] 
                   if (nx,ny) in ghostplaces]

Upvotes: 1

John La Rooy
John La Rooy

Reputation: 304245

for dx,dy in ((1,0),(0,1),(-1,0),(0,-1)):
    search_position(x+dx, y+dy)

Upvotes: 2

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72765

What is the problem? Your question (and the previous) one are quite unclear. What's wrong with this.

x,y = numPos
positions_to_search = [ (x-1, y),
                        (x-1, y-1),
                        (x, y-1),
                        (x+1, y-1),
                        (x+1, y),
                        (x+1, y+1),
                        (x, y+1),
                        (x-1, y+1)]

What have you tried?

Upvotes: 2

Related Questions