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!