Jump to content
  • Advertisement
Sign in to follow this  
jniblick

Multidimensional Arrays for a Rogue-Like Map?

This topic is 2518 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!