AI-Project/survival/pathfinding.py

59 lines
1.4 KiB
Python
Raw Normal View History

2021-04-18 21:15:11 +02:00
from collections import deque as Queue
def valid_neighbor(n, game_map, visited):
if n[0] < 0 or n[1] < 0 or n[0] >= game_map.width or n[1] >= game_map.height:
return False
if visited[n[0]][n[1]]:
return False
if game_map.is_colliding(n):
return False
return True
def breadth_first_search(game_map, start, target):
2021-04-19 01:41:02 +02:00
visited = [[False for _ in range(game_map.height)] for _ in range(game_map.width)]
2021-04-18 21:15:11 +02:00
q = Queue()
came_from = dict()
2021-04-19 01:41:02 +02:00
start = tuple(start)
target = tuple(target)
came_from[start] = None
2021-04-18 21:15:11 +02:00
q.append(start)
visited[start[0]][start[1]] = True
while len(q) > 0:
cell = q.popleft()
if cell == target:
break
neighbors = [
(cell[0] - 1, cell[1]),
(cell[0], cell[1] + 1),
(cell[0] + 1, cell[1]),
(cell[0], cell[1] - 1),
]
for neighbor in neighbors:
if valid_neighbor(neighbor, game_map, visited):
q.append(neighbor)
visited[neighbor[0]][neighbor[1]] = True
came_from[neighbor] = cell
path = list()
current = target
while current != start:
path.append(current)
2021-04-19 01:41:02 +02:00
if current not in came_from:
path.clear()
return path
2021-04-18 21:15:11 +02:00
current = came_from[current]
2021-04-19 01:41:02 +02:00
path.reverse()
2021-04-18 21:15:11 +02:00
return path