Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collections Advice Please?

Status
Not open for further replies.

itlee

Programmer
Feb 14, 2001
34
CH
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 implement.

Thanks,
Lee.

itlee. MCP\Analyst\Programmer\SQL\.NET\VB\C#
 
Interesting. In VB, there is a generic collection, that you can just add objects to, which should work for what you want. There should be a C# equiv.

-Sometimes the answer to your question is the hack that works
 
interface IObject
{
void Render();
}

class Star : IObject{}
class Planet : IObject{}

then use a a generic list to store all the objects
foreach(IObject obj in new IObject[] {new Star(), new Planet()})
{
obj.Render();
}

this is one option.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Another option is to create a base class which all of your other objects are derived from, create a System.Collections.Generic.List<object> this will then hold any object derived from it.

Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class spaceobject
    {
        private string _name;

        public string name
        {
            get { return _name; }
            set { _name = value; }
        }
	
    }
    class spaceship : spaceobject
    {
        private int _shipNumber;

        public int shipNumber
        {
            get { return _shipNumber; }
            set { _shipNumber = value; }
        }
	
    }
    class planet : spaceobject
    {
        private string _colour;

        public string colour
        {
            get { return _colour; }
            set { _colour = value; }
        }
	
    }
    class Program
    {
        static void Main(string[] args)
        {
            System.Collections.Generic.List<spaceobject> objects = new System.Collections.Generic.List<spaceobject>();

            spaceship ship = new spaceship();
            ship.name = "Enterprise";
            ship.shipNumber = 4;

            planet earth = new planet();
            earth.name = "earth";
            earth.colour = "green";

            objects.Add(ship);
            objects.Add(earth);
            Console.WriteLine(objects[0].name + objects[1].name);
            Console.ReadKey();
        }
    }

}

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
Rick Cook, The Wizardry Compiled
 
blounty,

What happens if I am iterating through objects and try to access the shipNumber or colour properties that aren't part of the spaceobject class that the collection was built on? Are they still accessible or would I have to cast the object to its actual class?

Thanks,
Bill
 
you could use reflection to see what the object is the do what you need based on that.

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
Rick Cook, The Wizardry Compiled
 
if you use the command pattern (my example above) then you would pass the board to each object in the constructor. all rendering is done withing the object itself. so you would not need public accessors to details like ship number, or brightness of star.

the board should only care how objects interact with each other (a ship cannot fly through a star, or stars cannot move). other than that the board should not care what the object is.

each object (star, ship, planet) would have a set of movement rules. the board would use these rules to determine if a move is valid or not. (for stars/planets you would have a no-move move).

so to expand on my idea above
Code:
(used to be IObject)
public interface IPiece
{
   //draw image on board
   void RenderTo(IBoard board);

   //determines if the current move is valid
   bool IsAValidMove(IMovement move);

   //a list of all moves
   IMovementRules[] GetAllValidMoves;

   //called when the player takes their turn
   void TakeTurn();   
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top