Multidimensional Array of Nodes

I am trying to make a 7x7x3 map for a game, representing a house 7 units wide by 7 units deep by 3 units tall (1st floor, 2nd floor, basement). The route I have chosen to take for this problem is an array of nodes. I considered a linked list but the number of pointers required would be outrageous.

Anyway, when I use a one-dimensional array, I get no errors. For example:

1
2
3
4
5
6
struct house{
	string room;
	string info;
};

house *h1 = new house[7];

This seems to be okay. When I add a second or third dimension to the array, like this:

house *h1 = new house[7][7][3];

I get an error on the 'new' keyword saying

A value of type "house(*)[7][3]" cannot be used to initialize an entity of type "house*"

Can anybody tell me what I'm doing wrong here and how to correct this mistake?

Any help would be greatly appreciated. Thanks!
Array's come with constant pointers, why are we doing this "dynamically"? I would try it straight first:
 
house h1[7][7][3];

And that's it! You're done!

EDIT: You may have your reasons for doing this the way you are and if so please post them. I just can't see it.
Last edited on
I am trying to make an old-school adventure game, much like the original Zork. I need to have each cell act as a location in the playable world, complete with a description, usable items, things to examine, etc.

That is why I need nodes in the cells as opposed to ints or strings.

All I'm doing now is trying to make the template for the world (or house in this case) so that I can add information to the cells later.

Will this method be appropriate for what I'm trying to do?
Last edited on
I would make house into a class. This will allow you to work with the data members more implicitly. The class should then be loaded into a vector so that you don't have to think about size. Or you could set the size with global static variables, this is so that you don't have to go through ALL of your code everytime you want to change the size of the world.
1
2
3
4
5
6
/*Global*/
int X = 7;
int Y = 7;
int Z = 3;
/* code code code */
house h1[X][Y][Z];

But you're on the right track and most of the stuff I mentioned is NOT necessary, it only makes life easier.

EDIT: I missed the "not" that is why it is now in caps.
Last edited on
I won't need to change the size of the world. All I need to do for the sake of this project is create a small playable area (in this case, the aforementioned house). Since I know the dimensions I need to work with, I opted for an array. Given that the dimensions won't change, would an array work sufficiently?

Also, thanks for your advice on making house a class rather than a struct. That should let me work with its members with more ease. When I'm working with creating and modifying rooms, would it be easiest to make each room a class and inherit from house? Or is there an easier way to go about that?
Yup an array would work for this. Vectors are just more scalible. I want to reassert my suggestion for basing the size of the array on static global variables, it would help with bounds checking, determining relative location and a few other things as well.

Your second question is a style preference IMO. It would make it easier to read if each room inherited from house, but it's really up to you and your preference.
Well, thank you for all of your help and advice. It has been greatly appreciated. I'm gonna take a stab at what I can do tonight and hope that I don't run into too many more roadblocks. You have certainly set me on the right track.
Topic archived. No new replies allowed.