def paintPointsBlack(originalBoard, point0, point1, point2):
board = originalBoard[:]
# do something
return board
def fillTheBoard(board, point):
movePointToWhitePoint(board, point)
if point['row'] == height - 1:
print('last line. terminate')
return
elif point['row'] != height - 1:
LCanFit = canPutLShape(board, point)
if LCanFit['total'] != 0:
point0 = dict(point)
if LCanFit['RdD'] == 1:
...
newBoard = paintPointsBlack(board[:], point, point1, point2)
fillTheBoard(newBoard, point0)
if LCanFit['DL'] == 1:
...
newBoard = paintPointsBlack(board[:], point, point1, point2)
fillTheBoard(newBoard, point0)
if LCanFit['RD'] == 1:
...
newBoard = paintPointsBlack(board[:], point, point1, point2)
fillTheBoard(newBoard, point0)
if LCanFit['DR'] == 1:
...
newBoard = paintPointsBlack(board[:], point, point1, point2)
fillTheBoard(newBoard, point0)
print("inspected everything around : ", point['row'], point['col'])
else:
return
fillTheBoard
is a function that calls itself if certain condition(LCanFit
) matches. There are many if
s, and for this to work properly, the initially passed argument board
should not change at the same depth of if
. The only function that could have changed board
was paintPointsBlack
but to avoid this, I passed the copy (board[:]
) and in an extra cautious but meaningless attempt, I made it to copy the argument inside the paintPointsBlack
.
The problem I have is that the board
is still changed after one branch of recursion finishes and moves to the next branch(next if
). So the board
inside if LCanFit['RdD'] == 1
and board
inside if LCanFit['DL'] == 1
(the same depth) is not the same because board
is changed to newBoard
of the last called function(deepest as it can go with the previous if
).
I know there is no way that pass by value can change the original but I still see it changes board
and it's so confusing. If I better rewrite the code, how should I structure it?