Original Post
I just learned python and wanted to practice by implementing the A* algorithm and using it to solve the 8-puzzle. So I made a node class that looks like this: where data is a 1D representation of the 2D board (so that data[x+3*y] is the (x,y) square) where the emty square has the value 0. I also have a function called GenerateNeighbors(node) which takes a node as input and returns a list of neighboring nodes. Now, just to check that everything was working I made a function that shuffles a board using the GenerateNeighbors function: I know that it's not random at all but I did it just to see if it would work. So then I did: and got: [1, 2, 3, 4, 5, 6, 7, 8, 0] 10 [1, 2, 3, 4, 5, 0, 7, 8, 6] 9 [1, 2, 0, 4, 5, 3, 7, 8, 6] 8 [1, 2, 3, 4, 5, 0, 7, 8, 6] 7 [1, 2, 0, 4, 5, 3, 7, 8, 6] 6 [1, 2, 3, 4, 5, 0, 7, 8, 6] 5 [1, 2, 0, 4, 5, 3, 7, 8, 6] 4 [1, 2, 3, 4, 5, 0, 7, 8, 6] 3 [1, 2, 0, 4, 5, 3, 7, 8, 6] 2 [1, 2, 3, 4, 5, 0, 7, 8, 6] 1 None So I know that the functions are working but why did I get None at the end? Thanks.
class Node:
def __init__(self, data):
self.data = data
parent = 0
f,g,h=0,0,0
def Shuffle(node, depth):
if depth==0:
return node.data, depth
print node.data , depth
Shuffle(GenerateNeighbors(node)[0],depth-1)
start = [1,2,3,4,5,6,7,8,0]
print Shuffle(Node(start),10)