dodany test sprawdzajacy dlugosc sciezki
This commit is contained in:
parent
83c00349d5
commit
42c0a6ccfc
5
astar.py
5
astar.py
@ -12,6 +12,7 @@ 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 = {}
|
||||||
|
hscore = {start: heurystyka(obiekty, start, cel)}
|
||||||
gscore = {start: 0}
|
gscore = {start: 0}
|
||||||
fscore = {start: heurystyka(obiekty, start, cel)}
|
fscore = {start: heurystyka(obiekty, start, cel)}
|
||||||
oheap = []
|
oheap = []
|
||||||
@ -20,12 +21,14 @@ def astar(obiekty, start, cel):
|
|||||||
while oheap:
|
while oheap:
|
||||||
current = heapq.heappop(oheap)[1]
|
current = heapq.heappop(oheap)[1]
|
||||||
if current == cel:
|
if current == cel:
|
||||||
|
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]
|
||||||
close_set.add(current)
|
close_set.add(current)
|
||||||
|
obiekty["plansza"][current[0], current[1]].setKolor((255, 0, 0))
|
||||||
for i, j in sasiedzi:
|
for i, j in sasiedzi:
|
||||||
sasiad = current[0] + i, current[1] + j
|
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:
|
elif 6 <= sasiad[0] <= 7 and 10 <= sasiad[1] <= 11:
|
||||||
continue
|
continue
|
||||||
tentative_g_score = gscore[current] + heurystyka(obiekty, current, sasiad)
|
tentative_g_score = gscore[current] + heurystyka(obiekty, current, sasiad)
|
||||||
|
|
||||||
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]:
|
||||||
@ -43,4 +47,5 @@ def astar(obiekty, start, cel):
|
|||||||
gscore[sasiad] = tentative_g_score
|
gscore[sasiad] = tentative_g_score
|
||||||
fscore[sasiad] = tentative_g_score + heurystyka(obiekty, sasiad, cel)
|
fscore[sasiad] = tentative_g_score + heurystyka(obiekty, sasiad, cel)
|
||||||
heapq.heappush(oheap, (fscore[sasiad], sasiad))
|
heapq.heappush(oheap, (fscore[sasiad], sasiad))
|
||||||
|
obiekty["plansza"][sasiad[0], sasiad[1]].setKolor((0, 255, 0))
|
||||||
return False
|
return False
|
||||||
|
21
modele.py
21
modele.py
@ -57,17 +57,18 @@ 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:
|
if sciezka:
|
||||||
if koord[0] == self.x - 1 and koord[1] == self.y:
|
for koord in sciezka:
|
||||||
self.w_lewo()
|
if koord[0] == self.x - 1 and koord[1] == self.y:
|
||||||
elif koord[0] == self.x + 1 and koord[1] == self.y:
|
self.w_lewo()
|
||||||
self.w_prawo()
|
elif koord[0] == self.x + 1 and koord[1] == self.y:
|
||||||
elif koord[0] == self.x and koord[1] + 1 == self.y:
|
self.w_prawo()
|
||||||
self.w_gore()
|
elif koord[0] == self.x and koord[1] + 1 == self.y:
|
||||||
elif koord[0] == self.x and koord[1] - 1 == self.y:
|
self.w_gore()
|
||||||
self.w_dol()
|
elif koord[0] == self.x and koord[1] - 1 == self.y:
|
||||||
|
self.w_dol()
|
||||||
print("skonczylem")
|
print("skonczylem")
|
||||||
|
|
||||||
def w_lewo(self):
|
def w_lewo(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user