From c21203fb72a64f4d5e3bbfd15a44b0fdf3d980f3 Mon Sep 17 00:00:00 2001 From: Cezary Adamczak Date: Mon, 21 Jun 2021 14:14:44 +0200 Subject: [PATCH] Zaktualizuj 'node.py' --- node.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/node.py b/node.py index 3c1ee8b..0747927 100644 --- a/node.py +++ b/node.py @@ -137,10 +137,12 @@ class Node: closedList.append(currentNode) if currentNode.field[currentNode.position[0]][currentNode.position[1]].planted and \ + currentNode.field[currentNode.position[0]][currentNode.position[1]].field_type == "soil" and \ currentNode.field[currentNode.position[0]][currentNode.position[1]].hydration < 2: path = [] for _ in range(currentNode.field[currentNode.position[0]][currentNode.position[1]].hydration, 4): path.append("hydrate") + path.append("fertilize") current = currentNode while current is not None: path.append(current.action) @@ -174,3 +176,57 @@ class Node: continue heapq.heappush(openList, child) + + def findPathToPlantSpot(self, goals): + startNode = Node(self.field, self.position, self.rotation) + + openList = [] + closedList = [] + + startNode.parent = None + + heapq.heappush(openList, startNode) + + while len(openList) > 0: + currentNode = heapq.heappop(openList) + + closedList.append(currentNode) + + if not currentNode.field[currentNode.position[0]][currentNode.position[1]].planted and \ + goals[currentNode.position[0]][currentNode.position[1]] != "": + path = [] + path.append("plant") + current = currentNode + while current is not None: + path.append(current.action) + current = current.parent + return path[::-1] + + children = succesor(currentNode) + + perm = 0 + for child in children: + for closedChild in closedList: + if child.position == closedChild.position and child.rotation == closedChild.rotation and child.action == closedChild.action: + perm = 1 + break + if perm == 1: + perm = 0 + continue + child.parent = currentNode + child.startCost = currentNode.startCost + child.field[child.position[0]][child.position[1]].moveCost + child.heuristic = abs(startNode.position[0] - child.position[0]) + abs( + startNode.position[1] - child.position[1]) + child.totalCost = child.startCost + child.heuristic + + for openNode in openList: + if child.position == openNode.position and child.rotation == openNode.rotation and child.action == openNode.action and child.startCost >= openNode.startCost: + perm = 1 + break + + if perm == 1: + perm = 0 + continue + + heapq.heappush(openList, child) +