Skip to main content
gamedev.net
🔒 Locked

Array as class member?

Started by BKBen7 6 replies 1.5k views
Original Post
BKBen7
BKBen7
What I'm trying to do is set up an array as room locations for a text adventure thinger I'm making, anyway heres my room class so far, could you guys let me know if what I'm trying do is correct? And maybe nudge me in the right direction when you see I'm way off?

class cRoom{
private:
    string m_RoomName;
    string m_RoomDesc;
    int m_RoomLoc[1000][1000];
public:

    string GetTitle();

    void SetTitle(string RoomName);

};

"Is life so dear, or peace so sweet, as to be purchased at the price of chains and slavery? Forbid it, Almighty God! I know not what course others may take; but as for me, give me liberty or give me death!"~Patrick Henry
Fruny
Fruny
The size of the array will cause major trouble if you try to instanciate the class on the stack.

Use a vector<int> instead, and manually convert from 2D to 1D coordinates:

[y][x] <-> [y*xsize+x]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
BKBen7
BKBen7
Could you explain a little more in depth what you mean in that last statement, also how would I then set a room to a certain location in the vector?

(I'm still a bit n00bish with this vector/array/class stuff)
"Is life so dear, or peace so sweet, as to be purchased at the price of chains and slavery? Forbid it, Almighty God! I know not what course others may take; but as for me, give me liberty or give me death!"~Patrick Henry
achi-gaki
achi-gaki
for one to try and help you, it would be good if you explained what you are trying to achieve. as for the class format, its good. the declaration of the 2-dimension array is alright.

hope you have a class "string" because some compilers usually bring some errors on this declaration. otherwise all the best.
BKBen7
BKBen7
What I want to set up is some kind of array type thing with m_RoomLoc so that when you are playing the text adventure and enter a certain room it checks the surrounding sections in that array for different exits from that room, ie.


0010
0101
0031


Now if the player was in '3' then he could move northwest, northeast and east.
"Is life so dear, or peace so sweet, as to be purchased at the price of chains and slavery? Forbid it, Almighty God! I know not what course others may take; but as for me, give me liberty or give me death!"~Patrick Henry
Fruny
Fruny
Instead of using a 2D indexing notation, which requires either the use of C arrays, kludgy arrays of arrays (and their vector counterpart) or developing your own 2D array classes, you should just use the equivalent 1D indexing notation, which allows you to directly use a vector member.

Just keep track somewhere of how wide your array is supposed to be, and instead of using a 2D array and accessing element n = array[y][x], you can compute the corresponding index and use a 1D array n = array[y*width+x].
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
RawRecruit
RawRecruit
I don't think the problem here is how to implement the array, but rather where to implement it. Looking at the 'cRoom' class I take it you want the class to be implemented for each room, in which case the location array shouldn't be in there as it will be created every time you instantiate a room. Instead, from an OO point of view, I think you'd be better creating a Room class that doesn't care about where it is, it just handles details like a description and maybe a list of some items, etc. You'd then have a Map class that imlpements a vector of Room classes and handles where the rooms are in that vector, etc.
Rasmadrak
Rasmadrak
Search google for "single linked list" or "double linked list" and you will find information that will make you cry with happiness. ;)

Anyway, it's really unnecessary (sp?) to have each room know ALL rooms... simply connect it's neighbours using linked lists! :)

or if you really want to hardcode the neighbours, you could either store the map ONCE, and make each tile remember it's position, then simply check its neighbours on the "global" map, or make each tile store only the max amount of connections around it... i.e 8 rooms in 2D, and 26 for 3d or something..

Cheers!
"Game Maker For Life, probably never professional thou." =)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.