Projekt_AI-Automatyczny_saper/Engine/PathFinder.py
2021-05-18 00:21:14 +02:00

67 lines
1.9 KiB
Python

from Constants import ROWS, COLS, MOVE
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, dTree):
super().__init__(board, dTree)
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) + self.weight(next)
if tentativeGScore < self.gScore.get(next, float('inf')):
self.cameFrom[next] = current
next.parent = current
self.gScore[next] = tentativeGScore
self.fScore[next] = tentativeGScore + self.heuristic(startState, next)
if next not in self.openList:
self.openList.append(next)
return []
def heuristic(self,startState,next):
return next.state.getPoint().distance(startState.getPoint())
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
def weight(self, next):
if next.state.getPoint() in self.board.mudMap and next.action == MOVE:
return 10
return 1