Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CIS2542 Gorg vs Boov Battle Part 3 Refactoring NOTE: Remember that you are adding on to your previous Boov and Gorg projects. See the previous

CIS2542 Gorg vs Boov Battle Part 3 Refactoring

NOTE: Remember that you are adding on to your previous Boov and Gorg projects. See the previous documentation for Boov and Gorg specific information.

NOTE: You will also be incorporating your code base with my main() function. You may not modify my code in any way (in your official submission anyway). You must change whatever you must on your end to make my file work with yours.

For the final part of this simulation, lets refactor the code base to include the use of inheritance and polymorphism! Think about everything common to both the Boov and Gorg classes. All of those member variables and member functions should be bubbled up into the base class, which I will call class Competitor.

When it comes to member variables, a name and a health value is common to both Boov and Gorg, so they should be moved into the Competitor base class. That means that name and health code should be totally removed from the Boov and Gorg derived classes. That goes for the Get() functions as well.

As for the other member functions, that will be left more up to you. If the same function name is used in both Gorg and Boov classes, the function should also bubble up to the base class. Now, if the bubbled up function shares a name but differs in its functionality, this is the perfect place to set up a virtual function in the base class. For starters, the GetsAttacked() function is a prime example of a virtual function. Same name used by both Boov and Gorg, yet implemented differently by each derived class. But what about IsDefeated() and Print()?

As for Print() You will also need to overload the << operator to complete this assignment properly. The Print() function can be used to do this, but it will be tricky work. I have a link here to get you finished up with that. I have found it is the easiest way to use friend functions with member functions. Its still messy. Make sure to get everything else going before you complete this part.

https://www.learncpp.com/cpp-tutorial/12-10-printing-inherited-classes-using-operator/ //Source.cpp:

// -------------------------------------------------- // THIS CODE FILE IS NOT TO BE MODIFIED IN ANY WAY!!! // --------------------------------------------------

// YOU ARE TO REFACTOR YOUR CODE FROM PREVIOUS HOMEWORKS // SO THAT THIS CODE WORKS PROPERLY WITH YOURS

// Include files #include #include "Boov.h" #include "Gorg.h"

int main() { // Maintain counters for each competitor long long boov_wins = 0; long long gorg_wins = 0;

// Keep the battling to a minimum const long long NUMBER_OF_SIMULATIONS = 1000;

// Lots of battling for (long long i = 1; i <= NUMBER_OF_SIMULATIONS; ++i) { // Set up each object dynamically so virtual functions will work Competitor * oh = new Boov("Oh!"); Competitor * george = new Gorg();

// Gorg gets attacked first // Boov only gets attacked if Gorg is still standing // See if we need to continue while ((oh->IsDefeated() == false) && (george->IsDefeated() == false)) { george->GetsAttacked(); if (george->IsDefeated() == false) { oh->GetsAttacked(); } }

// Only one of the two is defeated... So let's figure out who it is // You'll need this link for programmming the print statements correctly // https://www.learncpp.com/cpp-tutorial/12-10-printing-inherited-classes-using-operator/ if (george->IsDefeated()) { ++boov_wins; std::cout << *oh << " defeats " << *george << std::endl; } else { ++gorg_wins; std::cout << *george << " defeats " << *oh << std::endl; }

// This particular simulation is over so give back the memory // Maybe if we continued development we would add a Refresh() function or something delete oh; delete george; }

// Print out the overall results double gorg = gorg_wins / static_cast(NUMBER_OF_SIMULATIONS) * 100.0; double boov = boov_wins / static_cast(NUMBER_OF_SIMULATIONS) * 100.0; std::cout << "George wins " << gorg << "% of the time" << std::endl; std::cout << "Oh wins " << boov << "% of the time" << std::endl;

// Pause program before closing down system("PAUSE"); return 0; } My code:

//Boov.h: #pragma once

#include #include #include "Competitor.h" using namespace std;

class Boov : public Competitor { public: Boov(std::string, int);

std::string GetName(); int GetHealth(); bool IsDefeated(); virtual void Print() override; virtual void GetsAttacked() override; };

//Boov.cpp:

#include "Boov.h" #include using namespace std; #include

Boov::Boov(const std::string name, int health) :name(name), health(health) {

} std::string Boov::GetName() { return name; }

int Boov::GetHealth() { return health; }

bool Boov::IsDefeated() { return health <= 0; }

void Boov::Print() { std::cout << name << ": " << health; }

void Boov::GetsAttacked() { int randomNo = rand() % 4 + 1;

if (randomNo == 1) { health -= 0; }

else if (randomNo == 2) { health -= 1; }

else { health -= (rand() % 4 + 2); } } //Competitor.h: #pragma once #include class Competitor { public: Competitor(std::string, int); virtual ~Competitor(); std::string GetName(); int GetHealth(); bool IsDefeated(); virtual void GetsAttacked() = 0; virtual void Print() = 0;

protected: std::string name; int health; };

//Competitor.cpp: #include "Competitor.h"

Competitor::Competitor(std::string, int) :name(name), health(health) { }

Competitor::~Competitor() { }

std::string Competitor::GetName() { return name; }

int Competitor::GetHealth() { return health; }

bool Competitor::IsDefeated() { return health <= 0; } //Gorg.h: #pragma once

#include #include using namespace std; class Gorg { public: Gorg(std::string, int);

std::string GetName(); int GetHealth(); bool IsDefeated(); void Print(); void GetsAttacked();

private: std::string name; int health; }; //Gorg.cpp:

#include "Gorg.h" #include using namespace std; #include

Gorg::Gorg(const std::string name, int health) :name(name), health(health) { //Nothing here. }

std::string Gorg::GetName() { return name; }

int Gorg::GetHealth() { return health; }

bool Gorg::IsDefeated() { return health <= 0; }

void Gorg::Print() { std::cout << name << ": " << health; } void Gorg::GetsAttacked() {

int randomNum = rand() % 4 + 1;

if (randomNum == 1) { health -= 0; }

else if (randomNum == 2) { health += 1; }

else { health -= (rand() % 3 + 2); }

} //Answer is: 45.2%

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Databases On The Web Learn To Create Web Pages That Interface With Database Engines

Authors: Robert Papaj, Donald Burleson

11th Edition

1576100995, 978-1576100998

More Books

Students also viewed these Databases questions