Hey everyone,

I'm still tweaking the attributes but here is what I've got:

All races: dmg = rand, where 0 < r < str

Human
25% chance of blocking
5% chance of double attack (attack x2)
10% chance of dmg+60

Elf
5% chance of blocking
35% chance of double attack

Balrog
35% chance of attacking twice

Cyberdemon
35% chance of blocking


Balrog and Cyberdemon are inherited from Demon; all objects of class Demon have a 10% chance of inflicting dmg+50.

My code (very few comments so far, sorry):

#pragma once

#include <string>
#include <iostream>
using namespace std ;

class Creature
{
public:
    Creature ( ) ;
    Creature ( int newStr , int newHp ) ;
    int getDmg ( ) ;
    void takeHit ( int dmg ) ;
//protected:
    int str ;
    int hp ;
    string getSepcies ( ) ;
} ;





#include "creature.h"

Creature::Creature ( ) : str ( 15 ) , hp ( 250 )
{ }

Creature::Creature ( int newStr , int newHp ) : str ( newStr ) , hp ( newHp )
{ }

int Creature::getDmg ( )
{
    int damage ;

    //All creatures inflict damage which is a random number up to their strength
    damage = ( rand() % str ) + 1 ;
    //cout << "Default Creature attacks for " << damage << " points!" << endl ;

    return damage ;
}

void Creature::takeHit ( int dmg )
{
    hp -= dmg ;
}





#pragma once
#include "creature.h"

class Demon : public Creature
{
public:
    Demon ( ) ;
    Demon ( int newStr , int newHp ) ;

    bool isPlus50 ( ) ;
} ;





#include "demon.h"

Demon::Demon ( ) : Creature ( )
{ }

Demon::Demon ( int newStr , int newHp ) : Creature ( newStr , newHp )
{ }

bool Demon::isPlus50 ( )
{
    if ( ( rand() % 100 ) < 10 )
        return true ;
    else
        return false ;
}





#include "demon.h"

class Balrog : public Demon
{
public:
    Balrog ( ) ;
    Balrog ( int newStr , int newHp ) ;

    int getDmg ( ) ;
    bool twoAttack ( ) ;
} ;





#include "balrog.h"

Balrog::Balrog ( ) : Demon ( )
{ }

Balrog::Balrog ( int newStr , int newHp ) : Demon ( newStr , newHp )
{ }

int Balrog::getDmg ( )
{
    int damage = Creature::getDmg ( ) ;
    bool plus50 = isPlus50 ( ) ;
    bool twoA = twoAttack ( ) ;

    if ( twoA == true )
    {
        damage += Creature::getDmg ( ) ;
        cout << "Balrog Sneak Attack for " << damage << " points!!" << endl ;
    }

    if ( plus50 == true )
    {
        cout << "Balrog Damage Boost +50!!" << endl ;
        damage += 50 ;
    }

    return damage ;
}

bool Balrog::twoAttack ( )
{
    if ( ( rand() % 100 ) < 35 )
        return true ;
    else
        return false ;
}





#include "demon.h"

class Cyberdemon : public Demon
{
public:
    Cyberdemon ( ) ;
    Cyberdemon ( int newStr , int newHp ) ;

    int getDmg ( ) ;
    bool block ( ) ;
} ;





#include "cyberdemon.h"

Cyberdemon::Cyberdemon ( ) : Demon ( )
{ }

Cyberdemon::Cyberdemon ( int newStr , int newHp ) : Demon ( newStr , newHp )
{ }

int Cyberdemon::getDmg ( )
{
    int damage = Creature::getDmg ( ) ;
    bool plus50 = isPlus50 ( ) ;

    if ( plus50 == true )
    {
        cout << "Cyberdemon Damage Boost!!" << endl ;
        damage += 50 ;
    }

    return damage ;
}





#include "creature.h"

class Hume : public Creature
{
public:
    Hume ( ) ;
    Hume ( int newStr , int newHp ) ;

    int getDmg ( ) ;
    bool block ( ) ;
    bool dblAttack ( ) ;
    bool dmgPlusSixty ( ) ;
} ;





#include "hume.h"

Hume::Hume ( ) : Creature ( )
{ }

Hume::Hume ( int newStr , int newHp ) : Creature ( newStr , newHp )
{ }

int Hume::getDmg ( )
{
    int damage = Creature::getDmg ( ) ;
    bool dbl = dblAttack ( ) ;
    bool dmg = dmgPlusSixty ( ) ;
    if ( dbl == true )
    {
        cout << "Hume Damage doubled!!" << endl ;
        damage *= 2 ;
    }
    
    if ( dmg == true )
    {
        cout << "Hume Damage boost +60!!" << endl ;
        damage += 60 ;
    }
    
//    cout << "Hume attacks for " << damage << " points!" << endl ;

    return damage ;
}





bool Hume::block ( )
{
    if ( ( rand() % 100 ) < 25 )
        return true ;
    else
        return false ;
}

bool Hume::dblAttack ( )
{
    if ( ( rand() % 100 ) < 5 )
        return true ;
    else
        return false ;
}

bool Hume::dmgPlusSixty ( )
{
    if ( ( rand() % 100 ) < 10 )
        return true ;
    else
        return false ;
}





#include "creature.h"

class Elf : public Creature
{
public:
    Elf ( ) ;
    Elf ( int newStr , int newHp ) ;

    int getDmg ( ) ;
    bool block ( ) ;
    bool dblAttack ( ) ;
} ;





#include "elf.h"

Elf::Elf ( ) : Creature ( )
{ }

Elf::Elf ( int newStr , int newHp ) : Creature ( newStr , newHp )
{ }

int Elf::getDmg ( )
{
    int damage = Creature::getDmg ( ) ;
    bool dbl = dblAttack ( ) ;

    if ( dbl == true )
    {
        cout << "Elf Damage doubled!!" << endl ;
        damage *= 2 ;
    }

    return damage ;
}

bool Elf::block ( )
{
    if ( ( rand() % 100 ) < 5 )
        return true ;
    else
        return false ;
}

bool Elf::dblAttack ( )
{
    if ( ( rand() % 100 ) < 35 )
        return true ;
    else
        return false ;
}






#include "hume.h"
#include "elf.h"
#include "balrog.h"
#include "cyberdemon.h"

int main ( )
{
    Hume hume ( 25 , 300 ) ;
    Elf elf ( 25 , 300 ) ;
    Balrog balrog ( 25 , 300 ) ;
    Cyberdemon cyberD ( 25 , 300 ) ;

    int i = 1 ;        //number of rounds

    int elfDmg ;
    int humeDmg ;
    int balrogDmg ;
    int cyberDdmg ;

    while ( hume.hp > 0 && elf.hp > 0 )
    {
        cout << "*************** Round " << i << "  ***********************\n" ;
        elfDmg = elf.getDmg ( ) ;
        humeDmg = hume.getDmg ( ) ;

        if ( !( hume.block ( ) ) )
        {
            hume.hp -= elfDmg ;
            cout << "Elf hits Hume for " << elfDmg << " points!" << endl ;
        }
        else
            cout << "Hume blocks Elf's attack!" << endl ;

        if ( ! ( elf.block ( ) ) )
        {
            elf.hp -= humeDmg ;
            cout << "Hume hits Elf for " << humeDmg << " points!" << endl ;
        }
        else
            cout << "Elf block Hume's attack!" << endl ;

        cout << "\nEnd of Round Stats:  " << endl ;
        cout << "Hume Health:  " << hume.hp << endl ;
        cout << "Elf Health:  " << elf.hp << endl ;
        cout << "\n\n\n" ;

        i++ ;

        cout << "\nNext Round:  -Enter-\n\n\n\n" ;
        cin.get() ;
    }

    if ( hume.hp <= 0 )
        cout << "The Elf has won!\n" ;
    else
        cout << "The Hume has won!\n" ;

    hume.hp = 300 ;
    elf.hp = 300 ;

    i = 1 ;

    while ( hume.hp > 0 && balrog.hp > 0 )
    {
        cout << "*************** Round " << i << "  ***********************\n" ;
        balrogDmg = balrog.getDmg ( ) ;
        humeDmg = hume.getDmg ( ) ;

        if ( !( hume.block ( ) ) )
        {
            hume.hp -= balrogDmg ;
            cout << "Balrog hits Hume for " << balrogDmg << " points!" << endl ;
        }
        else
            cout << "Hume blocks Balrog's attack!" << endl ;

        balrog.hp -= humeDmg ;
        cout << "Hume hits Balrog for " << humeDmg << " points!" << endl ;

        cout << "\nEnd of Round Stats:  " << endl ;
        cout << "Hume Health:  " << hume.hp << endl ;
        cout << "Balrog Health:  " << balrog.hp << endl ;
    
        i++ ;

        cout << "\nNext Round:  -Enter-\n\n\n\n" ;
        cin.get() ;
    }

    if ( hume.hp <= 0 )
        cout << "The Balrog has won!\n" ;
    else
        cout << "The Hume has won!\n" ;

    balrog.hp = 300 ;
    elf.hp = 300 ;


    return 0 ;
}

main() only tests a fight between Hume vs Elf and Hume vs Balrog for now (Hume is way overpowerful, oops).

My problem is this:
Every run of the program produces the same results. Same damage, same blocks, same double attacks, same winners, everything. I thought rand() was supposed to randomize the outcome in this situation? What am I doing wrong?

Thanks for any help!

Recommended Answers

All 6 Replies

My code (very few comments so far, sorry):

Why do people apologise for doing bad things rather than just doing what they know they should? :icon_confused:

My problem is this:
Every run of the program produces the same results. Same damage, same blocks, same double attacks, same winners, everything. I thought rand() was supposed to randomize the outcome in this situation? What am I doing wrong?

Not wanting to waste time looking through 400+ lines of code for that 1 wayward statement, all I can suggest without you being more specific is that srand() should only be called once at the beginning of the program.

If this doesn't help, give us a clue as to what to look at.

Why do people apologise for doing bad things rather than just doing what they know they should? :icon_confused:

I clearly said "so far", meaning I was going to get around to it, but didn't see it as a priority for the moment.

Not wanting to waste time looking through 400+ lines of code for that 1 wayward statement, all I can suggest without you being more specific is that srand() should only be called once at the beginning of the program.

If this doesn't help, give us a clue as to what to look at.

If you would have looked through the code, you would see that doing rand() once is not an opption since it has to be nested in a loop to be effective.

It didn't help; and if you're going to be condescending in every post, I'd rather you not waste your time trying to help.

I clearly said "so far", meaning I was going to get around to it, but didn't see it as a priority for the moment.

So why point it out at all?

If you would have looked through the code, you would see that doing rand() once is not an opption since it has to be nested in a loop to be effective.

400+ lines. That's why I didn't. Just as you didn't comment. It takes time.

And reread my post. I mentioned srand() , not rand() .

It didn't help; and if you're going to be condescending in every post, I'd rather you not waste your time trying to help.

If you think I'm being condescending, sorry. I'm explaining why I (and most others) won't look at 400 line programs. Give us something to go on. Where do you think the problem is. Or as Salem always mentions, post a small program that shows the error or problem.

Sorry if I sounded rude; honestly, I was trying to. It's just getting late, and I'm tired. My appologies.

I'm not sure where the problem is. I just started using rand() and am not sure what srand() is.

Maybe a more descriptive explanation of the problem I'm having:

When I run the program it's supposed to have random effects on the character's attributes (stated at the top of the first post). The problem is, every time I run the program, I get the same exact results. Nothing changes. I wanted the outcome (winner) to be by chance every time I run the program, but it's like rand() is giving me the same numbers with every compilation.

Sorry if I sounded rude; honestly, I was trying to. It's just getting late, and I'm tired. My appologies.

OK

I'm not sure where the problem is. I just started using rand() and am not sure what srand() is.

Then look it up! :icon_rolleyes: You know by now we don't want to spoon-feed people.

Maybe a more descriptive explanation of the problem I'm having:

When I run the program it's supposed to have random effects on the character's attributes (stated at the top of the first post). The problem is, every time I run the program, I get the same exact results. Nothing changes. I wanted the outcome (winner) to be by chance every time I run the program, but it's like rand() is giving me the same numbers with every compilation.

Yep.... Look it up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.