Archived

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

hello_there

c++ class problem

Recommended Posts

hello_there    122
i have a base class AI then i have a class spider which is derived from AI. my game is tile based and the level array is in the class level which ai can''t access so in main i copied the level data to a array in AI. know why can''t spider use that data. all the data in AI is public. and spider inherits it like this class spiderublic AI. can you tell me why or tell me how i would do it. i need to read over some class stuff again but i can''t find my c++ book.

Share this post


Link to post
Share on other sites
Oluseyi    2103
quote:
Original post by hello_there
my game is tile based and the level array is in the class level which ai can''t access so in main i copied the level data to a array in AI.

What does this mean? Do you copy the data into an array in an instance of AI, or to a shared class variable (static)?

Either way, the proper solution is to provide accessor methods in the level class and then use those methods in algorithms involving the spider and the level.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I would suggest not having your Spider class inherit from your AI class. If a Spider object used or had (uses-a, has-a relationships) an AI object this would allow for modular AI. You could either pass in an AI object in the constructor or do something like the following:


  
class AI {/* ...*/ };
class NeuralNetAI : public AI {/* ... */
};

class Actor
{
public:
// ...


private:
virtual AI* CreateAI()=0;

AI* brain:
};

class Spider : public Actor
{
public:
Spider() {brain = CreateAI();}

private
AI* CreateAI() {return new NeuralNetAI();}
};


So you just have your Spider class work with the AI interface and you are free to have a Spider pick whatever AI it wants. You could also provide multiple AI subclasses and have the Spider take a constructor parameter that identifies which AI subclass to use. This can be done with a integer parameter and a switch statement.

As for accessing the level in the AI, you could give the Actor class an insert method that tells the Actor the level it is in. The actor can then tell the AI which level it is in. Something like the following:


  
class Level {/* ... */};

class AI
{
public:
// ...

void setLevel(Level *level) {this->level = level;}

private:
// ...

Level *level;
};

class Actor
{
public:
// ...

void insert(Level *level) {brain.setLevel(level);}
};


This way you only have to think about inserting an Actor into a level and the AI knows this automatically. You could do all of this through constructors, but this allows for an Actor to be reused in multiple levels. Actually, now that I think of it, an AI object should probably never have a level pointer at all. Any computation it does should just take an accessor object (level iterator perhaps) that represents where it is in the level. The AI will just access the level through the interface. Something like:


  
class Level
{
public:
class LevelIterator
{
public:
void moveUp() const;
void moveDown() const;
// ...

};

// ...

};

// Then in the AI class:

void NeuralNetAI::generateMove(LevelIterator *iter)
{
// ...

}


Anyway, I''m rambling. My point is to use object composition as well as object inheritence to achieve your objective. Inheritence is a tad overused.

Share this post


Link to post
Share on other sites