-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNodeBuilder.py
43 lines (40 loc) · 1.27 KB
/
NodeBuilder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- coding: UTF-8 -*-
from Node import Node
class NodeBuilder:
"""
Build child nodes for 15-puzzle
"""
def getChildNodes(self, node):
"""
Return list of valid child nodes
"""
children = []
iSpace, jSpace = node.getCoordByValue(0)
for i in range(-1, 2):
for j in range(-1, 2):
if i * j != 0 or i == j:
continue
iSwap, jSwap = iSpace + i, jSpace + j
if not (0 <= iSwap <= 3) or not (0 <= jSwap <= 3):
continue
position = node.getPosition()
position[iSpace][jSpace] = position[iSwap][jSwap]
position[iSwap][jSwap] = 0
moves = node.getMoves()
moves.append(self._getMoveNameFromDelta(i, j))
child = Node(
position,
moves,
node.getHeuristic()
)
children.append(child)
return children
def _getMoveNameFromDelta(self, iDelta, jDelta):
if iDelta == -1:
return 'up'
if iDelta == 1:
return 'down'
if jDelta == -1:
return 'left'
if jDelta == 1:
return 'right'