Multidimensional Arrays for a Rogue-Like Map?

Started by
8 comments, last by Zael 12 years, 2 months ago
I am working on a rogue-like RPG and have come to the point where I want to display a map. My first idea was to use a multidimensional array of characters, but I don't know how I would display them and Google hasn't been helping. I am wondering if there might not be an easier method.
Advertisement
Do you mean a 2D or a 3D array?
I am thinking of using a 2d array in the style of...

char mapBuild[4][4] = {{###$},{####},{##@$},(##$#}};

I don't know what kind of code I would to print that array onto the screen.
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
Try looking up Ncurses if you want a little more control over the look and feel of the console window.
An array of characters? Seriously? Your map tiles are going to be far more complex than one character: in a typical roguelike game they have various properties and flags and lists of things in that space, and when you start caching pathfinding results and querying nearest neighbours and connected components your data structures are probably going to be a bit more rich than a 2D array. Even as a derived intermediate representation of screen output, one character per tile isn't enough to represent colours and bold,italic and other attributes.

Omae Wa Mou Shindeiru


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.
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

An array of characters? Seriously?

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 [font=courier new,courier,monospace]char [/font]will quickly go away.
Strongly discouraged.

How to display them?
You don't.
You display something else instead based on the data obtained for each new room.

Previously "Krohm"


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.

This topic is closed to new replies.

Advertisement