0

I am writing a program (a bot) to play a Risk-like game in an AI competition. I'm new to programming so I've used some very basic coding so far. In this game, each round the program receive some information from the game engine. In the program, I have a class BotState, which allows me to treat information from the current round, such as the opponent bot moves, or the regions currently under my control, etc. This information is put in some ArrayLists. I have some getters to access this information and use them in the main class.

My problem is that each round, the information is overwritten (each round means a new run of the program), so I can only access the information from the current round. What I would like to do is save all of the information each round, so that for example if the game state is at round 10, I still can access the moves that the opponent made on round 8.

I looked for ways to solve this problem, and I came across something named "object serialization". I didn't quite understood how it works, so I would like to know if there is a simpler/better way to do what I want, or if serialization is the way to go. Thanks for your help.

edit: I can't link the program to my disk or a database. I upload the source files of the bot to the game server, so everything has to be in the source files

125ch209
  • 35
  • 1
  • 6

2 Answers2

1

Object serialization should be fairly simple for your case.
Simply put it is a way to store your object on disk and
to later on take data from the disk and recreate your object
in memory in the same state it was before serializing it.

Another way is to define some sort of representation yourself
e.g. as an XML chunk and for each object and to store those
chunks in an XML file. You can view this as a custom serialization
but it's still a serialization.

Another way is to store your objects into a database.

All in all, you need some permanent/persistent storage
for your objects (whether it's the disk directly or a DB
/which is again using the disk at the lowest level/).

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • The problem is that i can't use my disk or a database. I upload the source files of my bot into the website for the AI competition and it goes to their server. Once i upload the bot, there is no links to my computer, so everything has to be in the source files (can i put a database in the source files?) – 125ch209 Apr 12 '14 at 21:08
  • I don't think you can. Generally, if your code executes on another server A you need to have permissions to use its disk if you want to serialize your objects on that server A. Of course you can still solve it by calling from A some web service (which runs on another server B) and which can use its own disk i.e. the B's disk. But this maybe would sound too big for you, I think. – peter.petrov Apr 12 '14 at 21:11
  • the information i want to store can be put as arrays of integers, so if i add a .txt file to the sources files, maybe i can write the info on this .txt file? I would just clear the .txt file at the beginning of each game and write the infos for each rounds. Do you think that could work? – 125ch209 Apr 12 '14 at 21:21
  • I don't know but I think you're missing the point. You should probably check with whoever is maintaining the game server if and how you can write to files on its disk i.e. how you can maintain state between different runs of your program. – peter.petrov Apr 12 '14 at 21:34
1

Consider using a modeling framework for your application. The Eclipse Modeling Framework (EMF) comes with a simple XMI serialization built into it. If your model is small and/or simple enough it may be worth it. Have a look at this EMF introduction tutorial and this tutorial on serialization in EMF.

Also, have a look at this question: What's the easiest way to persist java objects?.

Community
  • 1
  • 1
s.d
  • 4,017
  • 5
  • 35
  • 65