0

I'm looking for the easiest way to save and load gamedata for a mastermind game. At the moment the things that need to be saved are, the number of colours being played with, the number of pegs being played with, how many guesses there have been and the actual answer. At the moment I'm saving all this information to a text file. I am having difficulty loading this information back in to the game, and have come across something called pickling. I've done some reading on it but I don't fully understand how it works and how it's different from the way I'm trying to do.

Thanks!

JaAnTr
  • 896
  • 3
  • 16
  • 28
  • But from what I see, Mastermind is a turn-based board game. What kind of difficulty would you have in using a text file in a save/load mechanism? – Truerror Nov 17 '13 at 19:20
  • @Truerror Unfortunately I'm just not great at programming! I'm sure I'll figure it out but I was wondering if there was a better way. – JaAnTr Nov 17 '13 at 19:20
  • Mastermind isn't really turn-based, since it is a single-player game. As the OP states, all you really need to store are the game config (the available colors and number of pegs in the solution), the solution, and all the guesses the player has made so far. From there you can reconstruct the full Mastermind board. – PaulMcG Nov 17 '13 at 19:28
  • @PaulMcGuire Yes, you're (arguably) right. I just read the Wikipedia article on it. But if so, then a text file would suffice, given the nature of the game. – Truerror Nov 17 '13 at 19:54

2 Answers2

1

Pickling is a method of serialization - persisting data on disk.

You can handle manual pickle/unpickling if you want. But you asked for the easiest way - python has that. Just use shelve:

import shelve

d = shelve.open('my_mastermind_shelf')

That's.. it. Now just treat d as you would treat any other dict; shelve handles all the pickling behind the scenes. The only caveat: remember to call its .close() method when you're done with it.

roippi
  • 25,533
  • 4
  • 48
  • 73
  • Is the shelve module only in Python 3 or higher? Because this needs to run on Python 2.7. – JaAnTr Nov 17 '13 at 19:16
  • @JaAnTr As far as I know, `shelve` was in python 1.5. It's certainly in all versions of python 2. I linked to the python 2 docs, so... – roippi Nov 17 '13 at 19:21
0

As I said in my comment, you can use a simple text file to save the current state of your game. You can use open() to open a (new) file. Usually it's like this:

fname = raw_input("Put in the filename: ")

f = open(fname, 'w')    # This opens the text file for write operations.
                        # If the file already exists, it will truncate it.
                        # If not, then it will creaate a new file.

f.write("Hey, I'm a hippie coder, ho.") # Or, whatever string you want.
                                        # You don't have to be a hippie coder.

f.close()

Oh, and don't mind my poor example. It's 4 in the morning here, and I'm supposed to be in bed.

Truerror
  • 146
  • 4