algorytm astar v1
This commit is contained in:
parent
97d97a9c31
commit
1b1451452c
48
astar.py
Normal file
48
astar.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
import heapq
|
||||||
|
|
||||||
|
|
||||||
|
def heurystyka(a, b):
|
||||||
|
return abs((b[0] - a[0])) + abs((b[1] - a[1]))
|
||||||
|
|
||||||
|
|
||||||
|
def astar(start, cel):
|
||||||
|
import game
|
||||||
|
|
||||||
|
obiekty = game.utworzObiekty()
|
||||||
|
sasiedzi = [(0, 1), (0, -1), (1, 0), (-1, 0)]
|
||||||
|
close_set = set()
|
||||||
|
came_from = {}
|
||||||
|
gscore = {start: 0}
|
||||||
|
fscore = {start: heurystyka(start, cel)}
|
||||||
|
oheap = []
|
||||||
|
heapq.heappush(oheap, (fscore[start], start))
|
||||||
|
while oheap:
|
||||||
|
current = heapq.heappop(oheap)[1]
|
||||||
|
if current == cel:
|
||||||
|
data = []
|
||||||
|
while current in came_from:
|
||||||
|
data.append(current)
|
||||||
|
current = came_from[current]
|
||||||
|
return data[::-1]
|
||||||
|
close_set.add(current)
|
||||||
|
for i, j in sasiedzi:
|
||||||
|
sasiad = current[0] + i, current[1] + j
|
||||||
|
tentative_g_score = gscore[current] + heurystyka(current, sasiad)
|
||||||
|
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
|
||||||
|
elif 4 < sasiad[0] and 4 < sasiad[1]:
|
||||||
|
continue
|
||||||
|
elif 4 > sasiad[0] and 4 > sasiad[1]:
|
||||||
|
continue
|
||||||
|
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]:
|
||||||
|
came_from[sasiad] = current
|
||||||
|
gscore[sasiad] = tentative_g_score
|
||||||
|
fscore[sasiad] = tentative_g_score + heurystyka(sasiad, cel)
|
||||||
|
heapq.heappush(oheap, (fscore[sasiad], sasiad))
|
||||||
|
return False
|
6
game.py
6
game.py
@ -4,6 +4,7 @@ import numpy as np
|
|||||||
import random
|
import random
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
import astar
|
||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
|
||||||
@ -67,8 +68,13 @@ def game():
|
|||||||
if event.key == pygame.K_DOWN:
|
if event.key == pygame.K_DOWN:
|
||||||
obiekty["smieciarka"].w_dol()
|
obiekty["smieciarka"].w_dol()
|
||||||
|
|
||||||
|
|
||||||
obiekty["smieciarka"].rand_move()
|
obiekty["smieciarka"].rand_move()
|
||||||
clock.tick(7)
|
clock.tick(7)
|
||||||
|
#start = obiekty["plansza"][0, 14]
|
||||||
|
#koniec = obiekty["plansza"][14, 0]
|
||||||
|
print(astar.astar((0, 14), (14, 0)))
|
||||||
|
#print(len(astar.astar(start, koniec)))
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
||||||
|
|
||||||
|
23
modele.py
23
modele.py
@ -139,10 +139,10 @@ class Smieciarka(pygame.sprite.Sprite):
|
|||||||
def getOdwiedzoneDomy(self):
|
def getOdwiedzoneDomy(self):
|
||||||
return self.odwiedzone_domy
|
return self.odwiedzone_domy
|
||||||
|
|
||||||
def setPlansza(self,plansza):
|
def setPlansza(self, plansza):
|
||||||
self.plansza = plansza
|
self.plansza = plansza
|
||||||
|
|
||||||
def setObiekty(self,obiekty):
|
def setObiekty(self, obiekty):
|
||||||
self.obiekty = obiekty
|
self.obiekty = obiekty
|
||||||
|
|
||||||
|
|
||||||
@ -190,21 +190,40 @@ class Kratka(pygame.sprite.Sprite):
|
|||||||
def __init__(self, poz_x, poz_y):
|
def __init__(self, poz_x, poz_y):
|
||||||
self.pozX = poz_x
|
self.pozX = poz_x
|
||||||
self.pozY = poz_y
|
self.pozY = poz_y
|
||||||
|
self.pozycja = (self.pozX, self.pozY)
|
||||||
|
self.rodzic = None
|
||||||
self.jestDomem = False
|
self.jestDomem = False
|
||||||
self.jestKontenerem = False
|
self.jestKontenerem = False
|
||||||
self.jestWysypiskiem = False
|
self.jestWysypiskiem = False
|
||||||
self.jestPrzeszkoda = False
|
self.jestPrzeszkoda = False
|
||||||
self.kolor = GREY
|
self.kolor = GREY
|
||||||
self.obiekt = None
|
self.obiekt = None
|
||||||
|
self.g = 0 # Distance to start node
|
||||||
|
self.h = 0 # Distance to goal node
|
||||||
|
self.f = 0 # Total cost
|
||||||
pygame.sprite.Sprite.__init__(self)
|
pygame.sprite.Sprite.__init__(self)
|
||||||
self.image = pygame.image.__class__
|
self.image = pygame.image.__class__
|
||||||
self.rect = pygame.Rect(self.pozX * WIDTH + MARGIN * self.pozX + MARGIN,
|
self.rect = pygame.Rect(self.pozX * WIDTH + MARGIN * self.pozX + MARGIN,
|
||||||
self.pozY * HEIGHT + MARGIN * self.pozY + MARGIN,
|
self.pozY * HEIGHT + MARGIN * self.pozY + MARGIN,
|
||||||
WIDTH, HEIGHT)
|
WIDTH, HEIGHT)
|
||||||
|
|
||||||
|
# Sort nodes
|
||||||
|
def __lt__(self, other):
|
||||||
|
return self.f < other.f
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return ('{0}'.format(self.pozycja))
|
||||||
|
|
||||||
|
# Compare nodes
|
||||||
|
def __eq__(self, other):
|
||||||
|
return True if self.pozycja == other.pozycja else False
|
||||||
|
|
||||||
def setImage(self, image):
|
def setImage(self, image):
|
||||||
self.image = image
|
self.image = image
|
||||||
|
|
||||||
|
def setRodzic(self, rodzic):
|
||||||
|
self.rodzic = rodzic
|
||||||
|
|
||||||
def setObiekt(self, obiekt):
|
def setObiekt(self, obiekt):
|
||||||
self.obiekt = obiekt
|
self.obiekt = obiekt
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user