0

I am currently storing game data such as a sequence of moves in a multi-dimensional array, that is defined as:

private final int[][] gameMoves = {{1},{2},{0},{3},{1,4},{1,4,5},{2,4}} 

The array is of course much bigger.

I feel that there should be a more elegant way to do this, possibly storing the data separately from the code using either an XML file or the SQLite database and then retrieving it in a while-loop.

If using an XML file, what would be the format and what would be the method of retrieval?

If using an SQLite database, what would be the way to get the data inside the database before executing the source code?

Any examples would be much appreciated.

Chris
  • 44,602
  • 16
  • 137
  • 156
  • Are the moves only going to be stored for that game session? Or do you want to write them to the disk to read the next time they open the game? – Danny Mar 07 '12 at 19:15
  • @Danny looks like this is for read-only game data - never needs to be stored at runtime, only retrieved. – tcovo Mar 07 '12 at 19:23
  • yes @tcovo. This is fixed data that doesn't get changed during game. It just needs to be retrieved once into a list or array using a while loop. This stays the same from game to game and is not user generated. – newyorkgena Mar 07 '12 at 19:35
  • You might find something useful in the answers to [this question](http://stackoverflow.com/questions/3196222/store-static-data-in-android-custom-resource). – tcovo Mar 07 '12 at 20:39

2 Answers2

0

If this is data that never gets changed, there isn't really any reason to persist it, especially if it gets read from frequently, since you'll be taking a performance hit in the reading thereof. If anything, perhaps a separate package containing game data objects each providing 'gameMoves', might be a winner.

Or, if you're tied to the separation from a conceptual and/or requirement standpoint, JSON might be a good way to go, as it's more directly tied to multidimensional arrays than a XML file might be, and certainly easier to work with for that than a DB.

for example, your JSON file might be:

[1, 2, 3, [45, 6, 7], 8, [9, 10]]

You could then use the JSON library to parse it with minimal fuss:

JSONArray data = new JSONArray(textFromFile);

for(i = 0; i < data.length; ++i) {
    //Read data
}

See http://developer.android.com/reference/org/json/JSONArray.html

JRaymond
  • 11,625
  • 5
  • 37
  • 40
  • This wont actually be a list of game moves. It will be a list of elements that will be displayed on the screen. The array holds the order in which the elements come onto the screen. Each element will hold the type of element (monster, spikes, floating platforms, etc) and their positions on the screen. This is an action side-scroller game. what I had in mind was a 2D array of objects and I would create/poplate this array or list of objects in the beginning of the game (only once) by iterating through the values in an XML file. I dont know what xml file to use or how to format and access it. – newyorkgena Mar 07 '12 at 22:45
  • @newyorkgena Yep. Hence the JSON idea, since it will be much easier to change if you decide to have an array of objects instead of ints later. Updating answer above to describe. – JRaymond Mar 08 '12 at 17:16
0

If those numbers represent moves you should use constants.

const int MOVE_WAIT= 0;
const int MOVE_LEFT= 1;
const int MOVE_RIGHT = 2;
const int MOVE_UP = 3;
const int MOVE_DOWN = 4;
const int[] SHOOORYUKEN = {MOVE_RIGHT, MOVE_DOWN, MOVE_RIGHT};

int[][] gameMoves = {{MOVE_LEFT},{MOVE_RIGHT},{MOVE_WAIT},{MOVE_UP},SHOOORYUKEN} 

etc...

Dr.Denis McCracleJizz
  • 850
  • 2
  • 11
  • 26