Archived

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

Maddawg

Newbie trying to start a rpg

Recommended Posts

Maddawg    122
Here is what i have.... I can call the classes but what i would like is a way to have them select a race and be able to call the player class no mater what race they choose?
  
#include "stdafx.h"
#include "dos.h"
#include "iostream.h"
#include "stdio.h"
#include "string.h"


class player
{
public:

	char NAME[8];
	char RACE[8];
	int HP;
	int DEX;
	int STR;
	int AC;
	int INT;
	int MANA;
};

class monster
{
public:
	
	char NAME[8];
	int HP;
	int DEX;
	int STR;
	int AC;
	int INT;
	int MANA;
};


int main(int argc, char* argv[])
{
cout << "Choose a race\n\n";

cout << "1. Ogre\n";
cout << "2. Troll\n";
cout << "3. Human\n";
cout << "4. Elf\n";

int race; 

cin>>race; 

if (race == 1) 
{ 
    cout<<"You are an Ogre.\n"; 

player ogre;

ogre.RACE = 1;
ogre.HP = 30;
ogre.AC = 7;
ogre.STR = 20;
ogre.DEX = 8;
ogre.INT = 3;

cout << "your max HP is " << ogre.HP << ".\n";

} 
else if (race == 2) 
{ 
    cout<<"You are a Troll.\n"; 

player troll;

troll.RACE = 2;
troll.HP = 23;
troll.AC = 9;
troll.STR = 14;
troll.DEX = 9;
troll.INT = 5;

} 
else if (race == 3) 
{ 
    cout<<"You are a Human.\n"; 

player human;

human.RACE = 3;
human.HP = 15;
human.AC = 5;
human.STR = 10;
human.DEX = 16;
human.INT = 9;

} 
else if (race == 4) 
{ 
    cout<<"You are an Elf.\n"; 

player elf;

elf.RACE = 4;
elf.HP = 11;
elf.AC = 11;
elf.STR = 11;
elf.DEX = 16;
elf.INT = 6;

} 
else 
{ 
    cout<<"That wasn't a choice!!!\n"; 
} 


	return 0;
}
  
Thank you for any help. [edited by - Maddawg on April 29, 2003 9:00:42 PM]

Share this post


Link to post
Share on other sites
micepick    122
Why don''t you just do:

class CPlayer {...}

int main()
{
CPlayer player;

// blah blah

if (race == 1)
{
player.RACE = "Ogre";
// etc
}
else if (race == 2)
{
player.RACE = "Troll";
// etc
}
}

You should probably be using a switch statement instead of all those ifs.

Share this post


Link to post
Share on other sites
wizard341    144
Polymorhpism might be a good idea to try out here, im not sure just how deep of an rpg your making, but if you want to have more than 1 character using this method would be very very cool.

Here is a code snipet one of the forum vistors wrote earlier today demonstrating it.


  
class IShape {
public: virtual void draw() = 0;
};

class Circle : public IShape {
public: void draw() { draw circle here }
};

class Rectangle : public IShape {
public: void draw() { draw rectangle here }
};

class Polygon : public IShape{
public: void draw() { draw rectangle here }
};

void main()
{
IShape* vecShapes[3];
vecShapes[0] = new Circle();
vecShapes[1] = new Rectangle();
vecShapes[2] = new Polygon(); // draw all shapes


for(int loop = 0; loop < 3; ++loop)
{
vecShapes[loop]->draw();
}
for(int loop = 0; loop < 3; ++loop)
{
delete vecShapes[loop];
}
}

So basically, as long as each race has the same variables that you want to access (ie, hp, dex, str etx) you dont need to know what they are, only that they are stored in some type of an array.

So ill try to go one step further and help show you what you could do.

Player should be a base class, and you should create a virtual constructor, or if you want some other method to set all the values you want.

So if you want to create a warrior, you would of course make another class, and make sure to overwrite the constructor, or the other function you want to use to create the warrior.

  
class Player
{
public: virtual void setProperty; //{overwrite me!}

};
class Player : public Warrior {
public: void setProperty(); //(paraters we want)

};

so basically to use this, all we need to do is say,

Player* players[5];
players[0] = new Warrior();
players[0].setProperty(parameters);


So this looks pretty standard and all, and in fact you can acomplish this thing very easily with ordinary classes. But lets add another feature to this game.

Say we want to have a thief, a mage, and a warrior. Each of them have 5 special things they can do that no other class can. A thief can climb walls, assasinate low level mobs, steal and a few other things. Mages can cast fireball, levitate, create a shield barrior and 2 more things. Finally warriors can perform first aid, do a super slash, and get a bonus to strength after being damaged. To do these things, we would need alot of unecessary checks when we actually want to do them.


Say the user pressed key 1, which makes your character do his special move 1st special move. So to do this, we would need a kind of long if/then statement (even longer if we had many more classes!)Not only that, but the end user must know exactly what each classes 1st special move must be, and tell the programmer exactly what to do.

if(player[x] == mage)
player[x].castfireball();
if(player[x] == thief)
player[x].climbwall();
if(player[x] == warrior)
player[x].firstaid();

You can see this can make code look pretty nasty, and thats where polymorphism's charm comes in. So the user presses 1, but instead of figuring everything out ourselves, we tell the program (which already knows) to find the class and run the function. So all we have to do (assuming that each class has a function that has overwritten its parents virtual class is:

player[x]->specialMove1();

See how cool that is!? If that player was a mage he will cast a fireball, if he was a thief he will climb the wall and if hes a warrior he will perform first aid.

[edited by - wizard341 on April 29, 2003 10:28:21 PM]

Share this post


Link to post
Share on other sites