My solution for the maze problem. It’s a few lines shorter and might be clearer for some people. Hope it will help someone!
def is_legal_move(maze, position):
return (0 <= position.x < len(maze) and 0 <= position.y < len(maze) and maze[position.y][position.x] == 0)
def search_maze(maze, current, end, path=[]):
path = path + [current]
if current == end:
return path
for next_move in (Coordinate(current.x - 1, current.y), Coordinate(current.x + 1, current.y), Coordinate(current.x, current.y + 1), Coordinate(current.x, current.y - 1)):
if next_move not in path and is_legal_move(maze,current):
tested_path = search_maze(maze, next_move, end, path)
if tested_path is not None:
return tested_path
return None