from queue import Queue from Engine.Point import Point from Engine.State import State from Constants import RIGHT, LEFT, DOWN, UP, DEFUSE, COLS, ROWS from Engine.Node import Node class BfsPathFinder: def __init__(self, board): self.board = board self.goal = None def findBomb(self,startState): x = Node(startState) frontier = Queue() frontier.put(x) cameFrom = dict() cameFrom[x] = None while not frontier.empty(): current = frontier.get() if self.checkGoal(current): return self.constructActions(cameFrom,startState) for next in self.getNeighbour(current): if self.notIn(next,cameFrom): frontier.put(next) cameFrom[next] = current next.action = next.state.getDirection() return [] def constructActions(self,cameFrom,startState): current = cameFrom[self.goal] current.action = self.setAction(current, self.goal.state.getPoint()) path = [] path.append(self.goal.getAction()) path.append(current.getAction()) while current.state != startState: path.append(current.getAction()) current = cameFrom[current] path.reverse() return path # def constructActions(self, cameFrom, startState): # current = cameFrom[self.goal] # current.action = self.setAction(current,self.goal.state.getPoint()) # path = [] # path.append(self.goal.getAction()) # path.append(current.getAction()) # while current.state != startState: # point = current.state.getPoint() # current.action = self.setAction(current, point) # path.append(current.getAction()) # current = cameFrom[current] # path.reverse() # return path def getNeighbour(self, current): neighbourlist = [] node1 = Node(State(RIGHT,Point(current.state.getPoint().getX() + 1, current.state.getPoint().getY()))) node2 = Node(State(DOWN,Point(current.state.getPoint().getX(), current.state.getPoint().getY() + 1))) node3 = Node(State(UP,Point(current.state.getPoint().getX(), current.state.getPoint().getY() - 1))) node4 = Node(State(LEFT,Point(current.state.getPoint().getX() - 1, current.state.getPoint().getY()))) if self.checkField(current.state.getPoint(), node1.state.getPoint()): neighbourlist.append(node1) if self.checkField(current.state.getPoint(), node2.state.getPoint()): neighbourlist.append(node2) if self.checkField(current.state.getPoint(), node3.state.getPoint()): neighbourlist.append(node3) if self.checkField(current.state.getPoint(), node4.state.getPoint()): neighbourlist.append(node4) return neighbourlist def checkField(self, current, point): if not (point.getX() < 0 or point.getX() > COLS - 1 or point.getY() < 0 or point.getY() > ROWS - 1 or point.__eq__( current)) and point not in self.board.stoneMap: return True return False def checkGoal(self, current): if current.state.getPoint() in self.board.bombMap: self.goal = current self.goal.action = DEFUSE return True return False def notIn(self, next, camefrom): for x in camefrom: if x.state.point.__eq__(next.state.point): return False return True def setAction(self, current, goal): if goal.getX() > current.state.getPoint().getX(): return RIGHT elif goal.getY() < current.state.getPoint().getY(): return UP elif goal.getX() < current.state.getPoint().getX(): return LEFT elif goal.getY() > current.state.getPoint().getY(): return DOWN