2020-04-25 22:30:39 +02:00
|
|
|
import heapq
|
|
|
|
|
|
|
|
|
2020-04-25 23:03:25 +02:00
|
|
|
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
|
2020-04-26 12:58:52 +02:00
|
|
|
if obiekty["plansza"][b[0], b[1]].jestPrzeszkoda is True:
|
|
|
|
heur += 100
|
2020-04-25 23:03:25 +02:00
|
|
|
return heur
|
2020-04-25 22:30:39 +02:00
|
|
|
|
|
|
|
|
2020-04-25 23:03:25 +02:00
|
|
|
def astar(obiekty, start, cel):
|
2020-04-26 12:58:52 +02:00
|
|
|
|
2020-04-25 22:30:39 +02:00
|
|
|
sasiedzi = [(0, 1), (0, -1), (1, 0), (-1, 0)]
|
|
|
|
close_set = set()
|
|
|
|
came_from = {}
|
2020-04-26 11:14:26 +02:00
|
|
|
hscore = {start: heurystyka(obiekty, start, cel)}
|
2020-04-25 22:30:39 +02:00
|
|
|
gscore = {start: 0}
|
2020-04-25 23:03:25 +02:00
|
|
|
fscore = {start: heurystyka(obiekty, start, cel)}
|
2020-04-25 22:30:39 +02:00
|
|
|
oheap = []
|
2020-04-25 23:03:25 +02:00
|
|
|
dodatkowy_koszt = 0
|
2020-04-25 22:30:39 +02:00
|
|
|
heapq.heappush(oheap, (fscore[start], start))
|
|
|
|
while oheap:
|
|
|
|
current = heapq.heappop(oheap)[1]
|
|
|
|
if current == cel:
|
2020-04-26 11:14:26 +02:00
|
|
|
obiekty["plansza"][current[0], current[1]].setKolor((255, 0, 0))
|
2020-04-25 22:30:39 +02:00
|
|
|
data = []
|
|
|
|
while current in came_from:
|
2020-04-26 12:58:52 +02:00
|
|
|
|
2020-04-25 22:30:39 +02:00
|
|
|
data.append(current)
|
|
|
|
current = came_from[current]
|
|
|
|
return data[::-1]
|
|
|
|
close_set.add(current)
|
2020-04-26 11:14:26 +02:00
|
|
|
obiekty["plansza"][current[0], current[1]].setKolor((255, 0, 0))
|
2020-04-25 22:30:39 +02:00
|
|
|
for i, j in sasiedzi:
|
|
|
|
sasiad = current[0] + i, current[1] + j
|
2020-04-25 23:03:25 +02:00
|
|
|
|
2020-04-25 22:30:39 +02:00
|
|
|
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
|
2020-04-26 14:12:08 +02:00
|
|
|
tentative_h_score = heurystyka(
|
|
|
|
obiekty, sasiad, cel) + heurystyka(obiekty, current, sasiad)
|
2020-04-26 11:14:26 +02:00
|
|
|
|
2020-04-26 14:08:58 +02:00
|
|
|
if sasiad in [i[1] for i in oheap] and tentative_h_score < hscore.get(current, 0):
|
2020-04-25 22:30:39 +02:00
|
|
|
continue
|
2020-04-26 14:08:58 +02:00
|
|
|
elif sasiad not in close_set and sasiad not in [i[1] for i in oheap]:
|
2020-04-25 22:30:39 +02:00
|
|
|
came_from[sasiad] = current
|
2020-04-26 11:14:26 +02:00
|
|
|
hscore[sasiad] = tentative_h_score
|
|
|
|
fscore[sasiad] = tentative_h_score + gscore[current]
|
2020-04-26 14:11:19 +02:00
|
|
|
gscore[sasiad] = gscore[current] + \
|
|
|
|
heurystyka(obiekty, current, sasiad)
|
2020-04-25 22:30:39 +02:00
|
|
|
heapq.heappush(oheap, (fscore[sasiad], sasiad))
|
2020-04-26 11:14:26 +02:00
|
|
|
obiekty["plansza"][sasiad[0], sasiad[1]].setKolor((0, 255, 0))
|
2020-04-25 22:30:39 +02:00
|
|
|
return False
|