In: Computer Science
C++ - Almost done, I have a problem with read access violation error. This is the link to my assignment first part: https://pastebin.com/yvcvdqLY
second part: https://pastebin.com/vY7MK1Bf,
My header file: https://pastebin.com/pcJnctgu
My .cpp file: https://pastebin.com/9jAfP9u8
My source file: https://pastebin.com/kFsidY2k
Input:
main.cpp:
#include "GorgVsBoov.h"
Boov::Boov(std::string Name, int Health)
{
name = Name;
health = Health;
}
std::string Boov::GetName()
{
return name;
}
int Boov::GetHealth()
{
return health;
}
bool Boov::IsDefeated()
{
if (health <= 0)
return true;
else
return false;
}
void Boov::Print()
{
std::cout << "Boov's name: " << name << std::endl;
std::cout << "Boov's health: " << health << std::endl;
if (Boov::IsDefeated() == 0)
std::cout << "Boov isn't defeated yet!" << std::endl;
else
std::cout << "Boov is defeated!" << std::endl;
}
void Boov::GetsAttacked(Boov& Target, int attack, int damage)
{
if (attack > 1)
Target.health -= damage;
}
Gorg::Gorg(std::string Name, int Health)
{
name = Name;
health = Health;
}
std::string Gorg::GetName()
{
return name;
}
int Gorg::GetHealth()
{
return health;
}
bool Gorg::IsDefeated()
{
if (health <= 0)
return true;
else
return false;
}
void Gorg::Print()
{
std::cout << "Gorg's name: " << name << std::endl;
std::cout << "Gorg's health: " << health << std::endl;
if (Gorg::IsDefeated() == 0)
std::cout << "Gorg isn't defeated yet!" << std::endl;
else
std::cout << "Gorg is defeated!" << std::endl;
}
void Gorg::GetsAttacked(Gorg& Target, int attack, int damage)
{
if (attack > 1)
Target.health -= damage;
}
int main()
{
srand(static_cast<unsigned int>(time(NULL)));
double count = 0;
int gorgWins = 0;
int boovWins = 0;
int size = 100000;
std::cout << "Creating one Gorg object to test the Print() function" << std::endl;
Gorg george("George", 10);
george.Print();
std::cout << "\nCreating 100,000 Boov objects to get the average times the Boov can take damage before it is defeated" << std::endl;
Boov **boovArr = new Boov*[size];
Gorg **gorgArr = new Gorg*[size];
for (int i = 0; i < size; ++i)
{
int health = rand() % 5 + 7;
boovArr[i] = new Boov("Oh", health);
gorgArr[i] = new Gorg("George", 10);
// if (gorgArr[i]->IsDefeated() == 1)
// gorgArr[i] = new Gorg("George", 10);
while (boovArr[i]->IsDefeated() == 0)
{
int gAttack = rand() % 3 + 1;
int gDamage = rand() % 4 + 2;
int bAttack = rand() % 2 + 1;
int bDamage = rand() % 2 + 2;
std::cout << bAttack << std::endl;
gorgArr[i]->Print();
std::cout << bDamage << std::endl;
gorgArr[i]->GetsAttacked(*gorgArr[i], bAttack, bDamage);
boovArr[i]->GetsAttacked(*boovArr[i], gAttack, gDamage);
if (boovArr[i]->IsDefeated() == 1)
++gorgWins;
if (gorgArr[i]->IsDefeated() == 1)
++boovWins;
++count;
}
}
std::cout << "100,000 Boov killed after an average of " << count / 100000 << " tries" << std::endl;
std::cout << gorgWins << std::endl;
std::cout << boovWins << std::endl;
system("PAUSE");
return 0;
}
Output:
Explanation: