feat: moved methodes for state search to Agent.py

This commit is contained in:
Jager72 2024-04-15 22:49:39 +02:00
parent 976de19d18
commit 4d611cadef
2 changed files with 58 additions and 65 deletions

69
app.py
View File

@ -53,85 +53,24 @@ def draw_grid(window, cells, agent):
window.blit(multiplierText, (0, 50)) window.blit(multiplierText, (0, 50))
initBoard() initBoard()
agent = Agent(prefs.SPAWN_POINT[0], prefs.SPAWN_POINT[1], cells)
def bfs(start, target, cells):
queue = deque([(start,[])])
visited = set()
while queue:
current, path = queue.popleft()
if current==target:
return path + [current]
if current in visited:
continue
visited.add(current)
for neighbor in get_neighbors(current, cells):
queue.append((neighbor, path + [current]))
return None
def get_neighbors(cell, cells):
neighbors = []
x, y = cell.X, cell.Y
if x > 0 and not cells[x-1][y].blocking_movement:
neighbors.append(cells[x-1][y])
if x < prefs.GRID_SIZE - 1 and not cells[x+1][y].blocking_movement:
neighbors.append(cells[x+1][y])
if y > 0 and not cells[x][y-1].blocking_movement:
neighbors.append(cells[x][y-1])
if y < prefs.GRID_SIZE - 1 and not cells[x][y+1].blocking_movement:
neighbors.append(cells[x][y+1])
return neighbors
#Wpisujemy miejsce w ktorym znajduje sie agent i miejsce docelowe do funkcji znajdujacej najkrotsza sciezke #Wpisujemy miejsce w ktorym znajduje sie agent i miejsce docelowe do funkcji znajdujacej najkrotsza sciezke
start_cell = cells[5][5] start_cell = cells[5][5]
target_cell = cells[9][11] target_cell = cells[9][11]
shortest_path = bfs(start_cell, target_cell, cells) shortest_path = agent.bfs(start_cell, target_cell, cells)
if shortest_path: if shortest_path:
print("Sciezka: ", [(cell.X, cell.Y) for cell in shortest_path]) print("Sciezka: ", [(cell.X, cell.Y) for cell in shortest_path])
else: else:
print("Brak sciezki") print("Brak sciezki")
path = agent.convert_to_coordinates(shortest_path)
#oddaje tablice punktow jako sciezke agenta
def convert_to_coordinates(shortest_path):
coordinates = [(cell.X, cell.Y) for cell in shortest_path]
return coordinates
path = convert_to_coordinates(shortest_path)
#Wyjmuje pierwsze koordynaty do ruszenia agenta a potem usuwa go z listy
def pop_first_coordinates(coordinates):
if coordinates:
x, y = coordinates.pop(0)
return x, y
else:
print("Lista współrzędnych jest pusta.")
return None, None
agent = Agent(prefs.SPAWN_POINT[0], prefs.SPAWN_POINT[1], cells)
#Funkcja pomocnicza dla watku bo chcemy zeby agent poruszal sie ale zeby to normalnie wygladalo
def sciezkaAgenta():
x,y =pop_first_coordinates(path)
if x is not None and y is not None:
agent.moveto(x,y)
#osobny watek dla sciezki agenta zeby co *iles czasu* poruszal sie tam gdzie mowi path #osobny watek dla sciezki agenta zeby co *iles czasu* poruszal sie tam gdzie mowi path
def watekDlaSciezkiAgenta(): def watekDlaSciezkiAgenta():
while True: while True:
sciezkaAgenta() agent.sciezkaAgenta(agent,path)
time.sleep(1) time.sleep(1)
watek = threading.Thread(target=watekDlaSciezkiAgenta) watek = threading.Thread(target=watekDlaSciezkiAgenta)

View File

@ -73,3 +73,57 @@ class Agent:
else: else:
print("Agent cannot move to this direction") print("Agent cannot move to this direction")
def bfs(self, start, target, cells):
queue = deque([(start,[])])
visited = set()
while queue:
current, path = queue.popleft()
if current==target:
return path + [current]
if current in visited:
continue
visited.add(current)
for neighbor in self.get_neighbors(current, cells):
queue.append((neighbor, path + [current]))
return None
def get_neighbors(self, cell, cells):
neighbors = []
x, y = cell.X, cell.Y
if x > 0 and not cells[x-1][y].blocking_movement:
neighbors.append(cells[x-1][y])
if x < prefs.GRID_SIZE - 1 and not cells[x+1][y].blocking_movement:
neighbors.append(cells[x+1][y])
if y > 0 and not cells[x][y-1].blocking_movement:
neighbors.append(cells[x][y-1])
if y < prefs.GRID_SIZE - 1 and not cells[x][y+1].blocking_movement:
neighbors.append(cells[x][y+1])
return neighbors
#oddaje tablice punktow jako sciezke agenta
def convert_to_coordinates(self, shortest_path):
coordinates = [(cell.X, cell.Y) for cell in shortest_path]
return coordinates
#Wyjmuje pierwsze koordynaty do ruszenia agenta a potem usuwa go z listy
def pop_first_coordinates(self, coordinates):
if coordinates:
x, y = coordinates.pop(0)
return x, y
else:
print("Lista współrzędnych jest pusta.")
return None, None
#Funkcja pomocnicza dla watku bo chcemy zeby agent poruszal sie ale zeby to normalnie wygladalo
def sciezkaAgenta(self, agent, path):
x,y = self.pop_first_coordinates(path)
if x is not None and y is not None:
agent.moveto(x,y)