Hasen
Hasen

Reputation: 12314

Solitiare card game - how to program resume game function?

I've been programming a solitaire card game and all has been going well so far, the basic engine works fine and I even programmed features like auto move on click and auto complete when won, unlimited undo/redo etc. But now I've realised the game cannot be fully resumed ie saved so as to continue from the exact position last time the game was open.

I'm wondering how an experienced programmer would approach this since it doesn't seem so simple like with other games where just saving various numbers, like the level number etc is sufficient for resuming the game.

The way it is now, all game objects are created on a new game, the cards, the slots for foundations, tableaus etc and then the cards are shuffled and dealt out. This is random but the way I see it, the game needs to remember this random deal to resume game and deal it again exactly the same when the game is resumed. Then all moves that were executed have to be executed as they were as well. So it looks like the game was as it was last time it was played, but in fact all moves have been executed from beginning again. Not sure if this is the best way to do it but am interested in other ways if there are any.

I'm wondering if any experienced programmers could tell me how they would approach this and perhaps give some tips/advice etc.

Upvotes: 1

Views: 155

Answers (2)

WraitheDX
WraitheDX

Reputation: 58

(I am going to assume this is standard, Klondike Solitaire)

I would recommend designing a save structure. Each card should have a suit and a value variable, so I would write out:

[DECK_UNTURNED]
H 1
H 10
S 7
C 2
...
[DECK_UNTURNED_END]

[DECK_TURNED]
...
[DECK_TURNED_END]

etc

I would do that for each location cards can be stacked (I believe you called them foundations), the unrevealed deck cards, the revealed deck cards, each of the seven main slots, and the four winning slots. Make sure however you read them in and out, they end up in the same order, of course.

When you go to read the file, a simple way is to read the entire file into a vector of strings. Then you iterate through the vector until you find one of your blocks.

if( vector[ iter ] == "[DECK_UNTURNED]" )

Now you go into another loop, using the same vector and iter, and keep reading in those cards until you reach the associated end block.

while( vector[ iter ] != "[DECK_UNTURNED_END]" )
    read cards...
    ++iter

This is how I generally do all my save files. Create [DATA] blocks, and read in until you reach the end block. It is not very elaborate, but it works.

Upvotes: 1

lhf
lhf

Reputation: 72312

Your idea of replaying the game up to a point is good. Just save the undo info and redo it at load time.

Upvotes: 0

Related Questions