'Calculating orthogonal movement

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?



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Roberts