algorytm astar v1.1
This commit is contained in:
parent
1b1451452c
commit
a19449a1c4
24
astar.py
24
astar.py
@ -1,20 +1,21 @@
|
|||||||
import heapq
|
import heapq
|
||||||
|
|
||||||
|
|
||||||
def heurystyka(a, b):
|
def heurystyka(obiekty, a, b):
|
||||||
return 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:
|
||||||
|
heur += 2
|
||||||
|
return heur
|
||||||
|
|
||||||
|
|
||||||
def astar(start, cel):
|
def astar(obiekty, start, cel):
|
||||||
import game
|
|
||||||
|
|
||||||
obiekty = game.utworzObiekty()
|
|
||||||
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 = {}
|
||||||
gscore = {start: 0}
|
gscore = {start: 0}
|
||||||
fscore = {start: heurystyka(start, cel)}
|
fscore = {start: heurystyka(obiekty, start, cel)}
|
||||||
oheap = []
|
oheap = []
|
||||||
|
dodatkowy_koszt = 0
|
||||||
heapq.heappush(oheap, (fscore[start], start))
|
heapq.heappush(oheap, (fscore[start], start))
|
||||||
while oheap:
|
while oheap:
|
||||||
current = heapq.heappop(oheap)[1]
|
current = heapq.heappop(oheap)[1]
|
||||||
@ -27,22 +28,19 @@ def astar(start, cel):
|
|||||||
close_set.add(current)
|
close_set.add(current)
|
||||||
for i, j in sasiedzi:
|
for i, j in sasiedzi:
|
||||||
sasiad = current[0] + i, current[1] + j
|
sasiad = current[0] + i, current[1] + j
|
||||||
tentative_g_score = gscore[current] + heurystyka(current, sasiad)
|
|
||||||
if 14 < sasiad[0] or sasiad[0] < 0:
|
if 14 < sasiad[0] or sasiad[0] < 0:
|
||||||
continue
|
continue
|
||||||
elif 14 < sasiad[1] or sasiad[1] < 0:
|
elif 14 < sasiad[1] or sasiad[1] < 0:
|
||||||
continue
|
continue
|
||||||
elif 6 <= sasiad[0] <= 7 and 10 <= sasiad[1] <= 11:
|
elif 6 <= sasiad[0] <= 7 and 10 <= sasiad[1] <= 11:
|
||||||
continue
|
continue
|
||||||
elif 4 < sasiad[0] and 4 < sasiad[1]:
|
tentative_g_score = gscore[current] + heurystyka(obiekty, current, sasiad)
|
||||||
continue
|
|
||||||
elif 4 > sasiad[0] and 4 > sasiad[1]:
|
|
||||||
continue
|
|
||||||
if sasiad in close_set and tentative_g_score >= gscore.get(sasiad, 0):
|
if sasiad in close_set and tentative_g_score >= gscore.get(sasiad, 0):
|
||||||
continue
|
continue
|
||||||
if tentative_g_score < gscore.get(sasiad, 0) or sasiad not in [i[1] for i in oheap]:
|
if tentative_g_score < gscore.get(sasiad, 0) or sasiad not in [i[1] for i in oheap]:
|
||||||
came_from[sasiad] = current
|
came_from[sasiad] = current
|
||||||
gscore[sasiad] = tentative_g_score
|
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))
|
heapq.heappush(oheap, (fscore[sasiad], sasiad))
|
||||||
return False
|
return False
|
||||||
|
2
game.py
2
game.py
@ -73,7 +73,7 @@ def game():
|
|||||||
clock.tick(7)
|
clock.tick(7)
|
||||||
#start = obiekty["plansza"][0, 14]
|
#start = obiekty["plansza"][0, 14]
|
||||||
#koniec = obiekty["plansza"][14, 0]
|
#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)))
|
#print(len(astar.astar(start, koniec)))
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user