From 42c0a6ccfc2b3856032b86d6d339f2d8ea92a00a Mon Sep 17 00:00:00 2001 From: Adam Osiowy Date: Sun, 26 Apr 2020 12:18:15 +0200 Subject: [PATCH 1/3] dodany test sprawdzajacy dlugosc sciezki --- astar.py | 5 +++++ modele.py | 21 +++++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/astar.py b/astar.py index 182ccac..81dccac 100644 --- a/astar.py +++ b/astar.py @@ -12,6 +12,7 @@ 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)} gscore = {start: 0} fscore = {start: heurystyka(obiekty, start, cel)} oheap = [] @@ -20,12 +21,14 @@ def astar(obiekty, start, cel): while oheap: current = heapq.heappop(oheap)[1] if current == cel: + obiekty["plansza"][current[0], current[1]].setKolor((255, 0, 0)) data = [] while current in came_from: data.append(current) current = came_from[current] return data[::-1] close_set.add(current) + obiekty["plansza"][current[0], current[1]].setKolor((255, 0, 0)) for i, j in sasiedzi: sasiad = current[0] + i, current[1] + j @@ -36,6 +39,7 @@ def astar(obiekty, start, cel): elif 6 <= sasiad[0] <= 7 and 10 <= sasiad[1] <= 11: 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]: @@ -43,4 +47,5 @@ def astar(obiekty, start, cel): gscore[sasiad] = tentative_g_score fscore[sasiad] = tentative_g_score + heurystyka(obiekty, sasiad, cel) heapq.heappush(oheap, (fscore[sasiad], sasiad)) + obiekty["plansza"][sasiad[0], sasiad[1]].setKolor((0, 255, 0)) return False diff --git a/modele.py b/modele.py index a52d992..45f504f 100644 --- a/modele.py +++ b/modele.py @@ -57,17 +57,18 @@ class Smieciarka(pygame.sprite.Sprite): self.w_dol() def astar_move(self, obiekty): - sciezka = astar.astar(obiekty, self.pozycja, (14, 0)) + sciezka = astar.astar(obiekty, self.pozycja, (random.randrange(15), random.randrange(15))) print(sciezka) - for koord in sciezka: - if koord[0] == self.x - 1 and koord[1] == self.y: - self.w_lewo() - elif koord[0] == self.x + 1 and koord[1] == self.y: - self.w_prawo() - elif koord[0] == self.x and koord[1] + 1 == self.y: - self.w_gore() - elif koord[0] == self.x and koord[1] - 1 == self.y: - self.w_dol() + if sciezka: + for koord in sciezka: + if koord[0] == self.x - 1 and koord[1] == self.y: + self.w_lewo() + elif koord[0] == self.x + 1 and koord[1] == self.y: + self.w_prawo() + elif koord[0] == self.x and koord[1] + 1 == self.y: + self.w_gore() + elif koord[0] == self.x and koord[1] - 1 == self.y: + self.w_dol() print("skonczylem") def w_lewo(self): From 29734988bd38afc66812d607b3b385fde80c8052 Mon Sep 17 00:00:00 2001 From: Kacper Borkowski Date: Sun, 26 Apr 2020 12:08:58 +0000 Subject: [PATCH 2/3] Zaktualizuj 'astar.py' --- astar.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/astar.py b/astar.py index 81dccac..63245ea 100644 --- a/astar.py +++ b/astar.py @@ -9,6 +9,7 @@ def heurystyka(obiekty, a, b): def astar(obiekty, start, cel): + sasiedzi = [(0, 1), (0, -1), (1, 0), (-1, 0)] close_set = set() came_from = {} @@ -24,6 +25,7 @@ def astar(obiekty, start, cel): obiekty["plansza"][current[0], current[1]].setKolor((255, 0, 0)) data = [] while current in came_from: + data.append(current) current = came_from[current] return data[::-1] @@ -38,14 +40,15 @@ def astar(obiekty, start, cel): continue elif 6 <= sasiad[0] <= 7 and 10 <= sasiad[1] <= 11: 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 - 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 - gscore[sasiad] = tentative_g_score - fscore[sasiad] = tentative_g_score + heurystyka(obiekty, sasiad, cel) + hscore[sasiad] = tentative_h_score + fscore[sasiad] = tentative_h_score + gscore[current] + gscore[sasiad] = gscore[current] + heurystyka(obiekty, current, sasiad) heapq.heappush(oheap, (fscore[sasiad], sasiad)) obiekty["plansza"][sasiad[0], sasiad[1]].setKolor((0, 255, 0)) return False From 9c093c028edacdd45ef72b1bf69af6262d292a94 Mon Sep 17 00:00:00 2001 From: Kacper Borkowski Date: Sun, 26 Apr 2020 12:09:47 +0000 Subject: [PATCH 3/3] Zaktualizuj 'modele.py' --- modele.py | 1 - 1 file changed, 1 deletion(-) diff --git a/modele.py b/modele.py index 45f504f..1f5c36b 100644 --- a/modele.py +++ b/modele.py @@ -69,7 +69,6 @@ class Smieciarka(pygame.sprite.Sprite): self.w_gore() elif koord[0] == self.x and koord[1] - 1 == self.y: self.w_dol() - print("skonczylem") def w_lewo(self): if self.x > 0: