|
From: Danny A. <tam...@ms...> - 2002-11-05 00:19:24
|
Hey guys;
this newbie is back at it again trying to figure out what I'm doing below=
is a very long piece of code in 5 different files that I'm trying to com=
pile in one project but for some reason even though I have typed it out o=
f a book and I have played with it and I have even gone to the authors so=
urce code and copied and paste from them and it still doesn't want to wor=
k. I'm beginning to believe it could be a difference in compilers the au=
thors used "CodeWarrior" and the book I'm using is "C++ Programming for t=
he absolute beginner". if anyone can be of help I would appreciate it bec=
ause it would truly teach me something I can't figure out.
Thanks in advance
Danny
//8.4 - Dragon Lord - Mark Lee - Prima Publishing
// Dragon.cpp
#include <string>
#include <ctime>
#include <cstdlib>
#define MAX(a,b) a>b? a:b
using namespace std;
class Dragon
{
private:
int speed;
string name;
int hitPoints;
int armour;
int treasure;
int clawDamage;
int size;
protected:
Dragon(int theSize);
int getArmour() {return armour;}
int& getHitPoints() {return hitPoints;}
int getClawDamage() { return clawDamage;}
int getSize() { return size;}
virtual int attack(int targetArmour, int specialDamage);
public:
virtual int attack(int targetArmour) =3D 0;
virtual void defend(int damage) =3D 0;
int getTreasure() {return treasure;}
virtual string getName() {return name;}
int getSpeed() {return speed;}
bool isAlive() { return hitPoints >0;}
};
Dragon::Dragon(int theSize) : =20
size(theSize)
{
if (size < 1 || size > 4)
size =3D 3;
clawDamage =3D 2 * size;
speed =3D 3 * size;
hitPoints =3D 4 * size;
armour =3D size;
treasure =3D 1000 * size;
srand(time(0));
}
int Dragon::attack(int targetArmour, int specialDamage)
{
int useSpecial =3D rand() % 2; // 0 or 1
int damage;
if (useSpecial) =20
damage =3D specialDamage;
else damage =3D getClawDamage();
return MAX(damage - targetArmour,0);
}
//BlackDragon.cpp
#include "Dragon.cpp"
class BlackDragon : public Dragon
{
private:
int poisonDamage;
public:
BlackDragon(int theSize);
int attack(int targetArmour);
void defend(int damage);
string getName() {return "Black Dragon";}
};
BlackDragon::BlackDragon(int theSize) :
Dragon(theSize)
{
poisonDamage =3D getSize();
}
int BlackDragon::attack(int targetArmour)
{
return Dragon::attack(targetArmour, poisonDamage);
}
void BlackDragon::defend(int damage)
{
getHitPoints() -=3D damage - getArmour();
}
//BlueDragon.cpp
#include "Dragon.cpp"
class BlueDragon : public Dragon
{
private:
int iceDamage;
public:
BlueDragon(int theSize);
int attack(int targetArmour);
void defend(int damage);
string getName() {return "Blue Dragon";}
};
BlueDragon::BlueDragon(int theSize) :
Dragon(theSize)
{
iceDamage =3D 3 * getSize();
}
int BlueDragon::attack(int targetArmour)
{
return Dragon::attack(targetArmour, iceDamage);
}
void BlueDragon::defend(int damage)
{
getHitPoints() -=3D (damage - getArmour())/2;
}
//RedDragon.cpp
#include "Dragon.cpp"
class RedDragon : public Dragon
{
private:
int fireDamage;
public:
RedDragon(int theSize);
int attack(int targetArmour);
void defend(int damage);
string getName() {return "Red Dragon";}
};
RedDragon::RedDragon(int theSize) :
Dragon(theSize)
{
fireDamage =3D 4 * getSize();
}
int RedDragon::attack(int targetArmour)
{
return Dragon::attack(targetArmour, fireDamage);
}
void RedDragon::defend(int damage)
{
getHitPoints() -=3D (damage - getArmour())/3;
}
//DragonLord.cpp
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "Dragon.cpp"
#include "RedDragon.cpp"
#include "BlueDragon.cpp"
#include "BlackDragon.cpp"
using namespace std;
int menuChoice();
int main (void)
{
srand(time(0));
Dragon* dragons[3];
int hp =3D 15;
int armour =3D 2;
int tempArmour;
int tempAttack;
dragons[0] =3D new RedDragon(rand()%4+1);
dragons[1] =3D new BlackDragon(rand()%4+1);
dragons[2] =3D new BlueDragon(rand()%4+1);
Dragon* d =3D dragons[rand()%3];
cout << "Welcome noble knight.\n"
<< "You must save a princess."
<< " She has been captured by a "
<< d->getName() << ".\n"
<< "You must defeat the dragon.\n";
cout << "Your hit points are: " << hp << endl;
while (d->isAlive() && hp>0)
{
int choice =3D menuChoice();
if (choice =3D=3D 3) goto RUN;
else if (choice =3D=3D 1)
{
tempAttack =3D rand()%16+5;
tempArmour =3D armour;
}
else {
tempAttack =3D rand()%11;
tempArmour =3D armour + 4;
}
hp -=3D d->attack(armour);
d->defend(rand()%16-5);
cout << "\nYou deliver a mighty blow and deal "
<< tempAttack << " damage.\n";
cout << "Your hit points are: " << hp;
}
if (d->isAlive())
cout << "\nYou have perished before"
<< " the might of the dragon.\n";
else
cout << "\n\nYou have slain the dragon!"
<< " Congratulations.\n"
<< "The princess is saved.\n";
return 0;
RUN: =20
cout <<"\nYou have fled in cowardice.\n";
return 0;
}
int menuChoice()
{
int choice;
do {
cout << endl
<< "[1]Attack\n"
<< "[2]Defensive Mode\n"
<< "[3]Run Away\n";
cin >> choice;
} while (choice < 1 && choice > 3);
return choice;
} =20
|