CS 10B Programming Concepts and Methodologies 1

Project 23.1

Note: No documentation (comments) required on this project.

It's messy that in the Balrog class's getDamage() function and the Cyberdemon class's getDamage() function we have to write the name of the species before calling the Demon class's getDamage() function. It would be better if the Demon class's getDamage() function could print the name of the species. Taking this a step further, it would be even better if we didn't have to repeat the cout statement "The <whatever> attacks for ?? points!" in every class's getDamage() function. It would be better if that cout statement could occur just once, in the Creature class's getDamage() function.

  1. In the Creature class's getDamage() function, insert the following statement:

    cout << "The " << getSpecies() << " attacks for " << damage << " points!" << endl;
  2. Delete (or, if you prefer, comment out) the similar cout statements that appear in the getDamage() function of each of the 5 derived classes. (There will be one such cout statement to delete in each of the 5 getDamage() functions.)

  3. Try executing the program. The results won't be quite what we were hoping for.

  4. Now make the getSpecies() function in the Creature class a virtual function, and execute the program again. The results will now be correct.

  5. We can now simplify our derived classes even further. Two of the five derived classes have getDamage() functions that do nothing more than call their parent class's getDamage() function. Delete these two functions. (Don't forget to delete both the prototype in the class declaration and the definition.) We don't need them, because they can just inherit the getDamage() function from the Creature class.

  6. You may have noticed that the Creature class's getSpecies() function never gets called. However, it is absolutely critical that any class that is derived from the Creature class define a getSpecies() function since that function is called from the Creature class's getDamage() function. The best way to implement this is to make the Creature class's getSpecies() function a pure virtual function, so that every class that is derived from the Creature class will be required to implement a getSpecies() function. Make the Creature class's getSpecies() function a pure virtual function.

  7. Comment out the getSpecies() function in the Human class and try compiling the program to see what happens. then uncomment it (i.e., return it to it's previous state).

  8. Make getDamage() a virtual function. This will be important so that in your "battle()" function (see below) you can say "Creature1.getDamage()" and the damage will automatically be calculated for the correct Creature. Note that the parameters for "battle()" will be of type "Creature" and they will need to be pass-by-reference. (You might try making them pass-by-value to see what happens.)

  9. Create a new client program (discard the client program from part 1 of the assignment). Make a function in your client program that is called from main() and has the following prototype:

    void battle(Creature &Creature1, Creature& Creature2);

    (Note, the parameters are pass-by-reference because the Creature class is an abstract class, so objects of type "Creature" cannot be declared.) The function should calculate the damage done by Creature1, subtract that amount from Creature2's hitpoints, and vice versa. (When I say "subtract that amount from Creature2's hitpoints, I mean that the actual hitpoints data member of the Creature2 object will be modified. Also note that this means that both attacks are happening simultaneously; that is, if Creature2 dies because of Creature1's attack, Creature2 still gets a chance to attack back.) At the end of each round, the function should print the total hitpoints remaining for each Creature. If, at the end of a round, both Creatures end up with 0 or fewer hitpoints, then the battle results in a tie. Otherwise, at the end of a round, if one Creature has positive hitpoints but the other does not, the Creature with positive hitpoints is the winner. The function should loop until the battle is over.

    Note that you'll need to reset the creature's hitpoints after each battle.

    To start, you may want to use the following very simple main() function to test your battle() function:

    int main()
    {
        srand(static_cast<unsigned>(time(nullptr)));
    
        Elf e(50,50);
        Balrog b(50,50);
    
        battle(e, b);    
    }
    

    Once your battle() function is working, test it with the given main() below. In this main(), every Creature has a battle with every other Creature. To do this, we first declare four objects, one each of type Elf, Balrog, Human, and Cyberdemon. Then we declare an array of type Creature* with 4 elements, and make each array element point to one of the four objects. (The array elements will need to be pointers-to-Creatures rather than simply Creatures because Creature is an abstract class.) Then we use nested for loops to call battle() with every possible combination of Creatures.

    Use these constant definitions:

    const int NUM_CREATURES = 4;
    const int ELF_DEFAULT_STRENGTH = 24;
    const int BALROG_DEFAULT_STRENGTH = 10;
    const int HUMAN_DEFAULT_STRENGTH = 100;
    const int CYBERDEMON_DEFAULT_STRENGTH = 50;
    const int DEFAULT_HITPOINTS = 50;
    

    Use this main() function:

    int main()
    {
        srand(static_cast<unsigned>(time(nullptr)));
    
        Elf e(ELF_DEFAULT_STRENGTH, DEFAULT_HITPOINTS);
        Balrog b(BALROG_DEFAULT_STRENGTH, DEFAULT_HITPOINTS);
        Human h(HUMAN_DEFAULT_STRENGTH, DEFAULT_HITPOINTS);
        Cyberdemon c(CYBERDEMON_DEFAULT_STRENGTH, DEFAULT_HITPOINTS);
        Creature* creatures[] = {&b, &e, &c, &h};
        
        for (int i = 0; i < NUM_CREATURES; i++) {
            for (int j = i + 1; j < NUM_CREATURES; j++) {
                battle(*creatures[i], *creatures[j]);
            }
        }
    }
    

    Here is a sample output. As you know, you'll have to pay close attention to spacing, etc., in order to get your program to pass the zyBooks tests.

    *****  Battle between the balrog and the elf!!  ******
    
    The balrog attacks for 5 points!
    Balrog speed attack inflicts 4 additional damage points!
    The elf attacks for 23 points!
    The balrog has 27 hitpoints.
    The elf has 41 hitpoints.
    
    The balrog attacks for 8 points!
    Demonic attack inflicts 50 additional damage points!
    Balrog speed attack inflicts 1 additional damage points!
    The elf attacks for 21 points!
    The balrog has 6 hitpoints.
    The elf has -18 hitpoints.
    
    The balrog wins!
    
    
    *****  Battle between the balrog and the cyberdemon!!  ******
    
    The balrog attacks for 2 points!
    Balrog speed attack inflicts 3 additional damage points!
    The cyberdemon attacks for 27 points!
    The balrog has 23 hitpoints.
    The cyberdemon has 45 hitpoints.
    
    The balrog attacks for 2 points!
    Balrog speed attack inflicts 10 additional damage points!
    The cyberdemon attacks for 21 points!
    The balrog has 2 hitpoints.
    The cyberdemon has 33 hitpoints.
    
    The balrog attacks for 9 points!
    Balrog speed attack inflicts 7 additional damage points!
    The cyberdemon attacks for 45 points!
    Demonic attack inflicts 50 additional damage points!
    The balrog has -93 hitpoints.
    The cyberdemon has 17 hitpoints.
    
    The cyberdemon wins!
    
    
    *****  Battle between the balrog and the human!!  ******
    
    The balrog attacks for 1 points!
    Balrog speed attack inflicts 6 additional damage points!
    The human attacks for 17 points!
    The balrog has 33 hitpoints.
    The human has 43 hitpoints.
    
    The balrog attacks for 4 points!
    Balrog speed attack inflicts 6 additional damage points!
    The human attacks for 38 points!
    The balrog has -5 hitpoints.
    The human has 33 hitpoints.
    
    The human wins!
    
    
    *****  Battle between the elf and the cyberdemon!!  ******
    
    The elf attacks for 5 points!
    The cyberdemon attacks for 46 points!
    The elf has 4 hitpoints.
    The cyberdemon has 45 hitpoints.
    
    The elf attacks for 2 points!
    The cyberdemon attacks for 6 points!
    The elf has -2 hitpoints.
    The cyberdemon has 43 hitpoints.
    
    The cyberdemon wins!
    
    
    *****  Battle between the elf and the human!!  ******
    
    The elf attacks for 16 points!
    Magical attack inflicts 16 additional damage points!
    The human attacks for 34 points!
    The elf has 16 hitpoints.
    The human has 18 hitpoints.
    
    The elf attacks for 18 points!
    The human attacks for 100 points!
    The elf has -84 hitpoints.
    The human has 0 hitpoints.
    
    The battle results in a tie!
    
    
    *****  Battle between the cyberdemon and the human!!  ******
    
    The cyberdemon attacks for 50 points!
    Demonic attack inflicts 50 additional damage points!
    The human attacks for 45 points!
    The cyberdemon has 5 hitpoints.
    The human has -50 hitpoints.
    
    The cyberdemon wins!

    Special zyBooks submission note: To make this work with zyBooks, you will need to comment out the "srand()" line before submitting.

  10. All of the classes should still be in the cs_creature namespace

What's the Point?

Take some time to reflect on the program you just completed from the perspective of the base class, Creature. With respect to inheritance, you'll see 3 categories of member functions illustrated (not counting constructors, since they are not inherited).

  1. The accessors and mutators are typical member functions that are inherited and never redefined. For example, the balrog class's getDamage() function calls getStrength(), which it can do because it has been inherited from the Creature class (via the Demon class).

  2. getSpecies() is a pure virtual function. It is not defined in the Creature class, but it must be defined in every class that is derived from the Creature class, or the Creature class won't work, because the Creature class's getDamage() function depends on the fact that getSpecies() exists in the derived class.

  3. getDamage() is a virtual member function that is inherited and is sometimes redefined.