Zaktualizuj 'astar.py'

This commit is contained in:
Kacper Borkowski 2020-04-26 12:08:58 +00:00
parent 42c0a6ccfc
commit 29734988bd

View File

@ -9,6 +9,7 @@ def heurystyka(obiekty, a, b):
def astar(obiekty, start, cel): def astar(obiekty, start, cel):
sasiedzi = [(0, 1), (0, -1), (1, 0), (-1, 0)] sasiedzi = [(0, 1), (0, -1), (1, 0), (-1, 0)]
close_set = set() close_set = set()
came_from = {} came_from = {}
@ -24,6 +25,7 @@ def astar(obiekty, start, cel):
obiekty["plansza"][current[0], current[1]].setKolor((255, 0, 0)) obiekty["plansza"][current[0], current[1]].setKolor((255, 0, 0))
data = [] data = []
while current in came_from: while current in came_from:
data.append(current) data.append(current)
current = came_from[current] current = came_from[current]
return data[::-1] return data[::-1]
@ -38,14 +40,15 @@ def astar(obiekty, start, cel):
continue continue
elif 6 <= sasiad[0] <= 7 and 10 <= sasiad[1] <= 11: elif 6 <= sasiad[0] <= 7 and 10 <= sasiad[1] <= 11:
continue continue
tentative_g_score = gscore[current] + heurystyka(obiekty, current, sasiad) tentative_h_score = heurystyka(obiekty, sasiad, cel) + heurystyka(obiekty, current, sasiad)
if sasiad in close_set and tentative_g_score >= gscore.get(sasiad, 0): if sasiad in [i[1] for i in oheap] and tentative_h_score < hscore.get(current, 0):
continue continue
if tentative_g_score < gscore.get(sasiad, 0) or sasiad not in [i[1] for i in oheap]: elif sasiad not in close_set and sasiad not in [i[1] for i in oheap]:
came_from[sasiad] = current came_from[sasiad] = current
gscore[sasiad] = tentative_g_score hscore[sasiad] = tentative_h_score
fscore[sasiad] = tentative_g_score + heurystyka(obiekty, sasiad, cel) fscore[sasiad] = tentative_h_score + gscore[current]
gscore[sasiad] = gscore[current] + heurystyka(obiekty, current, sasiad)
heapq.heappush(oheap, (fscore[sasiad], sasiad)) heapq.heappush(oheap, (fscore[sasiad], sasiad))
obiekty["plansza"][sasiad[0], sasiad[1]].setKolor((0, 255, 0)) obiekty["plansza"][sasiad[0], sasiad[1]].setKolor((0, 255, 0))
return False return False