2021-04-13 21:27:51 +02:00
|
|
|
from queue import Queue
|
2021-04-26 20:24:52 +02:00
|
|
|
from Engine.Point import Point
|
|
|
|
from Engine.State import State
|
2021-04-26 21:51:08 +02:00
|
|
|
from Constants import RIGHT, LEFT, DOWN, UP, DEFUSE, COLS, ROWS
|
|
|
|
from Engine.Node import Node
|
2021-04-26 20:24:52 +02:00
|
|
|
class BfsPathFinder:
|
2021-04-13 21:27:51 +02:00
|
|
|
|
|
|
|
def __init__(self, board):
|
|
|
|
self.board = board
|
|
|
|
self.goal = None
|
2021-04-26 20:24:52 +02:00
|
|
|
def findBomb(self,startState):
|
2021-04-26 21:51:08 +02:00
|
|
|
x = Node(startState)
|
2021-04-13 21:27:51 +02:00
|
|
|
|
|
|
|
frontier = Queue()
|
2021-04-26 21:51:08 +02:00
|
|
|
frontier.put(x)
|
2021-04-13 21:27:51 +02:00
|
|
|
cameFrom = dict()
|
2021-04-26 21:51:08 +02:00
|
|
|
cameFrom[x] = None
|
2021-04-13 21:27:51 +02:00
|
|
|
|
|
|
|
while not frontier.empty():
|
|
|
|
current = frontier.get()
|
|
|
|
|
|
|
|
if self.checkGoal(current):
|
2021-04-26 21:51:08 +02:00
|
|
|
return self.constructActions(cameFrom,startState)
|
2021-04-13 21:27:51 +02:00
|
|
|
|
|
|
|
for next in self.getNeighbour(current):
|
|
|
|
if next not in cameFrom:
|
|
|
|
frontier.put(next)
|
|
|
|
cameFrom[next] = current
|
2021-04-26 21:51:08 +02:00
|
|
|
next.action = next.state.getDirection()
|
|
|
|
|
2021-04-13 21:27:51 +02:00
|
|
|
return []
|
|
|
|
|
2021-04-26 21:51:08 +02:00
|
|
|
def constructActions(self,cameFrom,startState):
|
2021-04-13 21:27:51 +02:00
|
|
|
current = cameFrom[self.goal]
|
|
|
|
path = []
|
2021-04-26 21:51:08 +02:00
|
|
|
path.append(self.goal.getAction)
|
|
|
|
while current.state != startState:
|
|
|
|
path.append(current.getAction)
|
2021-04-13 21:27:51 +02:00
|
|
|
current = cameFrom[current]
|
|
|
|
path.reverse()
|
|
|
|
return path
|
|
|
|
|
2021-04-26 20:24:52 +02:00
|
|
|
def getNeighbour(self, current):
|
|
|
|
neighbourlist = []
|
2021-04-26 21:51:08 +02:00
|
|
|
node1 = Node(State(RIGHT,Point(current.state.getPoint().getX() + 1, current.state.getPoint().getY())))
|
|
|
|
node2 = Node(State(UP,Point(current.state.getPoint().getX(), current.state.getPoint().getY() + 1)))
|
|
|
|
node3 = Node(State(DOWN,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)
|
2021-04-26 20:24:52 +02:00
|
|
|
return neighbourlist
|
|
|
|
|
2021-04-26 21:51:08 +02:00
|
|
|
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
|
|
|
|
|
2021-04-13 21:27:51 +02:00
|
|
|
def checkGoal(self, current):
|
2021-04-26 21:51:08 +02:00
|
|
|
if current.state.getPoint() in self.board.bombMap:
|
2021-04-13 21:27:51 +02:00
|
|
|
self.goal = current
|
2021-04-26 21:51:08 +02:00
|
|
|
self.goal.action = DEFUSE
|
2021-04-13 21:27:51 +02:00
|
|
|
return True
|
|
|
|
return False
|
2021-04-26 20:24:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-04-26 21:51:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|