Projekt_AI-Automatyczny_saper/Engine/PathFinder.py

59 lines
1.7 KiB
Python
Raw Normal View History

2021-04-12 22:54:33 +02:00
from Constants import ROWS, COLS
2021-04-27 21:10:01 +02:00
from Engine.BfsPathFinder import BfsPathFinder
from Engine.Node import Node
2021-04-12 22:54:33 +02:00
from Engine.Point import Point
2021-04-27 21:10:01 +02:00
from queue import PriorityQueue
2021-04-12 22:54:33 +02:00
2021-04-27 21:10:01 +02:00
class PathFinder(BfsPathFinder):
2021-04-12 22:54:33 +02:00
def __init__(self, board):
2021-04-27 21:10:01 +02:00
super().__init__(board)
2021-04-12 22:54:33 +02:00
self.board = board
self.openList = []
self.cameFrom = {}
self.gScore = {}
self.fScore = {}
2021-04-27 21:10:01 +02:00
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 not self.openList:
current = self.minKey(self.fScore, self.openList)
if self.checkGoal(current):
return self.constructActions(current, startState)
for next in self.getNeighbour(current):
tentativeGScore = self.gScore.get(self.current) + current.state.getPoint().distance(next.state.getPoint())
if tentativeGScore < self.gScore.get(next, 10000):
cameFrom[next] = current
next.parent = current
self.gScore[next] = tentativeGScore
self.fScore[next] = next.state.getPoint().distance(startState.getPoint())
if next not in self.openList:
self.openList.append(next)
2021-04-12 22:54:33 +02:00
return []
def minKey(self, fscore, openlist):
2021-04-27 21:10:01 +02:00
minkey = Node
2021-04-12 22:54:33 +02:00
minValue = float('inf')
2021-04-27 21:10:01 +02:00
for node in openlist:
value = fscore.get(node, 10000)
2021-04-12 22:54:33 +02:00
if value < minValue:
minValue = value
2021-04-27 21:10:01 +02:00
minkey = node
2021-04-12 22:54:33 +02:00
return minkey