1

So I'm creating a basic text-based RPG styled game. It utilises Rock-Paper-Scissors match-ups and level-based progression.

Because this game is so grindy to get anywhere, and to aid in my black box testing I would like to create a save & load system. So I could save and quit the program and before it quit it would give me a code of all important details. And then when I returned to the program I could re-enter this code and return to where I was at.

In my mind I imagine it to be similar to the system used in Lego games when creating characters, where there is a code accompanied with the character so that you can save the code and re-enter it to get the same character appearance back. However I'm relatively new to programming and am not sure if there's a better way to do this or how I would go about doing this.

Here's my attempt at saving the code:

if TrainTimes == 5483:
    print("Sorry to see you leave!")
    print("Here's your code to sign-in later:")
    code= name,power,rivalname,level,AP,limit,ExpGain,TotalExp,attack,defense,speed,evasiveness,health
    print(code)
    break

And here's an example after I played through and got to this point:

('Oliver', 'dark', 'Jim', 8, 3, 25, 14, 0, 5, 1, 1, 5, 10)

I'm not sure how I would then load this though, I can imagine I should use something like:

while SaveLoaded == False:
(FUNCTION)

for the code that would normally play without a save-file, and that I'd need to change SaveLoaded to True if I loaded a save. But how would I get the program to identify and load in the code?

Thanks in advance!

Obahar
  • 107
  • 1
  • 1
  • 10
  • So, to summarize and see if I understand. You want to prompt a user if they want to save. If they save, you save data somewhere. When they come back, you ask them for a code. They enter the code. If they enter the right code, it loads the previous data they had? – idjaw Feb 29 '16 at 12:28
  • Yes, that is what I'm hoping to achieve with this :) – Obahar Feb 29 '16 at 12:30
  • Is this the code they have to enter: `('Oliver', 'dark', 'Jim', 8, 3, 25, 14, 0, 5, 1, 1, 5, 10)` – idjaw Feb 29 '16 at 12:31
  • Yes, that's the code that my program outputs, so I hoped it would work just as well when it was input – Obahar Feb 29 '16 at 12:33
  • That was my thought as well, but I wasn't sure how I would be able to convert the data into this code. EDIT: This is what I had in mind when I started: https://i.ytimg.com/vi/Rp2pDrnB2kA/maxresdefault.jpg But I'm not sure how to go about converting it – Obahar Feb 29 '16 at 12:35
  • I'm not too sure about the conversion. But if you use a dictionary, then if you provide a code like: "XYZ". Then you can map like this: `{"XYZ": ('Oliver', 'dark', 'Jim', 8, 3, 25, 14, 0, 5, 1, 1, 5, 10)`. Does this make sense? – idjaw Feb 29 '16 at 12:39
  • Ah, I see. I can see how that would work; however I'm not sure on the actually doing it. I'm relatively new to coding, so my knowledge on some of these thing's existence is practically null. Do you know a document or article I could read on this? – Obahar Feb 29 '16 at 12:44
  • I'll put together a simple example in an answer of an idea I think would help you. – idjaw Feb 29 '16 at 12:45

3 Answers3

3

Considering you are looking to reclaim the state of your application, this sounds like a good job for pickle (or, similarly, dill).

So, what this will do is dump your objects to a file, then when you reload this file, you will load these properties back in to memory.

Now, with respect to what you are looking to do and based on the comment discussion. You can provide the user with some simple unique code like:

"ABCDEFG"

which will point to your objects by setting up a dictionary. Now, another modification you can make to make it even easier to access your data is to name each of your values. I don't know what each of your properties mean, but here is a sample of what you can do:

save_state = {
    "ABCDEFG": {
        'name': 'Oliver',
        'color': 'dark',
        'last_name': 'Jim',
        'val_1': 8,
        'val_2': 3,
        'val_3': 25,
        'val_4': 14,
        'val_5': 0,
        'val_6': 5,
        'val_7': 1,
        'val_8': 1,
        'val_9': 5,
        'val_10': 10
    }
}

Take that data, and using pickle, dump this to your pickle file like this:

import pickle

pickle.dump(save_state, open('dude.txt', 'wb'))

When you are ready to load the data back, you simply use pickle.load. Like this:

loaded_data = pickle.load(open('dude.txt', 'rb'))

Printing this data out:

{'ABCDEFG': {'last_name': 'Jim', 'val_2': 3, 'val_3': 25, 'name': 'Oliver', 'val_5': 0, 'val_10': 10, 'val_8': 1, 'val_7': 1, 'val_9': 5, 'val_1': 8, 'val_6': 5, 'color': 'dark', 'val_4': 14}}
idjaw
  • 25,487
  • 7
  • 64
  • 83
  • Oh thank you, this should work well with what I'm trying to do! I decided against including what each variable stood for as I didn't think it mattered. And it shouldn't with this- I'll be sure to try this out and see how it works out! Thanks! – Obahar Feb 29 '16 at 13:03
  • Best of luck. Try this out. play around with it. See how it works out for you. :) – idjaw Feb 29 '16 at 13:04
1

You can use the json module to first serialize the objects into a json string. Then, you can convert it into hexadecimal using the binascii module.

Encoding code:

import json
import binascii

name = 'abc'
power = 5.0
attack = [1, 2, 3]

jsonified = json.dumps((name, power, attack))
secret_number = binascii.hexlify(jsonified.encode('UTF-8'))
print(secret_number)

Decoding code:

import json
import binascii

secret_number = input()
jsonified = binascii.unhexlify(secret_number).decode('UTF-8')
name, power, attack = json.loads(jsonified)

print(name)
print(power)
print(attack)

Demo:

Encoding
Decoding

Anmol Singh Jaggi
  • 8,376
  • 4
  • 36
  • 77
0
def Load(save):
   if SaveLoaded == True:  
      saveSplit = save.split(',')
      if(len(saveSplit) == 13)
         cleaned = []
         string = ""
         for i in range(0,3):
            string = split[i].strip("'( )")
            cleaned.append(string)

        for i in range(3,13):
           string = split[i].strip("'( )")
           cleaned.append(int(string))

         name,power,rivalname,level,AP,limit,ExpGain,TotalExp,attack,defense,speed,evasiveness,health = saveSplit
      else:
         setDefault()
   else:
      SetDefault()

Basically you split string to parts with delimiter beeing comma. The you clean unnecesary characters (parentheses etc), convert it to crrect Data Type and assign to your variables, if you don't want to load, or your string is invalid (with invalid being too short) you set your values to your defaults.

Ashrak
  • 1
  • 2