Temple of The Roguelike Forums
Development => Programming => Topic started by: Pueo on May 08, 2012, 05:46:41 AM
-
Aloha again,
Another question, how do I copy a chtype array into another chtype array? For example, you are at level 15 in the dungeon. You go down the stairs, and it takes chtype level16 and copies it to chtype currentLvl. Then it prints currentLvl.
I tried "currentLvl = level16", but that didn't work. Chtype arrays aren't assignable, I guess. I also considered the function "cpystr", but that's for char arrays, not chtype arrays. Is there a special nCurses function, or do you have to just do it one-by-one (copy level16[1][1], currentLvl[1][1] = level16[1][1], etc)?
-
Why not just write something like this??
void loadLevel(/*level number or pointers to arrays*/) {
for(int y = 0; y < NR_MAP_Y_CELLS; y++) {
for(int x = 0; x < NR_MAP_X_CELLS; x++) {
currentLvl[x][y] = newLevel[x][y];
}
}
}
But that seems so obvious that I probably missed the point of this question.
-
Why not just write something like this??
void loadLevel(/*level number or pointers to arrays*/) {
for(int y = 0; y < NR_MAP_Y_CELLS; y++) {
for(int x = 0; x < NR_MAP_X_CELLS; x++) {
currentLvl[x][y] = newLevel[x][y];
}
}
}
But that seems so obvious that I probably missed the point of this question.
I could use that (and that was the point of the question), but I didn't really want to use that. I wanted more
curLvl = newLevel;
or
cpychtype(curLvl, newLevel);
It's not that the nested for loops don't work, it's that I'd rather use more concise code (I might have to repeat this over 30 times, once for each level)
And: Wow! Code tags! Never knew we had those things.
-
Another solution is to use the memcpy function:
memcpy(newLevel, currentLvl, sizeof(currentLvl))
You can also put your map inside a struct, then you can copy structs with newLevel=oldLevel (but this is inconvenient).
It's not that the nested for loops don't work, it's that I'd rather use more concise code (I might have to repeat this over 30 times, once for each level)
Then create a function which does that (as NON did). This is actually a general rule of good software design. Don't repeat yourself. Create functions instead. If you find yourself repeating something (anywhere in your game), then probably you should use a function, a loop, or another programming construct for it.
Moreover, you probably should not have 30 arrays level1, level2, level3, ..., level15, level16, ... You should have a three-dimensional array instead, level[30][HEIGHT][WIDTH]. Then the ">" simply does { lev++; loadLevel(level[lev], currentLvl); }
-
Then create a function which does that (as NON did). This is actually a general rule of good software design. Don't repeat yourself. Create functions instead. If you find yourself repeating something (anywhere in your game), then probably you should use a function, a loop, or another programming construct for it.
Good advice, thanks :)
Moreover, you probably should not have 30 arrays level1, level2, level3, ..., level15, level16, ... You should have a three-dimensional array instead, level[30][HEIGHT][WIDTH]. Then the ">" simply does { lev++; loadLevel(level[lev], currentLvl); }
Wait, what? You can make 3D arrays?! That's very cool. Is this available in C?
-
Yes, you can make as many dimensions as you can...
You can create an array of anything, and a 3D array in C is simply an array of arrays of arrays.
-
Wow. That's great. Thanks, Z!