From f89a0cbfd3e316762a5a882ff0ffae69ab20b6de Mon Sep 17 00:00:00 2001 From: Carlos Canet Date: Mon, 28 Jul 2025 21:36:41 +0200 Subject: [PATCH 1/2] Done. Riding to war, Odin watches! --- src/viking.js | 92 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 88 insertions(+), 4 deletions(-) diff --git a/src/viking.js b/src/viking.js index 9017bfc8a..9133ff5a2 100755 --- a/src/viking.js +++ b/src/viking.js @@ -1,11 +1,95 @@ // 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) { + super.receiveDamage(damage); + return (this.health > 0) ? `${this.name} has received ${damage} points of damage` : `${this.name} has died in act of combat`; + } + + battleCry() { + return "Odin Owns You All!"; + } +} // Saxon -class Saxon {} +class Saxon extends Soldier { + receiveDamage(damage) { + super.receiveDamage(damage); + return (this.health > 0) ? `A Saxon has received ${damage} points of damage` : `A Saxon has died in combat`; + } +} + // War -class War {} +class War { + constructor() { + this.vikingArmy = []; + this.saxonArmy = []; + } + + addViking(viking) { + if (viking instanceof Viking) { + this.vikingArmy.push(viking); + } + } + + addSaxon(saxon) { + if (saxon instanceof Saxon) { + this.saxonArmy.push(saxon); + } + } + + vikingAttack() { + const attacker = this.vikingArmy[Math.floor(Math.random() * this.vikingArmy.length)]; + const defenderIndex = Math.floor(Math.random() * this.saxonArmy.length); + const defender = this.saxonArmy[defenderIndex]; + const result = defender.receiveDamage(attacker.strength); + if (defender.health <= 0) { + this.saxonArmy.splice(defenderIndex, 1); + } + return result; + } + + saxonAttack() { + const attacker = this.saxonArmy[Math.floor(Math.random() * this.saxonArmy.length)]; + const defenderIndex = Math.floor(Math.random() * this.vikingArmy.length); + const defender = this.vikingArmy[defenderIndex]; + + const result = defender.receiveDamage(attacker.strength); + if (defender.health <= 0) { + this.vikingArmy.splice(defenderIndex, 1); + } + return result; + } + + 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."; + } + + } +} From 9f3f1a71218a9520d3aee3d97ac27891da9ae175 Mon Sep 17 00:00:00 2001 From: Carlos Canet Date: Mon, 28 Jul 2025 21:54:11 +0200 Subject: [PATCH 2/2] Bonus 5 optimization done. Another soul sent to Valhalla! --- src/viking.js | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/viking.js b/src/viking.js index 9133ff5a2..0c4e10fda 100755 --- a/src/viking.js +++ b/src/viking.js @@ -49,37 +49,22 @@ class War { addViking(viking) { if (viking instanceof Viking) { - this.vikingArmy.push(viking); + this.#addSoldier(viking, this.vikingArmy); } } addSaxon(saxon) { if (saxon instanceof Saxon) { - this.saxonArmy.push(saxon); + this.#addSoldier(saxon, this.saxonArmy); } } vikingAttack() { - const attacker = this.vikingArmy[Math.floor(Math.random() * this.vikingArmy.length)]; - const defenderIndex = Math.floor(Math.random() * this.saxonArmy.length); - const defender = this.saxonArmy[defenderIndex]; - const result = defender.receiveDamage(attacker.strength); - if (defender.health <= 0) { - this.saxonArmy.splice(defenderIndex, 1); - } - return result; + return this.#soldierAttack(this.vikingArmy, this.saxonArmy); } saxonAttack() { - const attacker = this.saxonArmy[Math.floor(Math.random() * this.saxonArmy.length)]; - const defenderIndex = Math.floor(Math.random() * this.vikingArmy.length); - const defender = this.vikingArmy[defenderIndex]; - - const result = defender.receiveDamage(attacker.strength); - if (defender.health <= 0) { - this.vikingArmy.splice(defenderIndex, 1); - } - return result; + return this.#soldierAttack(this.saxonArmy, this.vikingArmy); } showStatus() { @@ -92,4 +77,20 @@ class War { } } + + #addSoldier(soldier, army) { + army.push(soldier); + } + + #soldierAttack(attackerArmy, defenderArmy) { + const attacker = attackerArmy[Math.floor(Math.random() * attackerArmy.length)]; + const defenderIndex = Math.floor(Math.random() * defenderArmy.length); + const defender = defenderArmy[defenderIndex]; + + const result = defender.receiveDamage(attacker.strength); + if (defender.health <= 0) { + defenderArmy.splice(defenderIndex, 1); + } + return result; + } }