Reputation: 11
I am very new in Python, and I am doing an 8-puzzle problem (My first Program). I have a problem with appending list to another list. Below is part of my code.
def solvePuzzle(puzzle,path,visited):
moveState = []
if(init == goal):
path.append(puzzle)
return True;
puzzleT = deepcopy(puzzle)
visited.append(copy(puzzle))
index = findBlankTile(puzzleT)
moveState = moveTileChoice(puzzleT,visited,index)
#move process
for move in moveState:
path.append(copy(puzzle))
if(applyMove(move,puzzle,path,visited,index)):
return True;
else:
path.remove()
return False;
In the apply move function, when I swap the blank tile with other numbers tile in the puzzle (code below), all of the values in the Visited and Path lists are changed as well, and that's not what I wanted. What did I do wrong with my code?
def applyMove(move,puzzle,path,visited,index):
row = index[0]
col = index[1]
if move == "Left":
puzzle[row][col] , puzzle[row][col-1] = puzzle[row][col-1] , puzzle[row][col]
return solvePuzzle(puzzle,path,visited)
elif move == "Right":
puzzle[row][col] , puzzle[row][col+1] = puzzle[row][col+1] , puzzle[row][col]
return solvePuzzle(puzzle,path,visited)
elif move == "Up":
puzzle[row][col] , puzzle[row-1][col] = puzzle[row-1][col] , puzzle[row][col]
return solvePuzzle(puzzle,path,visited)
elif move == "Down":
puzzle[row][col] , puzzle[row+1][col] = puzzle[row+1][col] , puzzle[row][col]
return solvePuzzle(puzzle,path,visited)
else:
return False;
Upvotes: 1
Views: 84