Join 131,615 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,094 people online right now. Registration is fast and FREE... Join Now!
I am taking a console application that I've modified and I want to add more of a complete combat system. Nothing to complicated yet. The orginal code would generate random number for each encounter and then determine of the monster or the player was still alive.
What I want to do is add more than one type of monster and add static stats no levels and no regen and engage in combat.
Example:
CODE
public:
void Kobal() { int Hp = 20; unsigned int Attack = (rand() % 4) +1; int Defense = 12; int Spirit = 6; }
void Orce() { int Hp = 30; unsigned int Attack = (rand() % 8) +2; int Defense = 16; int Spirit = 12; }
void Troll() { int Hp = 40; unsigned int Attack = (rand() % 10) +2; int Defense = 19; int Spirit = 16; }
void Player() { int Hp = 25; unsigned int Attack = (rand() % 8) +2; int Defense = 14; int Spirit = 13; } bool IsDead() { return(Hp == 0); }
I'm stuck on the actual coding part. I ahve an idea in my head..I'm just not sure how to make it happen.
Have you ever considered making a monster base class which has hp, defense, and spirit members along with an attack method? Make it abstract and then inherit the various monsters from it. The reason I mention it is that it would give you the opportunity to create as many monsters as you want, limit the redundant code you are spewing out there in the functions, plus it would allow you to setup your combat easier with something like
cpp
Troll *troll1 = new Troll();
// Assign damage to troll that equals player1 attack troll1->damage(player1->attack());
if (troll1->getCurrentHP() <= 0) { // Troll is dead, destroy troll, assign rewards to player1 } else { // Troll survived, now it attacks player player1->damage(troll1->attack()); }
You get the idea I am sure. I thought I would mention it before you get to far into that path you are going through with. In the example above the Troll object would inherit from the Monster base class and override the attack method to deal appropriate range of damage etc.
Have you ever considered making a monster base class which has hp, defense, and spirit members along with an attack method? Make it abstract and then inherit the various monsters from it. The reason I mention it is that it would give you the opportunity to create as many monsters as you want, limit the redundant code you are spewing out there in the functions, plus it would allow you to setup your combat easier with something like
cpp
Troll *troll1 = new Troll();
// Assign damage to troll that equals player1 attack troll1->damage(player1->attack());
if (troll1->getCurrentHP() <= 0) { // Troll is dead, destroy troll, assign rewards to player1 } else { // Troll survived, now it attacks player player1->damage(troll1->attack()); }
You get the idea I am sure. I thought I would mention it before you get to far into that path you are going through with. In the example above the Troll object would inherit from the Monster base class and override the attack method to deal appropriate range of damage etc.
Would you mind if we talk some more? I've been through programming 1. going back for programing two. I'm very interesting in grasping what you talking about. It was the direction I was stumbling towards by doing what I was doing. I don't have much expirence with structs, classes. If your willing I'd like learning more about what your speaking of. I still need a lot of expirence. :-/
I tried to PM but I guess I'm missing something...wouldn't let me.
What Martyr2 is talking about is polymorphism, or inheritance. The idea behind it is to make one base class (or object) with all of the generic functions and properties of that type. That might not make much sense, so here's an example.
Trolls, Kobal and Orce are all Monster() objects. Every monster has a few base properties, like HP, Defense and Sprint. They also all have an Attack() function. So, to save code length, flexibility and time what we do is create that base Monster() class and create the different monsters from there. Your Troll class would then extend the Monster class, thus giving it every property and function of the Monster class. You can then go in and tweak only what needs tweaking, ie, give it more HP, a better attack, etc.
What Martyr2 is talking about is polymorphism, or inheritance. The idea behind it is to make one base class (or object) with all of the generic functions and properties of that type. That might not make much sense, so here's an example.
Trolls, Kobal and Orce are all Monster() objects. Every monster has a few base properties, like HP, Defense and Sprint. They also all have an Attack() function. So, to save code length, flexibility and time what we do is create that base Monster() class and create the different monsters from there. Your Troll class would then extend the Monster class, thus giving it every property and function of the Monster class. You can then go in and tweak only what needs tweaking, ie, give it more HP, a better attack, etc.
Hey, after all your guys explaining and reading that section on your post. I came up with the following code for my mosters. Tell me what you guys think. I have zero errors at this time. I've not defined any specific classes but hey it's a seriously good step in the right direction.
CODE
// Base definition of all monster classes. class Monster { public: Monster(char theName[10], int theHp, int theAttack, int theDefense, int theStrength, int theAgility, int theStaminia);
// Class methods char getName() const; int getHP() const; int getAttack() const; int getDefense() const; int getStrength() const; int getAgility() const; int getStamina() const;
protected: char Name; int Hp; int Attack; int Defense; int Agility; int Stamina; };
Monster::Monster(char theName[10], int theHp, int theAttack, int theDefense, int theStrength, int theAgility, int theStamina) { char name =theName[10]; int hp =theHp; int attack =theAttack; int defense =theDefense; int strength=theStrength; int agility =theAgility; int stamina =theStamina; }
This post has been edited by fuzzylr: 5 Oct, 2008 - 01:55 PM
I think your starting to get the concept but you need to read some more tutorials, your base class will need virtual functions so that your derived classes can use there own version of the function.
that said , that doesn't mean you should make every function a virtual one just in case someone wants to change that function always think ahead in your project.
also if your base class does not need an implementation of a certain function then it should be declared as pure virtual, heres a link for you to read and have another go, then repost and we'll give you some more pointers
Thanks for the words of encouragement. Anyways, I have a question. I'm struggling to figure out how capture the players name so it can be reused. Here is the code at present.
Header
CODE
#define player_h #ifdef player_h
#include <iostream> #include <string> using namespace std;
// Base definition of all player class. class Player { int hp, str, att; string name;
public: Player(); int gethp(){return hp;}; int getstr(){return str;}; int getatt(){return att;}; string getname(){return name;};
// Initlialize the random number generator void roll() { // Not yet being used. time_t qTime; time(&qTime); srand((unsigned int)qTime); }
int main(int argc, char* argv[]) {
Player user; cout<<"Welcome to Fuzzy's test Console RPG\n"; cout<<"\n"; cout<<"Enter player name: "; cin>>user.getname(); cout<<"\n\nWelcome "<<user.getname()<<"\n"; cout<<"Your health is: "<<user.gethp()<<"\n"; cout<<"Your strength is: "<<user.getstr()<<"\n"; cout<<"You have ventured into an untold adventure leading to most "; cout<<"likely your doom. Be wary of the road ahead. Good luck Adventurer.\n"; }
The problem is if you look in the Int main and see I take the users name and then I display it later on...I think the problem is I'm not storing the data from name in the class...I'm so tired and cross eyed.
This post has been edited by fuzzylr: 10 Oct, 2008 - 11:24 PM