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.
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:
You could then use the JSON library to parse it with minimal fuss:
See http://developer.android.com/reference/org/json/JSONArray.html