tags:

views:

219

answers:

5

Okay... I'll try to describe it.

It's a text adventure game where you have items you pick up and encounter monsters in rooms (they can be hard-coded in). All my pickups are going to do damage, and the player and the monsters have hitpoints, a name and damage (so the pickups just add to the player's damage when you use them).

We have to have an item base class and a creature base class. All the pickups are derived classes from the Item class and the player and monsters are derived from the Creature class. Except I'm not sure about the specifics of how to make those individual monsters - like, would each monster be its own class? Or would they be different individual objects of an overall monster class? And what would the syntax look like?

I also need to have a pure virtual function under the Item class, but I have no idea what part of the items could be virtual - I mean, all they have is an int for damage at this point, and the only other thing they do is that if a player has a particular item then you can get past into certain rooms.

Anyways, any help is appreciated. If you can show me specific code for how you would initialize the monsters or make the derived classes, that would be nice. I feel like I'm asking really basic stuff, but my programming classes are just getting over my head to the point where I'm losing stuff I was comfortable with before.

So yeah... do people get what I'm talking about, or are more specifics needed? Thanks again.

A: 

Virtual functions are useful to create a "polymorphic" interface for your classes. So if you had a base Item class, it might have a virtual method to access the int for the damage. Then the subclasses (sword, bow, etc?) can override that method to fill in their own damage value.

Some other block of code can then interact with all the different sub-items via the a pointer or reference to the base class and access the damage via the virtual method, without having to know the exact type of item it's dealing with.

Not including code as this is homework, but this should give you a rationale for why a virtual function there would be useful. It could be a pure virtual function since the base item class is abstract, i.e. it does not have a damage value associated with it -- only the subclasses do.

Hope this helps, good luck!

Mike Kale
+3  A: 

Normally, you would want to create derived classes if those classes would have additional data or different member function behavior.

If all your Monsters simply hold an int damage, then just a Monster class is enough for now.

Create derived classes when a particular Monster needs unique data or needs to handle a member function differently.

It sounds like you've decided to use inheritance and virtual functions first, and now you're trying to figure out why you would have decided that. Keep your project simple for as long as it can be.

Shmoopty
A: 

For your Item class, a simple virtual method you could have is to check whether a certain room is enterable with that item, such as

virtual bool accessRoom(Room room);

This of course assumes you have a room type. If you don't, you could just use an int (which is perhaps an index into an array).

Each type of monster should be it's own class - that's the purpose of a class, to encapsulate a type.

class Animal {
public:
  virtual ~Animal(){}
  virtual void speak() const = 0; 
  std::string getName() const { return name; }

protected:
  const std::string name;
};

class Dog : public Animal {
public:
  Dog(): name("Dog") { }
  void speak() const { cout << "ruff" << endl; }
};

class Cat : public Animal { 
public:
  Cat(): name("Cat") { }
  void speak() const { cout << "meow" << endl; }
};

If you want to interact with a derived class with polymorphism, you must user pointers.

Animal* barn[3];
barn[0] = new Cat();
barn[1] = new Dog();
barn[2] = new Dog();

for(int i = 0; i < 2; i++){
  barn[i]->speak();
  delete barn[i];
}
FranticPedantic
Just have a nagging feeling with code responses to homework questions: just pointing him in the right direction should be good. Teach him to fish on his own...
Gishu
I have to disagree with Gishu. Code can ground abstract concepts. We shouldn't punish some learners on the off chance that others might not pay their dues properly. In any case, this is hardly a complete program.
willc2
A: 

I'd start with the parser, and then add appropriate classes/structure to create the behavior you need to add. Once you have that, it will probably be a little easier to see where the commonality really is.

A bad class hierarchy can really make your life difficult.

kyoryu
+2  A: 

If you want sample code or anything to get inspired from you can always google up the open source classic game NetHack.

dudico