Hi all.
I'm writing a battleships type game. Each player is represented as a struct, which contains 2 10 x 10 arrays, player's name, etc.
There are 2 players, but there are 4 instances of player, so that I can swap players, called p1, p2, current & opponent.Code:struct player { char shipBoard[10][10]; char bombBoard[10][10]; string name; int shipRem; };
I want to be able to save the game, so I've created another struct, which contains 4 instances of player, called saveGame.
Code:struct saveGame { player savep1; player savep2; player saveCurr; player saveOpp; };
here's the save function:
I have a restore function which is similar, only in reverse.Code:void save() { saveGame save; save.savep1 = p1; save.savep2 = p2; save.saveCurr = current; save.saveOpp = opponent; ofstream outputStream("saveBattle.dat", ios::out | ios::binary); if(!outputStream) { cout << "Failed to open file"; exit(1); } outputStream.write((char *) &save, sizeof(saveGame)); outputStream.close(); cout << "Game has been saved"; exit(0); }
So I have a struct that contains structs.
I then write that struct to a file, and read it to restore the game, using a similar process.
What I want to know is do I have to specifically use a loop process, to put the data back into the 10 x 10 matrices, for each player? Ive tried just using the call
ans so on for each player, and then continuing the game, but it doesn't seem to work.Code:p1 = restore.savep1;
Sorry if what I have written isn't clear. It's quite hard to explain!
Thanks in advance,
Justin



LinkBack URL
About LinkBacks


