Search code examples
javafilevariablesconfigrobocode

Java Create Configuration-like File


I am using Robocode to develop GA tanks that evolve to give better performance (achieving highest fitness levels) and in order to do that I need to store information about each tank in one file.

The class Tank will have several variables such as:

  • Speed
  • Angular Momentum
  • Fire power
  • Scan Rate

I would like a way to be able to save these variables in a file called "settings.txt" and to be able to update them after a battle.

I would like the file to be formatted like:

robot(1):
speed=12
angMomentum=6
firePower=2
scanRate=10

robot(2):
speed=8
angMomentum=4
firePower=3
scanRate=13

What would be the best way to write a file like this and to be able to easily update the variable values after each battle?

Many Thanks.


Solution

  • If you want all your data in one big file. Maybe it's a good idea to use something like a JSON format because you can maintain your object structure. Use variables, objects and arrays.

    There are lots of JSON readers and writers available. Even some 'official' bij json.org to be found here

    Your JSON could look like this:

    {
        "tanks":[
            {"id": 1, "speed": 12, "angMomentum": 6, "firePower": 2, "scanRate": 10},
            {"id": 2, "speed": 8, "angMomentum": 4, "firePower": 3, "scanRate": 13}
        ]
    }