Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Zeraan

Which is better? Array of classes or linked list?

This topic is 5893 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

In my game, I''m debating whether to use array of classes or linked lists for the asteroids. In the game, they get shot, and breaks apart into smaller asteroids (aka classic asteroids). Which is the best way to do this? The reason why I''m asking this is because I don''t know pointers and lists real well, so I don''t know if they will be useful in this game. Arrays, on the other hand, I know how to use, but its hard to deal with. If you say linked list, can you point me to a tutorial on it, or tell me how to use it?

Share this post


Link to post
Share on other sites
Advertisement
I''m not sure if this is the best idea, but you can use a combination of both.

Since the objects could break down into smaller pieces, you could try using an array of linked list. Each asteroid could be a linked list of classes. You could make the length of each linked list be 3, or 4, depending on how many times the asteroid can be broken down. Now each asteroid has its own space to be broken down into.

I''d explain it a bit more, but I have to run out for a half hour.

Share this post


Link to post
Share on other sites
Well if you dont know pointers and list very well then you might want to use an array, but on the other hand you could do the linked list way to teach yourself how to use them. But personally the way I would do it is something like: CAstroid Astroids[MAX_ASTROIDS]; with MAX_ASTROIDS being at like 50 or something, as I cant see having anymore than that on screen at once and then you would have like:
class CAstroid
{
bool Active; // can you see it on screen yet?
int Type; // eg, big (ones u start with), medium, small...
int x, y; // position
...
etc.
}

CEO Platoon Studios

Share this post


Link to post
Share on other sites
That array of linked lists makes the most sense and I think it will work better because I shoot 1 big asteroid, it breaks into 3 medium asteroids, but still linked to the first big asteroid, then I shoot one of the medium asteroids, and it breaks in some small asteroids, but those are linked to the 1 medium that I shot, and it is linked back to the big asteroid, when I shoot all of the asteroids in that list, that list get deleted, right?

Share this post


Link to post
Share on other sites
the array of linked list idea is dumb, well, using it like that is dumb (its way to complex). youll be better to just use one list.

Share this post


Link to post
Share on other sites
You could just use a single linked list. When one breaks up you reduce the size of that asteroid and add N more of the same size to the end of the list.

When asteroid->size=0 (destroyed) take it out of the linked list.

There''s no reason to group asteroids using an Array of linked lists.

Ben


IcarusIndie.com [ The Rabbit Hole | The Labyrinth | DevZone | Gang Wars | The Wall | Hosting | Dot Com ]

Share this post


Link to post
Share on other sites
I just did this not too long ago. What I did was make an array of CAsteroids[MAX] The thing is you don't want too create and destroy a bunch of asteroid objects so create them all at the beginning and just include an isactive variable (bool) in the class definition then you can set asteroids active and in active, init them to different sizes,velocities etc... and then set them to inactive when they get destroyed. When you need a to create some medium asteroids just search the array for !asteroid{i}.isactive(); and use that one. A linked list is way overkill for this.

EDIT: can't use square brackets for array index i.

[edited by - nonnus29 on July 27, 2002 9:29:52 PM]

Share this post


Link to post
Share on other sites
Use Vectors:
take a look at an example program using the vector class:

#include <iostream>
#include <vector>
int main()
{
vector example; //Vector to store integers
example.push_back(3); //Add 3 onto the vector
example.push_back(10); //Add 10 to the end
example.push_back(33); //Add 33 to the end
for(int x=0; x cout< if(!example.empty()) //Checks if empty
example.clear(); //Clears vector
vector another_vector; //Creates another vector to store integers
another_vector.push_back(10); //Adds to end of vector
example.push_back(10); //Same
if(example==another_vector) //To show testing equality
example.push_back(20);
example.switch(1, 2); //Switches elements 1 and 2
for(int y=0; y cout< return 0;
}
Hopefully you know have an idea that vectors are useful and somewhat easier to use than regular arrays. At the very least, they get around having to be resized constantly using new and delete. Furthermore, their immense flexibility - support for any datatype and support for automatic resizing when adding elements - and the other helpful included functions gives the clear advantages to arrays.


X4J

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!