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?
Walk through (inclined) lines of diamond and transform into normal coordinates. Python code
Alternative approach: you can shift along the first coordinate in range of
nin both directions. If some moves are left (movesleft), you can shift along the second coordinate in any direction usingmovesleftsteps. The second stage must preserve oddity (parity?), so step 2 is used.pseudocode
Delphi code for n=2 and n=3 gives results
1st method (the same result in another order):