Multidimensional Arrays for a Rogue-Like Map?
#1 Members - Reputation: 103
Posted 24 January 2012 - 04:19 PM
#4 Members - Reputation: 570
Posted 24 January 2012 - 06:03 PM
#6 Members - Reputation: 1235
Posted 25 January 2012 - 03:17 AM
#7 Members - Reputation: 154
Posted 25 January 2012 - 10:16 AM
First of all, if you use 2D arrays youre going to have to set your size in advance (when you compile) which might be limiting. Its better to dynamically allocate a single dimensional array of size w*h and then access it with x+y*w. As to printing it, cant you just use two nested for loops? Im not sure how youd clear the screen though
That's not true for most languages. You can't change the size of an array when running, but when you can create it, it can be any variable size.
int width, height; std::cin >> width; std::cin >> height; char mapbuilder[width][height];
Granted the OP doesn't mention what language he is using, but it looks like a C based language.
OP: Two for-loops (one nested in the other) does seem the best way to print the array. There is no standard way to clear the screen. You would have to either use a library, or investigate how to clear the screen on each platform you plan to play it on.
#8 Members - Reputation: 570
Posted 25 January 2012 - 03:11 PM
#9 GDNet+ - Reputation: 1710
Posted 26 January 2012 - 01:49 AM
Agreed. I was asked by a friend to work on one of his designs. It was very simple at start so I tried using something like a grid-based approach. It did work (sort of) but there was support to provide arbitrary descriptions for each room. 1 char per room is just a no-go. BTW, it is basically binary encoding, the benefits of using char will quickly go away.An array of characters? Seriously?
Strongly discouraged.
How to display them?
You don't.
You display something else instead based on the data obtained for each new room.
#10 Members - Reputation: 154
Posted 26 January 2012 - 01:36 PM
Im fairly certain that only works in C99, not C++, and it also means you cant access the array outside of the function its declared in, which would be fairly limiting. Its also technically possible to dynamically allocate a 2D (or 3D, etc) array, but it involves one allocation for each row, and generally is sort of messy
It is perfectly valid C++, but you are right about the limitation in passing the data to functions. I will yield that a single dimensional array with bookkeeping is probably the better way to go. Perhaps done in a class that abstracts away the bookkeeping. Either that or a pointer of pointers, which can be passed around.
If the OP happens to be doing this in Java though, he can easily pass multidimensional arrays around.






