Character Class

Line: Object Line
Type: Exercise
You should have completed:

Virtual Student

This topic leads to:

Cast Away

Summary

This exercise will create a bare bones simulation of a fantasy fighting game.

Task

  1. Create a console application.
  2. Write a class to represent characters in a role-playing game. A character must have statistics for
    1. strength
    2. intelligence
    3. armour
    4. maximum health
    5. current health

    The first three range from 3 to 18. You should implement the class in such a way as to prevent values outside this range. Full health is commonly around 50 hit points.

  3. A character must support the following behaviours (methods)
    1. Attack : Returns an integer reporting how much damage the character does. This is half the character's strength rounded down.
    2. TakeDamage: Takes an integer parameter for how much damage the character takes, returns a boolean, true if the character dies as a result (i.e. hit points < 0). The character's current hit points are reduced by the damage minus one quarter of their armour (rounded down).
  4. Now write a program that creates two characters with random statistics (in the valid range) and makes them fight each other. Each turn, for each character, the program will use the attack value from one character to damage the other until one of the characters dies. The program should then write out both characters' stats and declare the winner.
  5. Run and test this program.

Questions

  1. Can you extend this program to include a WizardCharacter class? The attack calculation for this character would be 3/4 Intelligence, rounded down, from casting a spell. The maximum armour value allowed by this class would be 10. Randomly select at the start of the 'battle' whether none, one or both of the combatants are wizards.
  2. Can you modify this example so that armour works differently against damage from spells than from normal attacks? Hint: Damage might need to be a more complex type than just a number.
  3. Can you change the Attack method to accept a character object as a parameter (you could name it target) and return void, but to still have the same effect in the end?