Thread: RPG ( practice with classes )

  1. #1
    Registered Loser nickodonnell's Avatar
    Join Date
    Sep 2005
    Location
    United States
    Posts
    33

    Question RPG ( practice with classes )

    I had to flip a coin to decide whether this should go on this forum, or the C++ forum. Heads won...

    I'm messing with classes, getting familliar with them. I came up with a (probably) common way of doing character stats in an RPG.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Stats
    {
    public:
    	Stats();
    	~Stats();
    
    	void strength(int str);
    	int displaystrength();
    
    	void defense(int def);
    	int displaydefense();
    
    	void health(int hea);
    	int displayhealth();
    
    protected:
    	int power;
    	int vitality;
    	int hitpoints;
    };
    
    Stats::Stats()
    {
    }
    
    Stats::~Stats()
    {
    }
    
    void Stats::strength(int str)
    {
    	power=str;
    }
    
    int Stats::displaystrength()
    {
    	return power;
    }
    
    void Stats::defense(int def)
    {
    	vitality=def;
    }
    
    int Stats::displaydefense()
    {
    	return vitality;
    }
    
    void Stats::health(int hea)
    {
    	hitpoints=hea;
    }
    
    int Stats::displayhealth()
    {
    	return hitpoints;
    }
    
    int main()
    {
    
    	string char_class;
    	int classcount = 0;
    	string char_name;
    
    	Stats warrior;
    	warrior.strength(10);
    	warrior.defense(7);
    	warrior.health(120);
    
    	Stats rogue;
    	rogue.strength(6);
    	rogue.defense(8);
    	rogue.health(200);
    
    	Stats defender;
    	defender.strength(6);
    	defender.defense(12);
    	defender.health(150);
    
    	Stats character;
    
    	cout<<"*Class List*\n";
    	cout<<"\nWarrior\n";
    	cout<<"Rogue\n";
    	cout<<"Defender\n";
    
    	while (classcount < 1)
    	{
    		cout<<"\nChoose your class: ";
    		cin>>char_class;
    		cin.ignore();
    
    		if ((char_class == "Warrior") || (char_class == "warrior"))
    		{
    			character.strength(10);
    			character.defense(7);
    			character.health(120);
    			classcount++;
    		}
    		else if ((char_class == "Rogue") || (char_class == "rogue"))
    		{
    			character.strength(6);
    			character.defense(8);
    			character.health(200);
    			classcount++;
    		}
    		else if ((char_class == "Defender") || (char_class == "defender"))
    		{
    			character.strength(6);
    			character.defense(12);
    			character.health(150);
    			classcount++;
    		}
    		else
    		{
    			cout<<"\nPlease choose a valid class.\n";
    		}
    	}
    
    	cout<<"\nAlright, you are a " <<char_class<< ".\n";
    	cout<<"Now, enter your name: ";
    	cin>>char_name;
    	cin.ignore();
    	cout<<"\nOk, "<<char_name<<", here are your stats.\n\n";
    
    	cout<<"Power -- "<<character.displaystrength()<<endl;
    	cout<<"Vitality -- "<<character.displaydefense()<<endl;
    	cout<<"Hit Points -- "<<character.displayhealth()<<endl;
    
    	cin.get();
    }
    I've got the basic stuff down, but I'm wanting to take it farther.

    How would I make an enemy list of about 20 monsters? Would I use a separate class?

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Yeah looks good so far.

    How would I make an enemy list of about 20 monsters? Would I use a separate class?
    There are several ways to do this. I would use a vector from the STL. Create your base monster class and then create a vector of monsters.

    Code:
    class MonsterManager;
    
    class Monster
    {
      private:
        Monster(void) {}
        Monster(const Monster &m) {}
      public:
        friend class MonsterManager;
    
         ...
    
    };
    
    class MonsterManager
    {
      std::vector<Monster>  MonsterList;
    
    
      
      public:
    
        void AddMonster(...);
        ....
    
    
    };
    Essentially you have a class for the object and a class that manages a list of the objects. I find that routing all access to the objects through a class that manages the objects is much more efficient and centralized. So if you want to add a monster you don't specifically create one using the Monster constructor since it is private. The MonsterManager class is a friend of the Monster class and therefore can create a new Monster by calling Monster's constructor. The user of the MonsterManager class simply calls Add with the parameters it wants for the new monster and the rest is done by the manager class.

    Set it up any way you want.....this is just how I normally do it so I can directly control access to objects and have much more control over run-time creation and destruction of objects. An easy way to make sure that no unauthorized access is allowed for the object vector is to return an ID number in the Add() function.

    Code:
    DWORD MonsterManager::Add(.....)
    This ID can simply be the index value of the object in the vector or you can create a map of IDs that then map to indexes into the vector. All future access to any monster attributes and/or functions is then allowed only if the ID passed to the function is valid. If it's not, then the function simply returns. This allows you to have complete control over your monster system from start to finish.




    And this is something you might want to try as well:

    I've wanted to experiment with a type of DNA inheritance for RPG characters. This would be something like the Sims 2 DNA code that would spawn off characters that 'inherited' traits from their parents.

    Essentially you would have a base set of monsters/characters and then from there the game engine could literally create thousands more - at least stats-wise. The simplest DNA encoding I could come up with was using the logical operators and then extracting the binary values.

    For instance in one byte you could have traits like this:

    1001000

    Then use a logical operator to combine DNA of another character with this one.

    So:

    1001000
    AND
    1100101
    -----------
    1000000

    Each field or binary value can represent a 'trait' of your choosing and you can set inheritance rules for that trait and use logical operators to define the 'rules'.

    I'd be interested to see what new types of characters could be created using this scheme. Each game would probably be quite different depending on who 'mated'
    Last edited by VirtualAce; 09-30-2005 at 03:43 PM.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The first thing I'd do is make a constructor that would initalize the object for you. That'll make:
    Code:
    Stats warrior;
    warrior.strength(10);
    warrior.defense(7);
    warrior.health(120);
    into
    Code:
    Stats warrior(10,7,120);
    which is much cleaner.

    Now what I would suggest is looking into inheritance. I would create a class that defines everything that is similar between your "heros" and your "monsters". Then you create your Hero class from that class defining anything that Heros have that monster don't. And create a monster class from that orignal class that defines anything monsters have that heroes don't.

    I'd also looking into some containers for storing your different hero objects and monster objects. Maybe a map or something like that.

  4. #4
    Registered Loser nickodonnell's Avatar
    Join Date
    Sep 2005
    Location
    United States
    Posts
    33
    Thanks for the help. Yeah, I've got alot of reading up to do, but I think after this I'll get it eventually.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem
    By ExDHaos in forum C++ Programming
    Replies: 12
    Last Post: 05-22-2009, 04:50 AM
  2. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. practice with classes
    By tetra in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2003, 12:02 AM