Zaktualizuj 'node.py'

This commit is contained in:
Cezary Adamczak 2021-06-21 14:14:44 +02:00
parent 3fd1afdaeb
commit c21203fb72

56
node.py
View File

@ -137,10 +137,12 @@ class Node:
closedList.append(currentNode) closedList.append(currentNode)
if currentNode.field[currentNode.position[0]][currentNode.position[1]].planted and \ 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: currentNode.field[currentNode.position[0]][currentNode.position[1]].hydration < 2:
path = [] path = []
for _ in range(currentNode.field[currentNode.position[0]][currentNode.position[1]].hydration, 4): for _ in range(currentNode.field[currentNode.position[0]][currentNode.position[1]].hydration, 4):
path.append("hydrate") path.append("hydrate")
path.append("fertilize")
current = currentNode current = currentNode
while current is not None: while current is not None:
path.append(current.action) path.append(current.action)
@ -174,3 +176,57 @@ class Node:
continue continue
heapq.heappush(openList, child) 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)