Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 109,372 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 951 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Collections Advice Please?

 
Reply to this topicStart new topic

Collections Advice Please?

itlee
post 16 Jul, 2008 - 07:41 AM
Post #1


New D.I.C Head

*
Joined: 16 Jul, 2008
Posts: 3


My Contributions


I am designing a sci-fi game and need to create a "map" of the galaxy (a 8x8 grid) that contains space objects (stars, planets, spaceships, etc) some of these objects contain no more than a name property, but some like the spaceships are more complex having methods as well as properties.

What I am confused about is that a collection holds a list of a-like objects, so it could hold a list of planets or stars but not both.

So, how can I create a Galaxy collection containing all the different types of space objects?

I am not looking for code just some advice on how to impelement.

Thanks,
Lee.
User is offlineProfile CardPM

Go to the top of the page


baavgai
post 16 Jul, 2008 - 08:17 AM
Post #2


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,576



Thanked 44 times

Dream Kudos: 325

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


I afraid I'm going to offer code, because it's just much easier they trying to explain. However, the basic idea is that all your elements have the same base class and you keep a list of the base class, unboxing into the more specific classes as needed.

Just off the top of my head, something like this should work.

csharp

// an element of your 8x8 grid
class GalaxyZone {
public int posX;
public int posY;
// things in the grid
// not the type is a base type,
// there can actually be no instances of it
private List<ZoneItem> zoneItems = new List<ZoneItem>();
public List<ZoneItem> ZoneItems { get { return this.zoneItems; } }
}

// basic thing that can be in zone
abstract class ZoneItem {
protected string itemName;
public ZoneItem(string itemName) { this.itemName = itemName; }
// all items with have this properties
public abstract string ItemName { get; }
// we can get this name for out type, but we main want to override it.
public virtual string ItemType { get { return this.GetType().Name; } }
}

// start building various types
// keep in mind that you only need a new class if it has
// properties that are unique to it.
class Star : ZoneItem {
private int size;
private float gravity;
public Star(string itemName, int size, float gravity) : base(itemName) {
this.size = size;
this.gravity = gravity;
}
public int Size { get { return this.size; } }
public float Gravity { get { return this.gravity; } }
}

class Planet : ZoneItem {
private Star star;
public Planet(string itemName, Star star) : base(itemName) {
this.star = star;
}
public Star Star { get { return this.star; } }
}

// example
GalaxyZone gz = new GalaxyZone();
Star sol = new Star("Sun", 1, 1);
gz.ZoneItems.Add(sol);
gz.ZoneItems.Add(new Planet("Earth", sol));
gz.ZoneItems.Add(new Planet("Mars", sol));


Hope this helps.
User is offlineProfile CardPM

Go to the top of the page

itlee
post 16 Jul, 2008 - 08:35 AM
Post #3


New D.I.C Head

*
Joined: 16 Jul, 2008
Posts: 3


My Contributions


baavgai,

wow and thank you for your quick reply.

its gonna take me a little while to process that! but its a massive help thanks.

Lee.
User is offlineProfile CardPM

Go to the top of the page

itlee
post 18 Jul, 2008 - 12:24 AM
Post #4


New D.I.C Head

*
Joined: 16 Jul, 2008
Posts: 3


My Contributions


baavgai,

I have had a chance to understand your code and it works brilliantly.

Thanks,
Lee.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/6/08 11:28PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month