0

I'm trying to complete the finishing touches on an assignment that uses python by adding a save/load function (that has to use files unfortunately). I have four function defined and given a shot at writing the code for each, but I've come up with nothing as it doesn't want to work and our instructors instructions for files are extremely basic.

def savegame():
    f=open("savefile.txt","w")
    f.write(playeroneright/playeroneleft/playertworight/playertwoleft/playertworight/computerright/computerleft)
    f.close()

def opengame():
    f=open("savefile.txt")
    lines=f.readlines()
    playeroneright=lines[0]
    playeroneleft=lines[1]
    playertworight=lines[2]
    playertwoleft=lines[3]
    computerright=lines[4]
    computerright=lines[5]
    f.close()

def newgame():
    deletecontent("savefile.txt")
    f=open("savefile.txt","w")
    f.write(1)
    f.write(1)
    f.write(1)
    f.write(1)
    f.write(1)
    f.write(1)
    f.close()
    opengame()

def deletecontent(pfile):
    fn=pfile.name 
    pfile.close()

The first function (savegame) should take each variable and save the numerical value of it on a different line as it currently holds in the game (the game manipulates the variables, playeroneright and so one). It should save it in the file savefile.txt.

The second function (opengame) should take whatever is currently saved in the file and assign that file to the variable, basically a load function.

The third function (newgame) should reset the game to the starting point (where all variables take on the value 1), then open the game up using the above function.

The last function (deletecontent) is just used in the third function to clear the document so that it can assign all new values.

These are my attempts thus far, and I'm not very good at files yet but I am trying to figure them out a bit more. If anyone has any ideas (preferably simple, if that's possible), that would be awesome. Thank you!

Eric
  • 13
  • 5
  • It looks like you're pretty close to meeting the requirements. What are you missing? Also, why does newgame() call opengame()? Seems like you should have a main function that calls newgame() if needed, then calls opengame. – Caleb Mauer Oct 15 '15 at 00:48
  • Yes that could work, I just used opengame in newgame because in newgame I reset the values to 1 and in opengame it assigns the variables, so every time I used newgame I am also going to have to use opengame too. – Eric Oct 15 '15 at 01:52

1 Answers1

2

Just to get you going (not doing other folks homework here ;))

def savegame(playerone_right, playerone_left, ....):
  f=open("savefile.txt","w")
  f.write(playerone_right + "\n")
  f.write(playerone_left + "\n")
  ...
  f.close()

"\n" results in a linebreak (new line) + on two strings concatenate them.

In general you would choose other ways pickle, json, ..., but it looks like the required format is given in your example.

To really use the functions you would need to save the variables somewhere and give them to the function as argument requiring them, or use global variables.

To use global variables, in your case add:

  global playeroneright, playeroneleft, playertworight, playertwoleft, playertworight, computerright, computerleft

to the begin of each function using this variables. Now you do not need to give them to the functions as arguments anymore, as every function can just work with the global variables.

This isn't the nicest way to solve this problem, but without introducing more pattern to you, this should be the easiest way to go for you.

SleepProgger
  • 364
  • 6
  • 20
  • Thank you, that was really helpful. I've gotten most of it fixed, but when I assign the variables in opengame():, it only assigns to two of them, the rest are left blank (I just printed the variables and only two of them showed us as being assigned the value 1), and I can't figure out why only some would work while the rest I guess are ignored. – Eric Oct 15 '15 at 01:47