Now I’ve had chance to run it and figure out the rules,

I think it would be simple enough to detect ‘no more moves’ from a numbered tile.

The special tiles should be relatively simple since they only need at least 1 free tile in a row or a column (K & Q), or 1 free tile anywhere (!).

Really it’s just a matter of:

- Does the player have any more free turns?
- Can the player move exactly N tiles?

The first check is easy. The second check is a bit more complicated,

but it would essentially just be a variant of flood-fill and/or Dijkstra’s algorithm.

(Some of that explanation is a bit more complicated than you need to worry about because your ‘nodes’ (tiles) aren’t weighted. Although technically you could look at it as a turned tile having a distance of infinity (or at least greater than N).)

However, since you know the player can only visit tiles within an manhattan distance of N,

you can limit the search to just those tiles:

```
grid_width = 7
grid_height = 7
def get_card_index(x, y):
return ((y * grid_width) + x)
def get_card(x, y):
index = get_card_index(x, y)
return cardList[index]
def manhattan_distance(x0, y0, x1, y1):
return (abs(x0 - x1) + abs(x0 - x1))
# Possibly better version
def get_dijkstra_candidates(x0, y0, limit, cards):
return [(x1, y1) for y1 in range(y0 - limit, y0 + limit + 1) for x1 in range(x0 - limit, x0 + limit + 1)]
# Alternative version
def alternative_get_dijkstra_candidates(x0, y0, limit, cards):
return [(x1, y1) for y1 in range(grid_height) for x1 in range(grid_width) if (manhattan_distance(x0, y0, x1, y1) <= limit)]
```

(Ironically if the player didn’t have to move exactly N tiles then figuring this out would be a lot easier.)

(**Edit:** Fixed the code and figured out a possibly better way of getting the candidate coordinates in the process.)

Normally I’d have a go at modifying your code to do this,

but since this is a competition I think it would be unfair to actually contribute code.

At least until after the contest is over.

By the way, Python does have `True`

and `False`

, like most civilised languages.