Prerequisite: clone this repository.
Heracles must now defeat the Erymanthian boar 🐗 a huge beast that terrifies the inhabitants of Mount Erymanthus in Arcadia.
The boar's strength and dexterity are far superior to yours, it's impossible to beat it in these conditions, your fists won't be enough! You will have to equip yourself accordingly!
First of all, you'll have to add a sword.
- Create a new class
Weapon
in the src/ folder. - This one will correspond to the weapon you will equip the fighters with. It must have a name and a
damage
property initialized to 10. - To add the weapon to the character, in the
Fighter
class, we create aweapon
property. This will be initialized tonull
. - In the index.js file, instantiate an object of type
Weapon
with the name "Sword", a damage of 10. Then associate it with Heracles. - At the moment, the sword has been assigned but it doesn't give you any advantage. Back in the
Fighter
class, we'll modify the behavior of thefight()
method a bit. First, create agetDamage()
method. This should return the strength (strength) of the Fighter, to which you will add the damage from the weapon (if it carries one). - In the
fight()
method, use thisgetDamage()
method to determine the damage inflicted by the fighter. Thus the damage inflicted (viarand()
) will be between 1 andgetDamage()
(the strength + the weapon damage) and not just the strength.
All this calculation could be done directly in
fight()
, but using thegetDamage()
method simplifies thefight()
method and moves the damage calculation logic elsewhere. Furthermore, it is now possible to usegetDamage()
independently offight()
, which will be useful later.
You will do almost the same thing to manage this time a shield. The steps are voluntarily given here in outline.
- Create a
Shield
class with aprotection
property set to 10. - In
Fighter
, add ashield
property. - Then create a
getDefense()
method, adding dexterity and shield protection (if the hero is wearing one). This method will also be used infight()
(instead of the simple call todefender.dexterity
) so that the shield can play its protective role. - As for the weapon, remember to instantiate a shield and associate it to Heracles.
Relaunch the fight with node index.js
, you now do a lot more damage and you are better protected, you should win the fight without too much difficulty!