Projekt_AI-Automatyczny_saper/Engine/BfsPathFinder.py
Kraton99 c1cdc1489d bfs
2021-04-13 21:27:51 +02:00

44 lines
1.2 KiB
Python

from queue import Queue
from Engine.PathFinder import PathFinder
class BfsPathFinder(PathFinder):
def __init__(self, board):
super().__init__(board)
self.board = board
self.goal = None
def findBomb(self,start):
frontier = Queue()
frontier.put(start)
cameFrom = dict()
cameFrom[start] = None
while not frontier.empty():
current = frontier.get()
if self.checkGoal(current):
return self.constructPath(cameFrom,start)
for next in self.getNeighbour(current):
if next not in cameFrom:
frontier.put(next)
cameFrom[next] = current
return []
def constructPath(self,cameFrom,start):
current = cameFrom[self.goal]
path = []
path.append(self.goal)
while current != start:
path.append(current)
current = cameFrom[current]
path.append(start)
path.reverse()
return path
def checkGoal(self, current):
if current in self.board.bombMap:
self.goal = current
return True
return False