2

I have been writing a card game for Android, and I am desperately stuck on how to save my game state. To briefly explain my program, I have a Sprite class for displaying my cards which contains a bitmap image, a point for the position, and a bounding rectangle. I inherit this class to make a card class which has two bitmaps which are switched depending on whether the card is face up or down, and enumerations for storing suit and rank. These card objects are place in CardStack objects which contain and additional position, and an arraylist of cards. The game model consists of various CardStacks, and the rules for move cards between stacks. The game works great as is.

Now I am trying to figure out how to save my game state. I would like to serialize my entire game object and save it to a file. However, there are numerous objects (bitmap, point, rectangle, color, etc) that are not serializable. In C#, I know you can use a MemoryStream and just copy the whole object, but I can't find a similar method in Java that does not require everything to be serializable. Is there anything similar in Java? Any better way I could structure my game model object? Any advice would be greatly appreciated. Thanks!

2
  • 4
    You don't need to save things like bitmaps and rectangles. Saving the positions of stacks together with positions and your enums for the cards in them should be enough to restore the state and would also save memory. Commented Sep 1, 2011 at 20:44
  • Without knowing how the game is played I can't tell which info you'd have to save. But if the way I am imagining it is correct wouldn't you only need to store the values and suites of each card in all of the stacks in order to recreate the game state? The bitmaps, and colors can be figured out if you know the cards suite and rank. As for position(point and rectangle) Is it important to have these be exact? I.E. is the user allowed to move cards/stacks to any x/y location? or are there allowable "slots"? If the latter you need just to store which slot the card/stack should be in. Commented Sep 1, 2011 at 20:55

3 Answers 3

4

abstract the gui into business objects and make the gui objects encapsulate these business objects

this way a card doesn't need to know what bitmap it needs to be displayed (or even that it gets displayed at all)

those business objects are what you want to serialize and the gui can be rebuild from the deserialized objects

Sign up to request clarification or add additional context in comments.

Comments

1

You can always write your own customized serialization methods within Java's serialization framework. See Serializable.

Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:

private void writeObject(java.io.ObjectOutputStream out) throws IOException
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;
private void readObjectNoData()  throws ObjectStreamException;

You are correct that Java's default implementation will fail if your object contains non-serializable objects, but you can define how to serialize them in methods above.

Also: on Android, you may want to use Parcelable which is Android's version of serialization.

Comments

0

Take a look at protocol buffers (protobuf)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.