Welcome to Dream.In.Code
Getting Help is Easy!

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!




Combat question

 
Reply to this topicStart new topic

Combat question, console application I'm modifying

fuzzylr
post 4 Oct, 2008 - 03:02 PM
Post #1


New D.I.C Head

*
Joined: 4 Oct, 2008
Posts: 7


My Contributions


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.

Fuzzy
User is offlineProfile CardPM

Go to the top of the page


Martyr2
post 4 Oct, 2008 - 10:29 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,008



Thanked 170 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


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.

smile.gif

User is offlineProfile CardPM

Go to the top of the page

fuzzylr
post 4 Oct, 2008 - 10:58 PM
Post #3


New D.I.C Head

*
Joined: 4 Oct, 2008
Posts: 7


My Contributions


QUOTE(Martyr2 @ 4 Oct, 2008 - 11:29 PM) *

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.

smile.gif


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.
User is offlineProfile CardPM

Go to the top of the page

.Maleficus.
post 5 Oct, 2008 - 05:31 AM
Post #4


D.I.C Head

**
Joined: 7 Mar, 2008
Posts: 126



Thanked 2 times
My Contributions


You need at least 10 posts I believe.

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.

Take a look here (http://www.cs.bu.edu/teaching/cpp/polymorphism/intro/) for a little more detail.

This post has been edited by .Maleficus.: 5 Oct, 2008 - 05:32 AM
User is offlineProfile CardPM

Go to the top of the page

stayscrisp
post 5 Oct, 2008 - 08:14 AM
Post #5


D.I.C Regular

***
Joined: 14 Feb, 2008
Posts: 257



Thanked 6 times
My Contributions


I have attached an essay i wrote for college about inheritance, its hopefully quite informative and helpful, enjoy smile.gif



Attached File(s)
Attached File  Inheritance_essay.pdf ( 17.46k ) Number of downloads: 10
User is online!Profile CardPM

Go to the top of the page

fuzzylr
post 5 Oct, 2008 - 11:34 AM
Post #6


New D.I.C Head

*
Joined: 4 Oct, 2008
Posts: 7


My Contributions


QUOTE(stayscrisp @ 5 Oct, 2008 - 09:14 AM) *

I have attached an essay i wrote for college about inheritance, its hopefully quite informative and helpful, enjoy smile.gif


That was some good information. I left you a thanks for that. I'm dying to see some examples...examine the code and try to understand it.. :-)
User is offlineProfile CardPM

Go to the top of the page

fuzzylr
post 5 Oct, 2008 - 12:59 PM
Post #7


New D.I.C Head

*
Joined: 4 Oct, 2008
Posts: 7


My Contributions


QUOTE(.Maleficus. @ 5 Oct, 2008 - 06:31 AM) *

You need at least 10 posts I believe.

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.

Take a look here (http://www.cs.bu.edu/teaching/cpp/polymorphism/intro/) for a little more detail.


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
User is offlineProfile CardPM

Go to the top of the page

stayscrisp
post 6 Oct, 2008 - 12:19 PM
Post #8


D.I.C Regular

***
Joined: 14 Feb, 2008
Posts: 257



Thanked 6 times
My Contributions


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

http://www.codersource.net/cpp_virtual_functions.html

http://www.codersource.net/cpp_overriding.html

http://www.codersource.net/cpp_tutorial_inheritance.html

icon_up.gif your on the right track

This post has been edited by stayscrisp: 6 Oct, 2008 - 12:20 PM
User is online!Profile CardPM

Go to the top of the page

fuzzylr
post 10 Oct, 2008 - 11:20 PM
Post #9


New D.I.C Head

*
Joined: 4 Oct, 2008
Posts: 7


My Contributions


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;};
            
    bool IsDead()
    {
        return (hp == 0);
    }
};

Player::Player()
{
    hp=20;
    str=12;
    att=(rand() % 12) +2;
}

#endif


and the main.ccp

CODE
#include <iostream>
#include <ctime>
#include "monster.h"
#include "player.h"

using namespace std;

// 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
User is offlineProfile CardPM

Go to the top of the page

fuzzylr
post 11 Oct, 2008 - 08:51 PM
Post #10


New D.I.C Head

*
Joined: 4 Oct, 2008
Posts: 7


My Contributions


never mind I fixed it. it was a simple fix.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 04:34AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month