Projekt_AI-Automatyczny_saper/Engine/BfsPathFinder.py

113 lines
3.8 KiB
Python
Raw Normal View History

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 22:16:57 +02:00
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):
2021-04-26 22:16:57 +02:00
if self.notIn(next,cameFrom):
2021-04-13 21:27:51 +02:00
frontier.put(next)
cameFrom[next] = current
return []
2021-04-26 23:24:59 +02:00
# def constructActions(self,cameFrom,startState):
# current = cameFrom[self.goal]
# current.action = self.setAction(current)
# 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):
2021-04-13 21:27:51 +02:00
current = cameFrom[self.goal]
2021-04-26 23:24:59 +02:00
current.action = self.setAction(current,self.goal.state.getPoint())
2021-04-13 21:27:51 +02:00
path = []
2021-04-26 23:24:59 +02:00
path.append(self.goal.getAction())
path.append(current.getAction())
2021-04-26 21:51:08 +02:00
while current.state != startState:
2021-04-26 23:32:41 +02:00
point = current.state.getPoint()
current.action = self.setAction(current, point)
2021-04-26 23:24:59 +02:00
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())))
2021-04-26 22:16:57 +02:00
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)))
2021-04-26 21:51:08 +02:00
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 22:16:57 +02:00
def notIn(self, next, camefrom):
for x in camefrom:
if x.state.point.__eq__(next.state.point):
return False
return True
2021-04-26 23:24:59 +02:00
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
2021-04-26 20:24:52 +02:00
2021-04-26 21:51:08 +02:00