diff --git a/src/viking.js b/src/viking.js index fe1febf..e1c5a4d 100644 --- a/src/viking.js +++ b/src/viking.js @@ -1,14 +1,98 @@ // Soldier -class Soldier {} +class Soldier { + constructor(health, strength) { + this.health = health; + this.strength = strength; + } + + attack() { + return this.strength; + } + + receiveDamage(damage) { + this.health -= damage; + } +} // Viking -class Viking {} +class Viking extends Soldier { + constructor(name, health, strength) { + super(health, strength); + this.name = name; + } + + receiveDamage(damage) { + this.health -= damage; + if (this.health > 0) { + return `${this.name} has received ${damage} points of damage`; + } else { + return `${this.name} has died in act of combat`; + } + } + + battleCry() { + return "Odin Owns You All!"; + } +} // Saxon -class Saxon {} +class Saxon extends Soldier { + receiveDamage(damage) { + this.health -= damage; + if (this.health > 0) { + return `A Saxon has received ${damage} points of damage`; + } else { + return `A Saxon has died in combat`; + } + } +} // War -class War {} +class War { + constructor() { + this.vikingArmy = []; + this.saxonArmy = []; + } + + addViking(vikingObject) { + this.vikingArmy.push(vikingObject); + } + + addSaxon(saxonObject) { + this.saxonArmy.push(saxonObject); + } + + #genericAttack(attackerArmy, defenderArmy) { + if (attackerArmy.length === 0 || defenderArmy.length === 0) return; + const randomAttackerIndex = Math.floor(Math.random() * attackerArmy.length); + const randomDefenderIndex = Math.floor(Math.random() * defenderArmy.length); + const attacker = attackerArmy[randomAttackerIndex]; + const defender = defenderArmy[randomDefenderIndex]; + const result = defender.receiveDamage(attacker.attack()); + if (defender.health <= 0) { + defenderArmy.splice(randomDefenderIndex, 1); + } + return result; + } + + vikingAttack() { + return this.#genericAttack(this.vikingArmy, this.saxonArmy); + } + + saxonAttack() { + return this.#genericAttack(this.saxonArmy, this.vikingArmy); + } + + showStatus() { + if (this.saxonArmy.length === 0) { + return "Vikings have won the war of the century!"; + } else if (this.vikingArmy.length === 0) { + return "Saxons have fought for their lives and survived another day..."; + } else { + return "Vikings and Saxons are still in the thick of battle."; + } + } +} @@ -16,4 +100,4 @@ class War {} /* Environment setup. Do not modify the below code. */ if (typeof module !== 'undefined') { module.exports = { Soldier, Viking, Saxon, War }; -} +} \ No newline at end of file