Projekt_AI-Automatyczny_saper/Engine/PathFinder.py
2021-04-27 21:45:58 +02:00

60 lines
1.7 KiB
Python

from Constants import ROWS, COLS
from Engine.BfsPathFinder import BfsPathFinder
from Engine.Node import Node
from Engine.Point import Point
from queue import PriorityQueue
class PathFinder(BfsPathFinder):
def __init__(self, board):
super().__init__(board)
self.board = board
self.openList = []
self.cameFrom = {}
self.gScore = {}
self.fScore = {}
def findBomb(self, startState):
x = Node(startState, None)
self.openList.append(x)
self.gScore[x] = 0
self.fScore[x] = 0
cameFrom = dict()
cameFrom[x] = None
while self.openList:
current = self.minKey(self.fScore, self.openList)
if self.checkGoal(current):
return self.constructActions(current, startState)
self.openList.remove(current)
for next in self.getNeighbour(current):
tentativeGScore = self.gScore.get(current) + current.state.getPoint().distance(next.state.getPoint())
if tentativeGScore < self.gScore.get(next, 10000):
self.cameFrom[next] = current
next.parent = current
self.gScore[next] = tentativeGScore
self.fScore[next] = tentativeGScore + next.state.getPoint().distance(startState.getPoint())
if next not in self.openList:
self.openList.append(next)
return []
def minKey(self, fscore, openlist):
minkey = Node
minValue = float('inf')
for node in openlist:
value = fscore.get(node, 10000)
if value < minValue:
minValue = value
minkey = node
return minkey