Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

The Fights

Now Aria steps into the arena. She is an object of a new class, Hero, which is already in the program. Its constructor takes her name, health and attack, so Hero hero("Aria", 100, 10); creates a hero with 100 health whose every hit takes 10 health from a monster. Keep that line as it is: the exercise changes it to try a stronger and a weaker Aria.

Aria's health is private, so main can read it with the getter getHealth(), but can't change it. That's on purpose: only the class's own methods change her health, so it always follows the class's rules. The fights need two methods that the class doesn't have yet. Add a method takeDamage() that lowers her health by the damage, but never below 0, and a const method isAlive() that says whether she has any health left.

The rules. Aria fights the monsters in the order of the line-up, and each fight goes like the one with the dragon. Aria strikes first. Each of her hits takes as much health from the monster as her attack, and the monster is defeated when its health drops to 0 or below. After every hit that doesn't defeat it, the monster strikes back and takes as much health from Aria as its attack. Nobody heals her between the fights, so each fight starts with the health that the one before it left her.

The Bat has 10 health, so Aria's first hit defeats it, and it never strikes back. The Goblin survives 2 hits and strikes back twice, which takes 2 × 6 = 12 health, so 88 is left. The Orc needs 5 hits and strikes back 4 times, which takes 40, so 48 is left. The Troll's strikes take 15 each, and after its 4th strike, Aria's health is down to 0. Then she loses the fight, and her day in the arena is over: she doesn't fight the monsters after that one, so she never meets the Dragon. Below the line-up, the program prints a line for each fight:

Bat: Aria wins. Health left: 100
Goblin: Aria wins. Health left: 88
Orc: Aria wins. Health left: 48
Troll: Aria loses.

A tip: you can play out each fight hit by hit, in a loop that goes on while both of them have health, or work it out at once, as in the dragon fight. Either way, break ends the loop over the monsters.

Instructions

  1. 1

    Print a line for each monster that Aria defeats, with the health she has left, as in the example.

  2. 2

    When a monster defeats Aria, print that instead, as in the example, and end the fights there.

Back

2/3

  1. 1. The Line-Up
  2. 2. The Fights
  3. 3. The Loot
Next

Try it yourself

Do this exercise right here in your browser. No account needed.