Projekt_AI-Automatyczny_saper/Engine/PathFinder.py

60 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
2021-04-27 21:34:05 +02:00
while self.openList:
2021-04-27 21:10:01 +02:00
current = self.minKey(self.fScore, self.openList)
if self.checkGoal(current):
return self.constructActions(current, startState)
2021-04-27 21:34:05 +02:00
self.openList.remove(current)
2021-04-27 21:10:01 +02:00
for next in self.getNeighbour(current):
2021-04-27 21:34:05 +02:00
tentativeGScore = self.gScore.get(current) + current.state.getPoint().distance(next.state.getPoint())
2021-04-27 21:10:01 +02:00
if tentativeGScore < self.gScore.get(next, 10000):
2021-04-27 21:34:05 +02:00
self.cameFrom[next] = current
2021-04-27 21:10:01 +02:00
next.parent = current
self.gScore[next] = tentativeGScore
2021-04-27 21:45:58 +02:00
self.fScore[next] = tentativeGScore + next.state.getPoint().distance(startState.getPoint())
2021-04-27 21:34:05 +02:00
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