0

Hey,
I try of saving my board to a text file and then trying to load my board from the text file but actually does not works.

If anyone knows how this can be solve let me know !

Thanks

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by G-Do
0

just write it to a text file so that the text file looks something like this:

sssssssss
sssssssss
rryrssyry
yyyryryyr
yryrryryr

where s is an unoccupied slot and r represents a red and y represents a yellow..

this way you can reconstruct the board giving each slot of the board a specific state or occupier.

It is quite easy to parse the string(s) using basic string manipulation.

0

Hi eleonora,

It's also easy to use the pickle module. Pickle dumps objects into a data file and reloads them without you having to worry about parsing text. So, if you have a variable named "board" and you want to dump it to a file called "board.dat" you can just do:

import pickle
board = dict()
f = open("board.dat", "w")
pickle.dump(board, f)
f.close()

And if you want to retrieve it, you can do:

import pickle
f = open("board.dat", "r")
board = pickle.load(f)
f.close()

Hope this helps!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.