from queue import Queue from Engine.Point import Point from Engine.State import State from Constants import RIGHT, LEFT, DOWN, UP, DEFUSE, REST class BfsPathFinder: def __init__(self, board): self.board = board self.goal = None def findBomb(self,startState): frontier = Queue() frontier.put(startState) cameFrom = dict() cameFrom[startState] = None while not frontier.empty(): current = frontier.get() if self.checkGoal(current): return self.constructPath(cameFrom,startState) for next in self.getNeighbour(current): if next not in cameFrom: frontier.put(next) cameFrom[next] = current return [] def constructPath(self,cameFrom,startState): current = cameFrom[self.goal] path = [] path.append(self.goal) while current.getAction != startState: path.append(current) current = cameFrom[current] path.append(startState) path.reverse() return path def getNeighbour(self, current): neighbourlist = [] state1 = State(LEFT,Point(current.getX() + 1, current.getY())) state2 = State(RIGHT,Point(current.getX(), current.getY() + 1)) state3 = State(UP,Point(current.getX(), current.getY() - 1)) state4 = State(DOWN,Point(current.getX() - 1, current.getY())) if self.checkField(current.getPoint(),state1.getPoint()): neighbourlist.append(state1.getPoint()) if self.checkField(current.getPoint(), state2.getPoint()): neighbourlist.append(state2.getPoint()) if self.checkField(current.getPoint(),state3.getPoint()): neighbourlist.append(state3) if self.checkField(current.getPoint(),state4.getPoint()): neighbourlist.append(state4) return neighbourlist def checkGoal(self, current): if current.getPoint() in self.board.bombMap: self.goal = current return True return False