Feb-22-2020, 01:29 AM
Hello,
I am currently working on a school project where we should solve an 8-Puzzle (3x3 grid with 8 tiles. You can always move one tile to the empty spot). I created 2-dimensional list with numbers 1-8 + 0. 0 Marks the empty spot.
Now I am using the following code to move one tile (I want to move the 6 to 0 (and vice versa).
I know that I need to create a copy() of the list, so it is not referenced. But I still get the output wrong. It changes both lists.
I tried to find a solution to the problem, but all suggestions are to make a copy, what I am doing right now.
Thank you for any help!
Best regards,
NoLuck
My Code:
I am currently working on a school project where we should solve an 8-Puzzle (3x3 grid with 8 tiles. You can always move one tile to the empty spot). I created 2-dimensional list with numbers 1-8 + 0. 0 Marks the empty spot.
Now I am using the following code to move one tile (I want to move the 6 to 0 (and vice versa).
I know that I need to create a copy() of the list, so it is not referenced. But I still get the output wrong. It changes both lists.
I tried to find a solution to the problem, but all suggestions are to make a copy, what I am doing right now.
Thank you for any help!
Best regards,
NoLuck
My Code:
exampleState = [[1, 2, 3], [4, 5, 6], [7, 8, 0]] movedState = exampleState.copy() newList = exampleState.copy() for i in range(len(newList)): for j in range(len(newList[i])): if newList[i][j] == 0: iPos = i jPos = j numMove = int(movedState[iPos-1][jPos]) movedState[iPos][jPos] = int(numMove) movedState[iPos-1][jPos] = 0 print("moved state:", movedState) print("input state:", exampleState)My Output:
Output:moved state: [[1, 2, 3], [4, 5, 0], [7, 8, 6]]
input state: [[1, 2, 3], [4, 5, 0], [7, 8, 6]]