tags:

views:

85

answers:

1

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
A: 

Your code is valid for a simple dictionary object:

seq = 1
a_dict = {}
moves = []
while seq < 4:
 a_dict['key' + str(seq)] = 'value' + str(seq)
 moves.append(copy.deepcopy(a_dict))
 seq = seq + 1

print moves

Somehow for your object, deepcopy does not reach the contents of your gameboard.

Is your gameboard data somehow stored outside of the object in which case only a pointer to it is copied? Which state of the board is saved, the first or the last one? Can you post more details about the structure of your object?


EDIT: Try adding your own getstate/setstate methods in order to tell deepcopy what data needs to copied between instances. For example if your rows array contains the gameboard:

def __getstate__(self): 
    return self.rows

def __setstate__(self, rows): 
    self.rows = rows 
littlegreen
The last one is saved, rather than the first one. It's not making a whole lot of sense to me.
amssage
Well it makes more sense than the first one being stored. Apparently when the game is over and you are reading out your moves[] array to print the game board history, data from the current object is read. Can you post more code about the object and how you read out the gameboard?
littlegreen
Sure, I posted the skeleton of the GameBoard class
amssage
Thanks. It would also help if you would include the method that gives back the gameboard data, the one that keeps giving you back the same board.
littlegreen
Nevermind, I believe I have found the problem.
littlegreen
Adding the above methods to the GameBoard class doesn't seem to change it. I am stepping out for the night, but I will check the link you linked me to in the morning. Thanks for your help
amssage
Hm. I created my own primitive GameBoard class to test it, and at first I had the same problem as you, but after adding those lines the problem was solved. Check it out for yourself, I posted the code online. You can remove and add the lines and see the difference. http://snipplr.com/view/41938/python-gameboard-class/
littlegreen
You just have to figure out which data is crucial for you to deepcopy and put that in one data structure. Anyhow.. goodnight! I hope the problem will be solved.
littlegreen