• Post Reply Bookmark Topic Watch Topic
  • New Topic

Saving a connect 4 game - specific question  RSS feed

 
Jack Higgs
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I have a connect 4 program working fine, the game plays and all is good. What I want to do is allow a player to save the game in its current state, and load it back up again. The best way of doing this in the context of my program is to create a new text file containing all relevent variables for each saved game so as to make parsing relatively simple.

Obviously this will mean that a user can't make two text files with the same name otherwise problems will occur! Therefore I need a method to look at the current directory (ie the one the java file is in) for text files with the same name as the one the user specified. If there is one with the same name then the method should return an error and the user should be forced to type in a new name etc etc. My UI is entirely text-based by the way!

Anyone got any ideas, algorithms, links as to how I could go about this?

Cheers bluds.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what exactly the question is - have you used the java.io package before? It has the File class that you need, and that has the exists() method that checks whether a file exists.

Are graphical elements totally out of the question? I'm asking because java.awt.FileDialog would be the tool to use to let the user specify the file and directory.

One idea: Instead of showing an error if the user selects a file name that already exists, give him the option to overwrite the existing file. Sometimes that's what the user actually wants.
 
Jack Higgs
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers, the exists method has cleared that one up for me! Sometimes when you're relatively new to Java you don't know these methods exist, and you make the problem appear much harder than it is!
 
Erik Silkensen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm relatively new to Java as well but something I've tried using before with some success is Serialization.

Let's say I have a Game object, that implements java.io.Serializable.

If the user wants to save the game, I could use an ObjectOutputStream with writeObject() to save it in a .ser file, and then upon request read it back in and restore it with ObjectInputStream and readObject().

I'm not sure if this is how Serialization is supposed to be used... but it seems like a nice solution instead of writing and parsing text files all the time ( that could maybe even be easy for a user to get into and change -- trying to cheat or something... )

http://java.sun.com/j2se/1.5.0/docs/api/java/io/Serializable.html
[ January 08, 2007: Message edited by: Erik Silkensen ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!