diff --git a/astar.py b/astar.py index 0978df4..4a8758d 100644 --- a/astar.py +++ b/astar.py @@ -1,25 +1,28 @@ import heapq -def heurystyka(obiekty, a, b): - heur = abs((b[0] - a[0])) + abs((b[1] - a[1])) +def heurystyka(a, b): + return abs((b[0] - a[0])) + abs((b[1] - a[1])) + + +def stepcost(obiekty, b): + cost = 1 if obiekty["plansza"][b[0], b[1]].jestDomem is True: - heur += 2 + cost += 2 if obiekty["plansza"][b[0], b[1]].jestWysypiskiem is True: - heur += 1 + cost += 1 if obiekty["plansza"][b[0], b[1]].jestKontenerem is True: - heur += 2 - return heur + cost += 2 + return cost def astar(obiekty, start, cel): - sasiedzi = [(0, 1), (0, -1), (1, 0), (-1, 0)] close_set = set() came_from = {} - hscore = {start: heurystyka(obiekty, start, cel)} + hscore = {start: heurystyka(start, cel)} gscore = {start: 0} - fscore = {start: heurystyka(obiekty, start, cel)} + fscore = {start: heurystyka(start, cel)} oheap = [] heapq.heappush(oheap, (fscore[start], start)) while oheap: @@ -27,7 +30,6 @@ def astar(obiekty, start, cel): if current == cel: data = [] while current in came_from: - data.append(current) current = came_from[current] return data[::-1] @@ -41,7 +43,7 @@ def astar(obiekty, start, cel): continue elif 6 <= sasiad[0] <= 7 and 10 <= sasiad[1] <= 11: continue - tentative_h_score = heurystyka(obiekty, sasiad, cel) + heurystyka(obiekty, current, sasiad) + tentative_h_score = heurystyka(sasiad, cel) + stepcost(obiekty, sasiad) if sasiad in [i[1] for i in oheap] and tentative_h_score < hscore.get(current, 0): continue @@ -49,6 +51,6 @@ def astar(obiekty, start, cel): came_from[sasiad] = current hscore[sasiad] = tentative_h_score fscore[sasiad] = tentative_h_score + gscore[current] - gscore[sasiad] = gscore[current] + heurystyka(obiekty, current, sasiad) + gscore[sasiad] = gscore[current] + heurystyka(current, sasiad) heapq.heappush(oheap, (fscore[sasiad], sasiad)) return False diff --git a/game.py b/game.py index ec34c3d..ba51b2e 100644 --- a/game.py +++ b/game.py @@ -79,12 +79,12 @@ def game(): (obiekty["kontener_organiczne"].x, obiekty["kontener_organiczne"].y)] temp = False while nieodwiedzone_domy: - nieodwiedzone_domy.sort(key=lambda x: astar.heurystyka(obiekty, (obiekty["smieciarka"].x, obiekty["smieciarka"].y), x)) + nieodwiedzone_domy.sort(key=lambda x: astar.heurystyka((obiekty["smieciarka"].x, obiekty["smieciarka"].y), x)) cel = nieodwiedzone_domy.pop(0) obiekty["smieciarka"].astar_move(obiekty, (obiekty["smieciarka"].x, obiekty["smieciarka"].y), cel) while nieodwiedzone_kontenery: - nieodwiedzone_kontenery.sort(key=lambda x: astar.heurystyka(obiekty, (obiekty["smieciarka"].x, obiekty["smieciarka"].y), x)) + nieodwiedzone_kontenery.sort(key=lambda x: astar.heurystyka((obiekty["smieciarka"].x, obiekty["smieciarka"].y), x)) cel = nieodwiedzone_kontenery.pop(0) obiekty["smieciarka"].astar_move(obiekty, (obiekty["smieciarka"].x, obiekty["smieciarka"].y), cel)