route function takes start, stop, and matrixs, returns path

This commit is contained in:
Cameron Smart
2020-04-20 02:27:28 -07:00
parent de72eed2c7
commit 6561469351

View File

@@ -2,27 +2,20 @@ from pathfinding.core.diagonal_movement import DiagonalMovement
from pathfinding.core.grid import Grid from pathfinding.core.grid import Grid
from pathfinding.finder.a_star import AStarFinder from pathfinding.finder.a_star import AStarFinder
# 0 is an unwalkable block. Numbers larger than 0 are walkable. def route(start, end, matrix) -> path:
# The higher the number the harder it is to walk on. """
matrix = [ Take a matrix of the level in the form wighted numbers, a start and stop point, and return a path between them.
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[0, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
param: start: (x, y) location of the monster
param: end: (x, y) location of the player
param: matrix: a 2d list of the level. 0s are walls, numbers greater than 0 are weighted
"""
grid = Grid(matrix=matrix) grid = Grid(matrix=matrix)
start = grid.node(1, 1) start = grid.node(start[0],start[1])
end = grid.node(8, 8) end = grid.node(end[0], end[1])
finder = AStarFinder(diagonal_movement=DiagonalMovement.always) finder = AStarFinder(diagonal_movement=DiagonalMovement.always)
path, runs = finder.find_path(start, end, grid) path, runs = finder.find_path(start, end, grid)
print('operations:', runs, 'path length:', len(path)) #grid.grid_str(path=path, start=start, end=end)
print(grid.grid_str(path=path, start=start, end=end)) return path