Calculating orthogonal movement

72 Views Asked by At

A board game of 8 * 8.
One of the players is able to move using orthogonal movement, up to a max of 3 spaces.
How do I calculate the possible steps he can take?

1

There are 1 best solutions below

9
Tim Roberts On

What you need is called "Manhattan Distance". It's the distance between two cells using only horizontal and vertical steps. So, you can compute the list of all cells where the Manhattan distance is 3 or less. So, consider:

M = (0,4)

def mdist(a,b):
    return abs(a[0]-b[0])+abs(a[1]-b[1])

def find_possible(M):
    possibles = []    
    for y in range(ROWS):
        for x in range(COLS):
            md = mdist(M, (x,y))
            if 1 <= md <= 3:
                possibles.append( (x,y) )
    return possibles