1#You can install this module with >>pip install pathfinding
2
3#You first need to import the module
4from pathfinding.core.diagonal_movement import DiagonalMovement
5from pathfinding.core.grid import Grid
6from pathfinding.finder.a_star import AStarFinder
7
8#Then, create a grid. The 0 are obstacles.
9matrix = [
10 [1, 1, 1],
11 [1, 0, 1],
12 [1, 1, 1]
13]
14grid = Grid(matrix=matrix)
15
16#You need now to set the start and the end.
17start = grid.node(0, 0)
18end = grid.node(2, 2)
19
20#This line allow your program to move with diagonal movements.
21finder = AStarFinder(diagonal_movement=DiagonalMovement.always)
22
23#Now you can run find_path.
24path, runs = finder.find_path(start, end, grid)
25
26print('operations:', runs, 'path length:', len(path))
27print(grid.grid_str(path=path, start=start, end=end))
28#If you want the list of instructions, print(path)