From a19449a1c4b8b6deae70991bb03e57d9644bb85d Mon Sep 17 00:00:00 2001 From: s444349 Date: Sat, 25 Apr 2020 23:03:25 +0200 Subject: [PATCH] algorytm astar v1.1 --- astar.py | 24 +++++++++++------------- game.py | 2 +- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/astar.py b/astar.py index 0a35700..182ccac 100644 --- a/astar.py +++ b/astar.py @@ -1,20 +1,21 @@ import heapq -def heurystyka(a, b): - return abs((b[0] - a[0])) + abs((b[1] - a[1])) +def heurystyka(obiekty, a, b): + heur = abs((b[0] - a[0])) + abs((b[1] - a[1])) + if obiekty["plansza"][b[0], b[1]].jestDomem is True: + heur += 2 + return heur -def astar(start, cel): - import game - - obiekty = game.utworzObiekty() +def astar(obiekty, start, cel): sasiedzi = [(0, 1), (0, -1), (1, 0), (-1, 0)] close_set = set() came_from = {} gscore = {start: 0} - fscore = {start: heurystyka(start, cel)} + fscore = {start: heurystyka(obiekty, start, cel)} oheap = [] + dodatkowy_koszt = 0 heapq.heappush(oheap, (fscore[start], start)) while oheap: current = heapq.heappop(oheap)[1] @@ -27,22 +28,19 @@ def astar(start, cel): close_set.add(current) for i, j in sasiedzi: sasiad = current[0] + i, current[1] + j - tentative_g_score = gscore[current] + heurystyka(current, sasiad) + if 14 < sasiad[0] or sasiad[0] < 0: continue elif 14 < sasiad[1] or sasiad[1] < 0: continue elif 6 <= sasiad[0] <= 7 and 10 <= sasiad[1] <= 11: continue - elif 4 < sasiad[0] and 4 < sasiad[1]: - continue - elif 4 > sasiad[0] and 4 > sasiad[1]: - continue + tentative_g_score = gscore[current] + heurystyka(obiekty, current, sasiad) if sasiad in close_set and tentative_g_score >= gscore.get(sasiad, 0): continue if tentative_g_score < gscore.get(sasiad, 0) or sasiad not in [i[1] for i in oheap]: came_from[sasiad] = current gscore[sasiad] = tentative_g_score - fscore[sasiad] = tentative_g_score + heurystyka(sasiad, cel) + fscore[sasiad] = tentative_g_score + heurystyka(obiekty, sasiad, cel) heapq.heappush(oheap, (fscore[sasiad], sasiad)) return False diff --git a/game.py b/game.py index efd970c..c8e6969 100644 --- a/game.py +++ b/game.py @@ -73,7 +73,7 @@ def game(): clock.tick(7) #start = obiekty["plansza"][0, 14] #koniec = obiekty["plansza"][14, 0] - print(astar.astar((0, 14), (14, 0))) + print(astar.astar(obiekty, (0, 14), (14, 0))) #print(len(astar.astar(start, koniec))) pygame.quit()