I can't seems to find a good way to solve this problem I'm working on.
Suppose I have a point on a 2D grid describing the location of a character(board game):
[n,m]
Now, on each turn I can move the character depending on the roll of a dice (1,2,3...), and I want to find all possible ways the character can move.
Moving the character once means changing only n
or m
in which a diagonal movement counts as 2 movements (i.e. [n+1,m] moved once, [n+1,m+1] moved twice).
For example, if I roll a 2 then all possible movements are:
[n,m+2]
[n-1,m+1][n,m+1][n+1,m+1]
[n-2,m][n-1,m][n,m][n+1,m][n+2,m]
[n-1,m-1][n,m-1][n+1,m-1]
[n,m-2]
I can't seem to get a clear algorithm for this.
What I have so far is another suggestion from Daniel Lew
position = [3,2] #position
n = 2 #number of moves allowed
for i in range(position[0]-n,position[0]+n):
for j in range(position[1]-n,position[1]+n):
if abs(position[0]-i) + abs(position[1]-j) <= n:
print i,j,'works\n'
However, it misses the [n][m+2],[n+2,m]
(or the way I wrote it misses it).
Any suggestions for concise answers or fixes?
With Python 2.7:
If you wanted to test it with PyGame, you could do it like:
In this example, the character can move 50 steps. If you move around a point keeping the same distance to the center, the result is a circle. If you can go anywhere inside that circle, you get the cartesian coordinates of a disk.