Skip to content
Advertisement

Imported function changes when running changes imported variable

I am coding Chess and for past three days i’ve been stuck on this problem: When i run simulate_play_and_check function which is imported from funcs.py file it takes arguments and changes them in a main file and i dont know why. When i print board.board before running this function it prints:

[[<pieces.Rook object at 0x0000029D1028BC40>, <pieces.Knight object at 0x0000029D1212E830>, <pieces.Bishop object at 0x0000029D1212E590>, <pieces.Queen object 
at 0x0000029D1212E7D0>, <pieces.King object at 0x0000029D1212E890>, <pieces.Bishop object at 0x0000029D1212E920>, <pieces.Knight object at 0x0000029D1212E9E0>, <pieces.Rook object at 0x0000029D1212E980>], [<pieces.pawn object at 0x0000029D1212EA40>, <pieces.pawn object at 0x0000029D1212EA70>, <pieces.pawn object at 0x0000029D1212EAA0>, [], <pieces.pawn object at 0x0000029D1212EB00>, <pieces.pawn object at 0x0000029D1212EB30>, <pieces.pawn object at 0x0000029D1212EB60>, <pieces.pawn object at 0x0000029D1212EB90>], [[], [], [], [], [], [], [], []], [[], <pieces.Bishop object at 0x0000029D1212ECB0>, [], <pieces.pawn object at 0x0000029D1212EAD0>, [], [], [], []], [[], [], [], [], <pieces.pawn object at 0x0000029D1212EE00>, [], [], []], [[], [], [], [], [], [], [], []], [<pieces.pawn object at 0x0000029D1212ED40>, <pieces.pawn object at 0x0000029D1212ED70>, <pieces.pawn object at 0x0000029D1212EDA0>, <pieces.pawn object at 0x0000029D1212EDD0>, 
[], <pieces.pawn object at 0x0000029D1212EE30>, <pieces.pawn object at 0x0000029D1212EE60>, <pieces.pawn object at 0x0000029D1212EE90>], [<pieces.Rook object at 0x0000029D1212EBC0>, <pieces.Knight object at 0x0000029D1212EBF0>, <pieces.Bishop object at 0x0000029D1212EC20>, <pieces.Queen object at 0x0000029D1212EC50>, <pieces.King object at 0x0000029D1212EC80>, [], <pieces.Knight object at 0x0000029D1212ECE0>, <pieces.Rook object at 0x0000029D1212ED10>]]

After running the function it prints:

[[[], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], []]]

Here is sample code of this: main.py

  if simulate_play_and_check(board.board, mouse_x, mouse_y, selected_piec, player):
                            board.board[mouse_y][mouse_x] = []
                            board.board[selected_piec.y][selected_piec.x] = []
                            selected_piec.selected = False
                            selected_piec.x = mouse_x
                            selected_piec.y = mouse_y
                            selected_piec.moves += 1
                            board.board[mouse_y][mouse_y] = selected_piec
                            method = "picking"
                            if player == "white":
                                player = "black"
                            else:
                                player = "white"

funcs.py

def simulate_play_and_check(board, mouse_x, mouse_y, selected, player):
    temp = board
    if temp[mouse_y][mouse_x] == []:
        temp[mouse_y][mouse_x] = selected
        temp[selected.y][selected.x] = []
        temp[mouse_y][mouse_x].selected = False
        temp[mouse_y][mouse_x].x = mouse_x
        temp[mouse_y][mouse_x].y = mouse_y
        for y in range(8):
            for x in range(8):
                temp[y][x] = []         
    else:
        temp[mouse_y][mouse_x] = selected
        temp[selected.y][selected.x] = []
        temp[mouse_y][mouse_x].selected = False
        temp[mouse_y][mouse_x].x = mouse_x
        temp[mouse_y][mouse_x].y = mouse_y
    return Check(temp,player)

I am quite new in coding, but i want to know board.board changes after running that function.

Advertisement

Answer

Problem is in that when i assign board to a temp it is still on that same place in memory. So when i change something in that temp variable it changes the spot in the memory where is the variable board. Doing copy like that is called shallow copy if i am not mistaken. And you need to create something called deep copy if you are copying the list you need to create new empty list then iterate through the board variable and copy it to the new list like this:

temp = [[[] for i in range(8)] for i in range(8)]
for i in range(8):
    for j in range(8):
        temp[i][j] = board[i][j]
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement