feat: Finding path using BFS function

This commit is contained in:
MlodyJ 2024-04-12 13:44:55 +02:00
parent ebb834cc81
commit 361d08fe91
2 changed files with 98 additions and 0 deletions

61
app.py
View File

@ -5,6 +5,7 @@ from classes.coffeMachine import CoffeMachine
from pygame.locals import K_w, K_s, K_a, K_d
from classes.cell import Cell
from classes.agent import Agent
from collections import deque
pygame.init()
window = pygame.display.set_mode((prefs.WIDTH, prefs.HEIGHT))
pygame.display.set_caption("Game Window")
@ -44,7 +45,67 @@ def draw_grid(window, cells, agent):
window.blit(scoreText, (0, 0))
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
#Wpisujemy miejsce w ktorym znajduje sie agent i miejsce docelowe
start_cell = cells[5][5]
target_cell = cells[10][10]
shortest_path = bfs(start_cell, target_cell, cells)
if shortest_path:
print("Sciezka: ", [(cell.X, cell.Y) for cell in shortest_path])
else:
print("Brak sciezki")
agent = Agent(prefs.SPAWN_POINT[0], prefs.SPAWN_POINT[1], cells)
running = True
while running:

View File

@ -1,4 +1,5 @@
import pygame
from collections import deque
import prefs
class Agent:
def __init__(self, x, y, cells, baseScore=0):
@ -12,6 +13,8 @@ class Agent:
self.cells = cells
self.score = baseScore
self.multiplier = 1
self.x = x
self.y = y
def move_up(self):
if pygame.time.get_ticks()-self.last_move_time > 125 and self.current_cell.Y > 0 and not self.cells[self.current_cell.X][self.current_cell.Y-1].blocking_movement:
@ -62,3 +65,37 @@ class Agent:
return
if self.multiplier < 1:
self.multiplier = 1
def shortest_path(grid, start, end):
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
rows, cols = len(grid), len(grid[0])
# Funkcja pomocnicza sprawdzająca, czy dany punkt znajduje się na siatce
def is_valid(x, y):
return 0 <= x < rows and 0 <= y < cols and grid[x][y] == 0
# Inicjalizujemy kolejkę BFS i odwiedzone punkty
queue = deque([(start, 0)]) # (punkt, odległość)
visited = set()
# Rozpoczynamy BFS
while queue:
(x, y), distance = queue.popleft()
# Sprawdzamy, czy osiągnęliśmy punkt docelowy
if (x, y) == end:
return distance
# Sprawdzamy wszystkie możliwe kierunki ruchu
for dx, dy in directions:
nx, ny = x + dx, y + dy
# Sprawdzamy czy nowa pozycja jest ważna i nieodwiedzona
if is_valid(nx, ny) and (nx, ny) not in visited:
visited.add((nx, ny))
queue.append(((nx, ny), distance + 1))
# Jeśli nie udało się osiągnąć punktu docelowego, zwracamy -1
return -1