I have a 'GameBoard' class and am doing a search on it. I want to save the current gameboard in a list, change the state of the gameboard, save THAT one in a list, and so on (so I would have incremental versions of the gameboard as a game progresses).
I'm currently doing this with copy.deepcopy, but it doesn't seem to be saving and moving on. Here is a sample of my code:
moves = [] # just for reference. this has at least one value in it by the time the while loop hits.
while winner is False and len(moves) > 0:
g_string = gb.to_string()
if g_string == win_string:
winner = True
break
possibilities = gb.legal_moves(visited) # returns a list of possible moves to take
if len(possibilities) > 0:
moves.append(copy.deepcopy(gb))
gb.perform(possibilities[0]) # this modifies gb, according to the gameplay move.
# snipped, for brevity.
If after 100 iterations, I were to print moves
, I would get 100 identical objects. If I print the object each time before appending it, they are definitely different at the time of appending.
To clarify, I would like copies of these objects (for use in things like a graph, to perform DFS and BFS on)
Here is more of my GameBoard class
class GameBoard:
number_of_rows = 3
rows = []
global random_puzzle
def __init__(self, setup):
#some code. fills in the rows. adds some things to that list. etcetc..
# more code