Classes

Pages: 1234
An object is a variable of the class


yes, although a better example might have been:

1
2
3
4
class ShortSword {};

ShortSword MyWeapon;


MyWeapon is the object, and it is of type ShortSword which is defined in the class.

I was trying to figure out a way to import the file through the constructor


I was thinking of having a function that reads the file, getting info for each type of weapon, create an object of that weapon type calling it's constructor at the same time. Here is an example using the new operator to return a pointer and calling a constructor at the same time.

1
2
CShortSword *pMyShortSwd = new CShortSword( args in here)  //creates a pointer & calls a    
                                                                                                     //constructor that matchs the args 


Here it is again without using new and no pointer.
1
2
CShortSword MyShortSwd( args in here)  //calls a    
                                                                 //constructor that matches the args 


However, I like the new version better because it gives me the pointer, which I would use to access the objects functions.


But first you need to design the classes, so that you can get the inheritance right and know what the member variables for each are, and have the appropriate constructors and functions for each.

I mentioned the design process earlier - it's really quite simple.

Would I create a getDamage member in the base class and call it in the derived classes?


Yes, and it is a member function that should be inherited to the derived classes. This should become clear when you do the design.

So to start the design, post a list of all the weapons you would like to have, along with the attributes and abilities for each one. Don't worry if there is repetition in these ( actually I expect to see it) - that is the point of designing, so we can see where it is and alter things so there isn't repetition.

Once you have done this, then I can explain the next bit.
I am using Code::Blocks. When I create a class, it creates a .h (header file) and a .cpp (contains a constructor and destructor)
Displayed like this:
weapons::weapons()
{
    //ctor
}

weapons::~weapons()
{
    //dtor
}

It also creates a constructor and destructor in the header file like this:
1
2
weapons();
~weapons();


Why does it do this and should I use the .cpp file?
I get compiler errors when declaring a const member variable.
Do I have to do something in the .cpp file?
Last edited on
OK, the header file has the declaration of the class and it's member variables and functions. That is this bit:
1
2
3
4
5
6
7
8
9
10
11
class weapons {
private:
//declaration of member variables here
//and private member functions
public:
weapons();   //default constructor
weapons(string arg1, string arg2);   //another constructor with args
~weapons();  //destructor

//public member functions here
};


The .cpp file has the definition of the functions and initialisation of the member variables. Like this:

1
2
3
4
5
6
7
8
9
10
11
12
weapons::weapons()
{
    //ctor
//put the code for the constructor here
}

weapons::~weapons()
{
    //dtor
//dont worry about this one
}
//put the definitions for other functions here 



Now about naming classes - don't use plurals like weapons Just weapon. This is because when you create an object you only create one of them. I put a C at the front so I know it is a class. Use Capitalisation of Words - so you get this CWeapon or CShortSword.

And how are you going with your list of weapons and their attributes?

It's important to do design, otherwise you will spend a heap of time rewriting code.

I get compiler errors when declaring a const member variable.


Interesting. I would like to see the design first.



I think I figured it out. Was watching youtube. ha
http://www.youtube.com/watch?v=53VYYMy-LBo&feature=player_detailpage#t=121s
The video should be at the correct time where he talks about my problem.
if (time != correct)
{
2 min
} =D

I will start typing my weapon list. Have it written on paper.
Quality ( This will be 1.00 base. Reductions occur during use.)
Value ( I won't type this, Just note value will be included)
Critical Hit Chance ( have not put much thought into the value of this yet. Daggers ill have a higher chance)


I'm assuming I will add the extra damage (if required) in the derived class for each weapon.
Such as critical's, crushing blows, and causing bleeding.

Wooden Short Sword
Damage: 9
Attack Rating: 90
Attack Speed: 1.35

Short swords have a reduced attack range.
Swords have a chance to cut enemies and make
them bleed out.

Wooden swords are the lightest of the sword class
and attack faster. Wooden swords can shatter!

BSS
Name: Bronze Short Sword	 Quality: 1.00

Damage: 16
Attack Rating: 90
Attack Speed: 1.20

Short swords have a reduced attack range.
Swords have a chance to cut enemies and make
them bleed out.

Bronze swords are stronger than wooden swords,
but require frequent repairs.

ISS
Name: Iron Short Sword	 Quality: 1.00

Damage: 24
Attack Rating: 90
Attack Speed: 1.20

Short swords have a reduced attack range.
Swords have a chance to cut enemies and make
them bleed out.

Iron swords can slash through the lightest
of armors with ease.

SSS
Name: Steel Short Sword	 Quality: 1.00

Damage: 34
Attack Rating: 90
Attack Speed: 1.20

Short swords have a reduced attack range.
Swords have a chance to cut enemies and make
them bleed out.

Steel is smelted with iron and coal which keeps
them sharp. They have an increased chance to cut.

SISS
Name: Silver Short Sword	 Quality: 1.00

Damage: 46 + 4
Attack Rating: 90
Attack Speed: 1.20

Short swords have a reduced attack range.
Swords have a chance to cut enemies and make
them bleed out.

Silver is the most powerful of metals against
undead enemies. Gains a bonus against undead.

GSS
Name: Glass Short Sword	 Quality: 1.00

Damage: 60
Attack Rating: 90
Attack Speed: 1.25

Short swords have a reduced attack range.
Swords have a chance to cut enemies and make
them bleed out.

Glass is the sharpest of weapons. Glass swords
have an increased chance to cut.

------------------------------------------------------------------

BLS
Name: Bronze Long Sword	 Quality: 1.00

Damage: 18
Attack Rating: 95
Attack Speed: 1.15

Long swords have a increased attack range.
Swords have a chance to cut enemies and make
them bleed out.

Bronze swords are stronger than wooden swords,
but require frequent repairs.

ILS
Name: Iron Long Sword	 Quality: 1.00

Damage: 27
Attack Rating: 95
Attack Speed: 1.15

Long swords have a increased attack range.
Swords have a chance to cut enemies and make
them bleed out.

Iron swords can slash through the lightest
of armors with ease.

SLS
Name: Steel Long Sword	 Quality: 1.00

Damage: 27
Attack Rating: 95
Attack Speed: 1.15

Long swords have a increased attack range.
Swords have a chance to cut enemies and make
them bleed out.

Steel is smelted with iron and coal which keeps
them sharp. They have an increased chance to cut.



This is what I have written so far. I am having my brother help me with two handed weapons.
Final list will contain these swords and axes, maces, staffs, 2h Sword, 2h axes, daggers, and bows. (maybe crossbows)
I want to add spells, but that is another animal all together.
Ok, We have all these swords, so now we need to sort all the things that are the same.

They all have Damage, Attack Rating, Attack speed. I would add Length (short or long), Material Type, Cutting effectiveness ( a number), ability to Cut ( a bool) , and Range, repair frequency as attributes.

What would be good, (to do a proper analysis) is a couple of examples of the other types - axes, maces, staves, daggers, clubs and bows.

I am guessing we will need classes for each of these types plus some others, because of things like cutting ability which applies to swords, axes, daggers etc.

So we are trying to get a list of all the attributes, decide which are common so we can push them up the inheritance tree, so that we only specify things once.

Ok, this is an idea of how it might look so far - might need to change this as we get more info - so don't write any code. Also might need more imaginative names than these!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class CWeapon {
string mName;   //almost forgot this one!!
float mDamage;  //maybe unsigned not sure how this works yet
float mAttackRating;  //a shorter name than this ?
float mAttackSpeed;
float mRange;
bool mCanCut;  //always true for CCutting - false for others
bool mSingleHand  //true for single handedness false for 2 handed weapons
bool mShort;  // Short or Long weapon - originally had this
                     // in CSword better here so can apply to clubs etc
};
class CCutting: public CWeapon {
float mCutEffective;   //cutting effectiveness - how well it cuts

};
class CSword: public CCutting {
string mMaterial;  //assume this only affects swords should have enum of materials types

};
class CAxe: public CCutting
class CDagger: public CCutting;

//so far don't need individual sword types, as long as all the values can be determined 
//from the values in the base class variables

class CStaff: public CWeapon;
class CClub: public CWeapon;
class CBow: public CWeapon {
float mReloadTime;
unsigned mSpareArrows
};
class CNormalBow: public CBow;
class CCrossBow: public CBow;



A lot of this is a bit of a guess at this stage.

With your weapon attribute list, post it like this:

Staff, attribute1, attribute2, attribute3 ... (say what the attributes are)

Normally you wouldn't have as got as far as mapping out the classes yet, because we don't have all the info - remember this could all change a bit yet.

Edit:

Notice how I added the CCutting class to allow for the mCutEffective and mCanCut variables? This why you need to know all the attributes before you design classes.
Last edited on
Ok - Thanks for this outline. Had a lot of work the past few days. I am going to step away from the compiler and brain-storm the weapons.

I play a lot of RPG's but seem to forget the little things. There are requirements to equip weapons. Can't allow a level one to equip a higher level weapon. I tend to just want to write code before I know whats going on. So I will revamp the weapon data base and post soon.
There are requirements to equip weapons. Can't allow a level one to equip a higher level weapon.


OK, sounds like an interaction between the player and the weapons classes, We will have to think about how that is going to work exactly.

I tend to just want to write code before I know whats going on. So I will revamp the weapon data base and post soon.


I guess you mean the list of attributes.

I have a lot of driving to do starting Monday. I have a 3000km drive to collect some of my gear, then 3000km back again. I will still be on line in the evenings, though I might not have time to do detailed posts.
I have completed the one hand weapons. Below is a link to the photo.
I wrote it in a spread sheet. Uploaded to Picasa.



My brother is being a bum.. ha I will have to do the 2 hand weapons.
Hi I am back.

I started a new thread which I think you should look at:

http://www.cplusplus.com/forum/general/78363/


The original question was about how to avoid using get & set functions, but now it seems I am being advised not to have the inheritance that I proposed earlier in this thread. I am still coming to grips with that advice - and have a few more things to say on that subject.

So the questions I have for you are:

1. What does the Quality attribute mean?
2. Are we going to have a simple model where damage is just subtracted from health, or is it more complicated than that?
3. Can the damage value be calculated from the other attributes eg from length, material, single-handed etc, or are they just going to be set at constant values?
Happy you are back, hope all went well.

1. The quality attribute is durability. The item can break, must be repaired.
2. I would like to include armor for some enemies. A bandit wears armor but an orc doesn't.

3. I think this one will be constant. Different weapons share the same material but do different damage. I would like to add unique weapons which will roll random properties within a defined limit.

I did a lot of thinking in your absence. More so of design than code. I did experiment with some game loops for maps. I think I would like to make it a little more than just text. Below is an image I drew in paint.

(The "key" is just for reference)

I was thinking of having instance battles. Similar to Final Fantasy. ( If you are familiar with that game ) The battles would start once you or an enemy meet in the same square. Then the game map would clear and it would be text combat.

I know we are working on weapons but I'm not sure if these ideas will affect the design. Running it by you.

I read your new post. Informative. Had to read it a few times to understand some parts of it better.

Sorry the response is brief. Fall semester started.
Last edited on
OK, for the answers you provided:

1. So that means that Quality goes into the Weapon class and will be inherited by all the derived classes.
2. It's not a simplistic model. So the Weapon.Use() will be a virtual function so that we can handle different behaviours in different situations.
3. OK, we could do this several ways: have a constant value hard coded for the damage; calc the damage given the other attributes (probably in the constructor); calc the damage as a result of the current situation (eg armour); or a combination of these ( this possibly the most flexible solution).

I know we are working on weapons but I'm not sure if these ideas will affect the design. Running it by you.


As I said in my other thread, a good design means that one can add things later without there being to much extra work or drama (redesign, recoding). So it doesn't hurt to imagine all kinds of extra features and abilities for the game.

I also said it's a good idea, to start simple, get things working, then add extra things.

This brings me to some other aspects of programming, some ideas to think about. This is a rather simple version - reality can be much more complicated.

Project Life Cycle:

1. Project requirements, scope,time deadlines.
2. Design.
3. Coding.
4. Testing.

For hobby coders (like me) number 1 above seems open ended. However one can say to oneself - "I will do these 5 simple things first, get them working and tested, then move onto the next group of tasks."

For design there are lots of different types. Class inheritance, interaction, interfaces, Data format, visual, and the code itself are just a few.

Testing is a very important part of any project - you have to prove that it works properly. So say you create lots of different weapons, it's a simple model where damage is subtracted from health. You should then write code to test all the different weapons against one Enemy to start with, then against lots of Enemies, and against more complex situations eg Enemy with armour.

Testing can be really involved, you have to try to expose all of the situations where something might fail, or give the wrong result. If you miss something, then your app has a bug. The more of these, the lesser the quality, and the lesser the popularity.

It is also very important to not leave the testing until it's too late, or becomes an inordinate amount of complex work. It is much better to do small amounts of relatively simple testing as you go along. So don't write 2000 lines of code, then start testing!!

With your project, at this stage it is important to sort out the relationship between a Player (Hero), an Enemy and a Weapon. First we get the interface between these set up. These will be functions that communicate between the objects. Once that is coded, I would test with one Player, one Enemy, and one Weapon. Then you could add some weapons. If the the interface is good, then there would be no reason why you couldn't add all the weapons. Then test, still the simple model - damage is subtracted from health.

I hope this has given you a bit to think on - probably lots going on with start of semester at your end. Good Luck.
Thank you for this good stuff.

But I am still not sure how I should load the weapon data. I could hard code all the data but would be a hassle to update once I start testing and want to tweak game balance so weapons are not too powerful or under powered.

If I use a text file I'm not sure how to pass specific lines within the text file when I need it. I am going to start a new post with this issue.
But I am still not sure how I should load the weapon data.


You need to create the classes and their member functions first. And Test the interface with one object first, so the hard coding won't matter at this stage.

I am thinking that it might be possible to come up with some sort of formula that calcs damage based on the other attributes Material, length etc.

If you do finish up loading data from files it's not that hard, it's just there are other things to do first.

Edit:

I need to head out now, back tmrw.

P.S In your other thread, you should point out that this one is related.
Last edited on
Just some other ideas.

From an organisation point of view, if you are going to have lots of weapons, enemies etc, which means lots of classes and files, you should figure out how to put all of your weapons .cpp & .h files into a directory called weapons under the usual source directory. Do the same for enemies etc. This will just avoid having dozens or 100's of files in 1 directory. The tricky part might be getting the IDE to compile them all without linker errors. I try in general to have my directory / source files arranged in a similar fashion to the inheritance tree within reason, determined by how many files there are.

With your work flow at the moment, I would create the CWeapon, CCutting, and CSword classes as per my outline above, in that order because they inherit. Hard code the damage and health values for now. Create the CActor, CPlayer, CEnemy classes. Create a CTroll class say which inherits from CEnemy, so that you can create an object of this type.

For the CWeapons class create a virtual function called Use which at this stage just returns the damage value .

For the CEnemy class create a virtual function called RecvAttack, which subtracts damage from health.

For the CPlayer class create a virtual function Attack which is called with pointers to CEnemy class, and CWeapons class. It will use the pointers to call the Use and RecvAttack functions which retrieve the damage value and subtracts it from the health value.

In Main, create objects of type CPlayer, CSword, and CTroll , and test the interface described.
A quick question. Why doesn't the "CActor" just be called character which contains the receive and attack function? The "CActor" class seems useless.

I was thinking a character class could hold pointers in the constructor which point to the players current health and enemies health. One class to hold variables and actions for the characters.
Ok, first the name - CCharacter would have been OK, but it is too long. CActor has the same meaning.

You are right the Attack & RecvAttack functions could go into CActor class, so they will be inherited to the CPlayer & CEnemy and derivatives of those.

I would overload the Attack function like this:

1
2
3

virtual bool Attack(CPlayer *, CEnemy *, CWeapon *); //Player vs Enemy
virtual bool Attack(CEnemy *, CPlayer *,  CWeapon *); // Enemy vs Player  


A pointer to the object doing the attacking is included, so that the object being attacked knows who to fight back to.

If there was just one Attack function with CActor and CWeapon as the arguments, then this could mean a free for all - Player vs Player and Enemy vs Enemy. I am guessing this is not what you want.

Looks as though I need to straighten out your terminology a little bit:

could hold pointers in the constructor which point to the players current health and enemies health.


Constructors are functions (you can have more than one in a class) that initialise member variables, they don't hold anything.

One class to hold variables and actions for the characters.


We need to have the derived classes so that we can create objects from them - having just CActor class would mean that we couldn't create a Hero (CPlayer) or say a Troll (CTroll). Well you could, but we want them to be different types of objects, so they can have different behaviours.

The CActor class is there to hold info that is common to all the Players & Enemies. An example of this is the m_Health member variable. This class could also hold other common actions (member functions) such as Move() .

Just going back to the Quality attribute - could we come up with a better name than that? Some thing along the lines of Newness or Used etc that might have a more obvious meaning.

With that, I would have some more variables. A step value which is subtracted each time the weapon is used. A minimum value such BrokenVal which means that when the UsedVal gets down to this number the weapon is broken. Also a bool IsBroken that is set to true when UsedVal == BrokenVal. These could all go into the Weapons class. You could have a virtual function, so that each weapon could have it's own step value and BrokenVal.

A final thing about types - I like to include the stdint.h file :

#include <stdint.h>

so that I can have types like uint16_t instead of unsigned short . In my mind this is easier for several reasons:

You know what you are getting - uint16_t is a 16 bit unsigned int, depending on the system short might be the same as int for example;

It is more concise when it comes to using qualifier like const etc think of const unsigned long long as opposed to const uint64_t;

int8_t is less confusing than char for an 8 bit number.

I would be inclined to make damage & health uint16_t. It would be best to have them the same, and 8 bits is only goes up to 255 which might be a bit restrictive.

I am going to have a go at creating this code myself, so we can compare.

Also, about your question of using maps. This becomes tricky because it varies so much from system to system. You use Visual Studio, right? Hmmm, I don't know anything about Windows Programming, which is what you would have to do to have visual maps. There all kinds of different systems out there, but unless we had the same, that's probably where my help to you would end.

Now you can do whatever you like, and you could most likely find someone to help you that is much more knowledgeable than me, for whichever system / technology you choose.

I use Qt Creator, although I confess to not knowing much about that either, but it does have these advantages:

Available on Windows, Linux and Mac;

Can cross compile to other OS. I am not sure but I have heard it might be a bit tricky to have 1 code set that can be compiled to work on Windows or Linux - I might be wrong there - great if I was!! ;

Can make Windows apps with windows code;

Can make apps for phones etc - you could make one for your android tablet;

Has access to all kinds of technologies Animation, Mobile, graphics, networking, OpenGL, plus heaps more. There are hundreds of example demo apps that come with it.

Now I should point out that learning a GUI system is generally a lot more involved than merely learning a programming language.

One other thing - there is a beginners problem on this site that is to make a Dungeon Crawler program. Use the search button at the top of this page to find the question and also various peoples attempts at it - Some better than others I suspect.

There you go - a whole lot more to think about !!!


I love being able to come onto this forum and learn. I come home from class to another class, your my professor of C++ programming.

Thanks!


**Edit
Having difficulties with the attack function.
When I try to use the function in main I get errors.
Troll.attack(CEnemy, CPlayer, CWeapon);
Error:
expected primary-expression before ',' token
expected primary-expression before ',' token
expected primary-expression before ')' token


How would I use the extra pointer in the attack function to make the defender aware of who is attacking them? I am obviously going to return something but would it just be a pointer to the attack function of the attacker?
Last edited on
Here is my code:

main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>

#include "CPlayer.h"
#include "CSword.h"
#include "CTroll.h"

int main()
{
    CPlayer Hero;
    CTroll Troll;
    CSword Sword;
    CEnemy Enemy;
    CWeapon Weapon;

    bool run = false;

    std :: cout << "\n\n\n\n\n\n\n\nYou come across a troll" << std :: endl;
    std :: cin.ignore();

    while ( !run )
    {
    std :: cout << "The troll attacks you" << std :: endl;

    Enemy.attack(&Enemy, &Hero, &Weapon);

    std :: cout << "Enemy HP: " << Enemy.displayHP() << std :: endl;
    std :: cout << "Hero HP: " << Hero.displayHP() << std :: endl;
    std :: cin.ignore();
    if (Hero.displayHP() > 0)
    {
    std :: cout << "You attack the troll" << std :: endl;
    Hero.attack(&Enemy, &Weapon);
    std :: cout << "Enemy HP: " << Enemy.displayHP() << std :: endl;
    std :: cout << "Hero HP: " << Hero.displayHP() << std :: endl;
    std :: cin.ignore();

    Troll.Rage();
    }

    if ( Enemy.displayHP() <= 0 || Hero.displayHP() <= 0)
    {
        if (Enemy.displayHP() == 0)
        {
            std :: cout << "You killed the beast." << std :: endl;
            std :: cin.ignore();
        }
        else
        {
            std :: cout << "You died." << std :: endl;
        std :: cin.ignore();
        }

        return true;
    }
    }
}

CWeapon.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef CWEAPON_H
#define CWEAPON_H

#include <stdint.h>

class CWeapon
{
    public:
        CWeapon();
        virtual uint16_t Use();
        void SpecialAttack(uint16_t x);
    protected:
    uint16_t mDamage;
    private:
};

#endif // CWEAPON_H 

CWeapon.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "CWeapon.h"

#include <iostream>

CWeapon::CWeapon()
{
    std :: cout << "Weapons Constructor" << std :: endl;
    mDamage = 4;
    std :: cout << mDamage << std :: endl;
}

uint16_t CWeapon::Use()
{
    return mDamage;
}

void CWeapon::SpecialAttack(uint16_t x)
{
    x = mDamage;
}

CSword.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef CSWORD_H
#define CSWORD_H

#include "CWeapon.h"

enum
{
    CUTTING_FALSE,
    CUTTING_TRUE
};

class CSword : public CWeapon
{
    public:
        CSword();
    protected:
        bool cut();
        uint16_t mCutDamage;
        uint16_t mCutTime;
    private:
};

#endif // CSWORD_H 

CSword.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "CSword.h"

#include <stdlib.h>
#include <time.h>

CSword::CSword()
{
    mDamage = 4;
}

bool CSword::cut()
{
    if (CUTTING_FALSE) //Messing around with ideas
    {
        srand(time(NULL));

        mCutDamage = rand() % 6 + 1;
        mCutTime = rand() % 4 + 1; //Not sure how to make these work
    }                              //If Enemy hit target will bleed 1-6 damage for 1-4 turns.
    else return false;
}

CActor.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef CACTOR_H
#define CACTOR_H

#include "CWeapon.h"

class CPlayer;
class CEnemy;

#include <stdint.h>

class CActor
{
    public:
        CActor();
        uint16_t displayHP();
        virtual bool attack(CEnemy *pMyPlayer, CPlayer *pMyEnemy, CWeapon *pMyWeapon);
        virtual void attack(CEnemy *pMyEnemy, CWeapon *pMyWeapon);
    protected:
        uint16_t mHealth;
    private:
};

#endif // CACTOR_H 

CActor.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "CActor.h"

#include "CEnemy.h"
#include "CPlayer.h"

#include <iostream>

CActor::CActor()
{
    std :: cout << "CActor Constuctor" << std :: endl;
}
uint16_t CActor::displayHP()
{
    return mHealth;
}
void CActor::attack(CEnemy *pMyEnemy, CWeapon *pMyWeapon) // Didn't see why the Hero needs to know who attacked them.
{
    pMyEnemy->recvAttack(pMyWeapon);  //Player vs Enemy
}
bool CActor::attack(CEnemy *pMyPlayer, CPlayer *pMyEnemy, CWeapon *pMyWeapon)
{
    pMyEnemy->recvAttack(pMyWeapon);  //Enemy vs Player
    return true;
}

CPlayer.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef CPLAYER_H
#define CPLAYER_H

#include "CActor.h"

class CPlayer : public CActor
{
    public:
        CPlayer();
        virtual void recvAttack(CWeapon *pMyWeapon);
    protected:
    private:
};

#endif // CPLAYER_H 

CPlayer.cpp:
1
2
3
4
5
6
7
8
9
10
11
#include "CPlayer.h"

CPlayer::CPlayer()
{
    mHealth = 100;
}

void CPlayer::recvAttack(CWeapon *pMyWeapon)
{
    mHealth -= pMyWeapon->Use();
}

CEnemy.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef CENEMY_H
#define CENEMY_H

#include "CActor.h"

class CEnemy : public CActor
{
    public:
        CEnemy();
        virtual void recvAttack(CWeapon *pMyWeapon);
    protected:
    private:
};

#endif // CENEMY_H 

CEnemy.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "CEnemy.h"

#include <iostream>

CEnemy::CEnemy()
{
    mHealth = 100;
}

void CEnemy::recvAttack(CWeapon *pMyWeapon)
{
    mHealth -= pMyWeapon->Use();
}

CTroll.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef CTROLL_H
#define CTROLL_H

#include "CEnemy.h"
#include "CWeapon.h"

class CTroll : CEnemy
{
    public:
        CTroll();
        void Rage();
    protected:
    private:
        CWeapon *pMyWeapon;
};

#endif // CTROLL_H 

CTroll.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "CTroll.h"

#include <iostream>

CTroll::CTroll()
{
    std :: cout << "Ugly Troll" << std :: endl;
}

void CTroll::Rage()
{
    std :: cout << "The troll is raged!" << std :: endl; //Testing a special property.
    pMyWeapon->SpecialAttack(30);                       //When the Troll's HP falls below a certain
}                                                       //point the Troll enters into a rage state which increases damage 
Gidday,

Have had a few things going on at my end, so haven't replied for a while. I am in the process of writing the code myself - have had some new thoughts about that. I thought I would post some comments about your code to give you some ideas to think on / implement, before I post my version of the code.

I come home from class to another class, your my professor of C++ programming.


Now, now, steady ....

I think that's very unfair towards someone who is a C++ professor !! I think that it is important in life to have a realistic view of where you stand in relation to others, whether it be for work, sport, or hobbies such as C++. Knowledge & experience is a relative thing - It's fair to say that you have lots to learn yet, and I know quite a bit more than you, but there a lots of others who know much much more than me.

Another bit of good advice is, to not let this work on the game project be at the expense of your study.

Having difficulties with the attack function.
When I try to use the function in main I get errors.
Troll.attack(CEnemy, CPlayer, CWeapon);


I don't think you realise the difference between an object and a class. An object is an instance of a class. You need to create an object from a class before you can access it's variable or functions. Think of a class as being a specification for an actual object. It's like drawings for a house - you don't live in the drawings, you build the house from the drawings. You also don't paint the drawings - you paint the house.

So you need to call the attack function with objects:

 
Troll.Attack(Troll, Hero, TrollSword);


A couple of problems with this:
1
2
virtual bool attack(CEnemy *pMyPlayer, CPlayer *pMyEnemy, CWeapon *pMyWeapon);
        virtual void attack(CEnemy *pMyEnemy, CWeapon *pMyWeapon);


First a minor one - names should start with a capital letter. If the name involves more than one word, then capitalise each word as in "DoSomethingWithThis".

There is some confusion about types in the function calls. Players are on our side and include Hero, Heroine, General, Soldier etc as examples. Enemies are on the other side and include Trolls, Orcs etc.

I was thinking the parameters for the functions would be:
1
2
3
4
5
6
7
8
9
virtual bool Attack(CEnemy *pAEnemy, CPlayer *pAPlayer, CWeapon *pAWeapon); {    //Enemy vs Player

return true;    //so we can return some sort of status for the attack
}
virtual bool Attack(CPlayer *pAPlayer, CEnemy *pAEnemy, CWeapon *pAWeapon);{    //Player vs Enemy

return true; //so we can return some sort of status for the attack
}


The attacking object is included in the function call so that the object being attacked can mount a counter attack.

This is one of the things I have been thinking about. I might have one Attack function in CActor (like Disch was saying), but then I would need to use RTTI (Run Time Type Identification) to see if it is a valid attack - that is, not Player v Player or Enemy vs Enemy. It would also be good that a Player can't call an attack on themselves by an Enemy. I tried to have separate Attack functions for Players & Enemy, but got into strife with circular includes. Using forward declarations of the classes didn't help.

Another thing I was going to do, was to create a AppGame object, to put Test & LoadData functions in. This will lead to minimum code in main.

Now for virtual functions - A virtual function means that the function may be redefined in a derived class. This is very handy because we can have a very general one in a base class - just subtracts damage from health say. Then we can have a specialised function (with the same name) in a derived class. So for the Weapon.Use() virtual function, we can have swords behave in different ways to bows, clubs, daggers etc. The same goes for the virtual Attack function.

If a virtual function is not redefined in a derived class, it will use the next one it finds going up the inheritance tree. So for the simple model where damage is subtracted form health, we define one virtual function in the base class, because it is not redefined in any derived class - it will be used for all the objects.

Ok, now to the rage state.

When the Troll's HP falls below a certain point


So this implies the need to keep track of the troll's health (In the RecvAttack function) and call the rage function when the health drops below a certain value. So the Rage function should be protected rather than public, and be called from the RecvAttack function. When the Rage function is public, any object can call it, at any time - Not the behaviour you want I think.

1
2
3
4
void CWeapon::SpecialAttack(uint16_t x)
{
    x = mDamage;
}


This is backwards - should be mDamage = x;

This code
1
2
3
4
5
enum
{
    CUTTING_FALSE,
    CUTTING_TRUE
};


is exactly the same as bool CanCut which I had in the Weapon class.


As for a Fight sequence - I thought of this:

1. The Player & Enemy each have a random number generated which is the number of blows they can do in a fight.
2. A random number that represents the time between blows, for each blow. Use the sleep function to achieve this.
3. Create different actions (attacking & defensive) for the weapons.
4. A random number to decide which action is carried out for each blow.
5. Logic to decide whether an action is effective or how effective.

This all makes it a game of chance ( no decision making by the user). It also add a whole lot of complexity, have to be careful not to get too far ahead of ourselves.

Any way I will have another go at completing my version of the code


Pages: 1234