Question
Warrior Program C++ //A simple Object Oriented program #include #include #include //Allows for the Sleep function using namespace std; //Declare the class Warrior class Warrior
Warrior Program C++
//A simple Object Oriented program
#include
#include
#include
using namespace std;
//Declare the class Warrior
class Warrior
{
private: //This allows the variables and methods to be accessible by external instructions
char name[10]; //Name of warrior
int power; //Attack power (6-10)
int defense; //Defensive power (absorbs 1-5)
int agility; //Percentage chance to dodge (10 - 30)
int health; //Amount of damage the warrior can take before defeat (15-30)
bool valid; //If warrior is valid
public:
Warrior(char inName[] = "?", int p = 8, int d = 3, int a = 20, int h = 20) //Constructor; note default values but no return type
{
valid = true; //Note the capitalization
strcpy(name,inName);
if ((p < 6)||(p > 10)) valid = false;
if ((d < 1)||(d > 5)) valid = false;
if ((a < 10)||(a > 30)) valid = false;
if ((h < 15)||(h > 30)) valid = false;
if (valid)
{
power = p;
defense = d;
agility = a;
health = h;
}
}
//The init method is gone, replaced by the constructor
int attack();
int takeHit(int);
char* getName();
int isDown();
bool isValid(); //New, check for validity
int getHealth();
int getAgility();
}; //NOTE the semicolon here. It's one of the rare places it follows something other than a simple statement
bool Warrior::isValid() //This is also new
{
return valid;
}
int Warrior::attack()
{
cout << name << " attacks! ";
Sleep(300);
return power;
}
int Warrior::takeHit(int inAttack)
{
int isHit = (rand()%100)+1;
if (isHit > agility)
isHit = 1;
else
isHit = 0;
if (isHit)
{
health -= (inAttack - defense);
if (health < 0)
health = 0;
cout << name << " has been hit and now has " << health << " health remaining. ";
}
else
cout << name << " dodges the attack. ";
Sleep(300);
return 0;
}
char* Warrior::getName()
{
return name;
}
int Warrior::isDown()
{
if (health <= 0)
{
cout << name << " has been defeated! ";
return 1;
}
else
return 0;
}
int Warrior::getHealth()
{
return health;
}
int Warrior::getAgility()
{
return agility;
}
int main()
{
srand((unsigned)time(0)); // "Seed" the random number generator
//This code replaces the initialization section
//Warrior warrior1("David",7,3,20,15);
Warrior warrior1("Default"); //You can now just give the warrior a name and the defaults will be filled in
if (!warrior1.isValid())
{
cout << "Error creating " << warrior1.getName() << ".";
return 0;
}
Warrior warrior2("Julio",6,4,22,15);
if (!warrior2.isValid())
{
cout << "Error creating " << warrior2.getName() << ".";
return 0;
}
//This ends the changes in the constructor-using program
if(warrior1.getAgility() >= warrior2.getAgility())
cout << warrior1.getName() << " is more agile than " << warrior2.getName() << ", and will attack first. ";
else
cout << warrior2.getName() << " is more agile than " << warrior1.getName() << ", and will attack first. ";
while((!warrior1.isDown())&&(!warrior2.isDown()))
{
if(warrior1.getAgility() >= warrior2.getAgility())
{
warrior2.takeHit(warrior1.attack());
if (!warrior2.isDown())
warrior1.takeHit(warrior2.attack());
else
break;
}
else
{
warrior1.takeHit(warrior2.attack());
if (!warrior1.isDown())
warrior2.takeHit(warrior1.attack());
else
break;
}
}
if (warrior1.getHealth())
cout << "The winner is " << warrior1.getName() << "! ";
else
cout << "The winner is " << warrior2.getName() << "! ";
return 0;
}
Use the Warriors program to create a 4-character single-elimination tournament. The program will announce each warrior, and then pair them off. The two winners will fight for the championship, and be declared the ultimate virtual fighting champion.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started