How to save a game state in Java
SolvedKX Posted messages 19031 Status Modérateur -
Hello everyone,
I just coded a simplified version of the Rummy game in Java. I would like to add a save option to my game. I have stored the players' deck, the draw pile, and the discard pile in lists. I assume that to create a save feature, I need to keep the content of the lists in a file or something similar and then restart the game from these stored lists, but I don't know how to implement it.
Thanks to everyone who takes the time to help me.
1 réponse
Hello,
The Rami game played with regular cards (kings, queens, jacks...) can be represented by a Unicode character. Example: Playing cards
The simplest way would be to write and read from a file with one line per list, each line containing all the characters corresponding to the cards.
For example, the first line for the draw pile, another for the discard pile, and as many lines as there are players.
To read/write in files, there are the methods of the Files class:
https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/nio/file/Files.html