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 开发者_C百科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.
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?
for dx,dy in ((1,0),(0,1),(-1,0),(0,-1)):
search_position(x+dx, y+dy)
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]
精彩评论