Projekt_AI-Automatyczny_saper/Engine/BfsPathFinder.py

132 lines
5.5 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-27 21:10:01 +02:00
from Constants import RIGHT, LEFT, DOWN, UP, DEFUSE, COLS, ROWS, MOVE
2021-04-26 21:51:08 +02:00
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
2021-05-18 00:21:14 +02:00
def __init__(self, board, dTree):
2021-04-13 21:27:51 +02:00
self.board = board
self.goal = None
2021-05-18 00:21:14 +02:00
self.dTree = dTree
2021-04-26 20:24:52 +02:00
def findBomb(self,startState):
2021-04-27 21:10:01 +02:00
x = Node(startState, None)
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-27 21:10:01 +02:00
return self.constructActions(current,startState)
2021-04-13 21:27:51 +02:00
for next in self.getNeighbour(current):
2021-04-27 21:10:01 +02:00
if next not in cameFrom:
2021-04-13 21:27:51 +02:00
frontier.put(next)
cameFrom[next] = current
2021-04-27 21:10:01 +02:00
next.parent = current
2021-04-13 21:27:51 +02:00
2021-04-26 23:37:54 +02:00
return []
2021-04-26 23:24:59 +02:00
2021-04-27 21:10:01 +02:00
def constructActions(self,current,startState):
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-27 21:10:01 +02:00
current = current.parent
2021-04-26 23:24:59 +02:00
path.append(current.getAction())
2021-04-13 21:27:51 +02:00
path.reverse()
return path
2021-04-26 23:37:54 +02:00
# 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
2021-04-13 21:27:51 +02:00
2021-04-27 21:10:01 +02:00
# 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
2021-04-26 20:24:52 +02:00
def getNeighbour(self, current):
neighbourlist = []
2021-04-27 21:10:01 +02:00
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))
2021-04-26 20:24:52 +02:00
return neighbourlist
2021-04-27 21:10:01 +02:00
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:
2021-04-26 21:51:08 +02:00
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-27 21:10:01 +02:00
self.goal = Node(current,DEFUSE)
2021-04-13 21:27:51 +02:00
return True
return False
2021-04-26 20:24:52 +02:00
2021-04-27 21:10:01 +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
2021-04-26 20:24:52 +02:00
2021-04-26 21:51:08 +02:00