SI_InteligentnyWozekWidlowy/util/PathVisualiser.py

44 lines
1.4 KiB
Python
Raw Normal View History

2022-04-08 00:43:25 +02:00
# thanks to @m1sp <Jaiden Mispy> for this simpler version of
# reconstruct_path that doesn't have duplicate entries
from typing import Dict, List
from util.Pathfinder import Location
def reconstruct_path(came_from: Dict[Location, Location],
start: Location, goal: Location) -> List[Location]:
current: Location = goal
path: List[Location] = []
while current != start: # note: this will fail if no path found
path.append(current)
current = came_from[current]
path.append(start) # optional
path.reverse() # optional
return path
def draw_grid(graph, **style):
print("___" * graph.width)
for y in range(graph.height):
for x in range(graph.width):
print("%s" % draw_tile(graph, (x, y), style), end="")
print()
print("~~~" * graph.width)
def draw_tile(graph, id, style):
r = " . "
if 'number' in style and id in style['number']: r = " %-2d" % style['number'][id]
if 'point_to' in style and style['point_to'].get(id, None) is not None:
(x1, y1) = id
(x2, y2) = style['point_to'][id]
if x2 == x1 + 1: r = " > "
if x2 == x1 - 1: r = " < "
if y2 == y1 + 1: r = " v "
if y2 == y1 - 1: r = " ^ "
if 'path' in style and id in style['path']: r = " @ "
if 'start' in style and id == style['start']: r = " A "
if 'goal' in style and id == style['goal']: r = " Z "
if id in graph.walls: r = "###"
return r