from queue import Queue from Engine.Point import Point from Engine.State import State from Constants import RIGHT, LEFT, DOWN, UP, DEFUSE, COLS, ROWS, MOVE from Engine.Node import Node class BfsPathFinder: def __init__(self, board): self.board = board self.goal = None def findBomb(self,startState): x = Node(startState, None) frontier = Queue() frontier.put(x) cameFrom = dict() cameFrom[x] = None while not frontier.empty(): current = frontier.get() if self.checkGoal(current): return self.constructActions(current,startState) for next in self.getNeighbour(current): if next not in cameFrom: frontier.put(next) cameFrom[next] = current next.parent = current return [] def constructActions(self,current,startState): path = [] path.append(self.goal.getAction()) path.append(current.getAction()) while current.state != startState: current = current.parent path.append(current.getAction()) 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 getNeighbour(self, current): neighbourlist = [] if current.state.getDirection() == RIGHT: neighbourlist.append(Node(State(DOWN, Point(current.state.getPoint().getX(), current.state.getPoint().getY())), RIGHT)) neighbourlist.append(Node(State(UP, Point(current.state.getPoint().getX(), current.state.getPoint().getY())), LEFT)) point = Point(current.state.getPoint().getX() + 1, current.state.getPoint().getY()) if self.checkField(point): neighbourlist.append(Node(State(RIGHT, point), MOVE)) elif current.state.getDirection() == LEFT: neighbourlist.append(Node(State(DOWN, Point(current.state.getPoint().getX(), current.state.getPoint().getY())), LEFT)) neighbourlist.append(Node(State(UP, Point(current.state.getPoint().getX(), current.state.getPoint().getY())), RIGHT)) point = Point(current.state.getPoint().getX() - 1, current.state.getPoint().getY()) if self.checkField(point): neighbourlist.append(Node(State(LEFT, point), MOVE)) elif current.state.getDirection() == UP: neighbourlist.append(Node(State(LEFT, Point(current.state.getPoint().getX(), current.state.getPoint().getY())), LEFT)) neighbourlist.append(Node(State(RIGHT, Point(current.state.getPoint().getX(), current.state.getPoint().getY())), RIGHT)) point = Point(current.state.getPoint().getX(), current.state.getPoint().getY() - 1) if self.checkField(point): neighbourlist.append(Node(State(UP, point), MOVE)) elif current.state.getDirection() == DOWN: neighbourlist.append(Node(State(LEFT, Point(current.state.getPoint().getX(), current.state.getPoint().getY())), RIGHT)) neighbourlist.append(Node(State(RIGHT, Point(current.state.getPoint().getX(), current.state.getPoint().getY())), LEFT)) point = Point(current.state.getPoint().getX(), current.state.getPoint().getY() + 1) if self.checkField(point): neighbourlist.append(Node(State(DOWN, point), MOVE)) return neighbourlist def checkField(self, point): if not (point.getX() < 0 or point.getX() > COLS - 1 or point.getY() < 0 or point.getY() > ROWS - 1) 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 = Node(current,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