kolejna poprawa astar

This commit is contained in:
s444349 2020-04-26 12:58:52 +02:00
parent 9127c46938
commit 7cb3aa904c
2 changed files with 7 additions and 3 deletions

View File

@ -5,10 +5,13 @@ def heurystyka(obiekty, a, b):
heur = abs((b[0] - a[0])) + abs((b[1] - a[1])) heur = abs((b[0] - a[0])) + abs((b[1] - a[1]))
if obiekty["plansza"][b[0], b[1]].jestDomem is True: if obiekty["plansza"][b[0], b[1]].jestDomem is True:
heur += 2 heur += 2
if obiekty["plansza"][b[0], b[1]].jestPrzeszkoda is True:
heur += 100
return heur return heur
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 +27,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]
@ -40,9 +44,9 @@ def astar(obiekty, start, cel):
continue continue
tentative_h_score = heurystyka(obiekty, sasiad, cel) + heurystyka(obiekty, current, sasiad) tentative_h_score = heurystyka(obiekty, sasiad, cel) + heurystyka(obiekty, current, sasiad)
if sasiad in close_set and tentative_h_score > hscore.get(current, 0): if sasiad in [i[1] for i in oheap] and tentative_h_score > hscore.get(current, 0):
continue continue
if tentative_h_score <= hscore.get(current, 0) or sasiad not in [i[1] for i in oheap]: if sasiad not in close_set and sasiad not in [i[1] for i in oheap]:
came_from[sasiad] = current came_from[sasiad] = current
hscore[sasiad] = tentative_h_score hscore[sasiad] = tentative_h_score
fscore[sasiad] = tentative_h_score + gscore[current] fscore[sasiad] = tentative_h_score + gscore[current]

View File

@ -57,7 +57,7 @@ class Smieciarka(pygame.sprite.Sprite):
self.w_dol() self.w_dol()
def astar_move(self, obiekty): 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) print(sciezka)
for koord in sciezka: for koord in sciezka:
if koord[0] == self.x - 1 and koord[1] == self.y: if koord[0] == self.x - 1 and koord[1] == self.y: