Jump to content

  • Log In with Google      Sign In   
  • Create Account


Multidimensional Arrays for a Rogue-Like Map?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 jniblick   Members   -  Reputation: 103

Like
0Likes
Like

Posted 24 January 2012 - 04:19 PM

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.

Sponsor:

#2 zacaj   Members   -  Reputation: 643

Like
0Likes
Like

Posted 24 January 2012 - 04:59 PM

Do you mean a 2D or a 3D array?

#3 jniblick   Members   -  Reputation: 103

Like
0Likes
Like

Posted 24 January 2012 - 05:34 PM

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.

#4 zacaj   Members   -  Reputation: 643

Like
1Likes
Like

Posted 24 January 2012 - 06:03 PM

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

#5 compscialien   Members   -  Reputation: 104

Like
0Likes
Like

Posted 24 January 2012 - 06:34 PM

Try looking up Ncurses if you want a little more control over the look and feel of the console window.

#6 LorenzoGatti   Crossbones+   -  Reputation: 2660

Like
1Likes
Like

Posted 25 January 2012 - 03:17 AM

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.
Produci, consuma, crepa

#7 Zael   Members   -  Reputation: 154

Like
0Likes
Like

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 zacaj   Members   -  Reputation: 643

Like
0Likes
Like

Posted 25 January 2012 - 03:11 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

#9 Krohm   Crossbones+   -  Reputation: 3044

Like
0Likes
Like

Posted 26 January 2012 - 01:49 AM

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

#10 Zael   Members   -  Reputation: 154

Like
0Likes
Like

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS