diff --git a/app.py b/app.py index b02984f..5f0b000 100644 --- a/app.py +++ b/app.py @@ -53,85 +53,24 @@ def draw_grid(window, cells, agent): window.blit(multiplierText, (0, 50)) initBoard() - -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 +agent = Agent(prefs.SPAWN_POINT[0], prefs.SPAWN_POINT[1], cells) #Wpisujemy miejsce w ktorym znajduje sie agent i miejsce docelowe do funkcji znajdujacej najkrotsza sciezke start_cell = cells[5][5] 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: print("Sciezka: ", [(cell.X, cell.Y) for cell in shortest_path]) else: print("Brak sciezki") - -#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) - +path = agent.convert_to_coordinates(shortest_path) #osobny watek dla sciezki agenta zeby co *iles czasu* poruszal sie tam gdzie mowi path def watekDlaSciezkiAgenta(): while True: - sciezkaAgenta() + agent.sciezkaAgenta(agent,path) time.sleep(1) watek = threading.Thread(target=watekDlaSciezkiAgenta) diff --git a/classes/agent.py b/classes/agent.py index 65d1c7a..bd644f5 100644 --- a/classes/agent.py +++ b/classes/agent.py @@ -73,3 +73,57 @@ class Agent: else: 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) \ No newline at end of file