for university I was working on a game with several friends where we had to load levels from a .txt, save it in a data format of our choosing and obviously display it on the screen. We decided on a two-dimensional char array, which I -- as I was programming the level parser -- filled row by row. To make it more descriptive, an excerpt of the code I used:
unconverted = "#####\n" + "#___#\n" + "#S>X#\n" + "#___#\n" + "#####" // sample data
while(z < unconverted.length())
{
current = unconverted.charAt(z);
if(current == 'S' || current == 'X' || current == '<' || current == 'v' || current == '>' || current == '^' || current == '_' || current == '#' ||current == 't')
{
level[x][y] = current;
x++;
}
else if (current =='\n')
{
x=0;
y++;
}
else
{
System.out.println("Level null because of an unrecognised character");
level = null;
return level;
}
z++;
}
It worked fine so far, but we didn't really expect a problem to come up. The others didn't know much beyond that they would get a char[][], which seemed like enough information ... but it wasn't! The problem that appeared was that at various points in the game logic as well as in the GUI, the array was read either per row or per column, meaning that for example the display of the level was turned on it's head.
This obviously cost us quite a lot of time fixing the code to make it all consistent again. So for the future I would like some advice on what is generally accepted to be the "right" way, if there is any: filling and accessing a 2-dimensional array by row or by column? Furthermore, is there any performance difference whatsoever between those two methods? My common sense would say that filling the second dimension first
Thank you very much!