开发者

Getting surrounding square

I have a 2D array of squares that make up a 50 x 50 grid. The top left square has a position of (0,0) and the bottom right has a position of (49,49). I'm creating a computer player and need help trying to efficiently check what the positions of the surrounding squares are (for the AI). For example, if the user is at position (x,y), how can 开发者_Python百科I find out what the positions of the squares are north, south, east and west of it? (I don't need the diagonal positions). Thank you.


North is (x, y-1), South (x, y+1), East(x-1, y), West (x+1, y) Bear in mind to do bounds checking though, ie(check none of the starting grid positions are either 0 or 49 before you try to move in a specific direction that may take you outside of the array/grid bounds. .)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜