From ffb9920a60c8cb7078d323b953d060b5646d98df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miquel=20Pi=C3=B1a=20Grau?= Date: Wed, 22 Oct 2025 18:00:27 +0200 Subject: [PATCH 1/2] Solved lab --- src/viking.js | 104 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 100 insertions(+), 4 deletions(-) diff --git a/src/viking.js b/src/viking.js index 9017bfc8a..383d92cc9 100755 --- a/src/viking.js +++ b/src/viking.js @@ -1,11 +1,107 @@ // 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 if (this.health <= 0){ + 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 if (this.health <= 0){ + return `A Saxon has died in combat`; + } + } +} // War -class War {} +class War { + + vikingArmy = []; + saxonArmy = []; + + addViking(viking) { + this.vikingArmy.push(viking); + } + + addSaxon(saxon) { + this.saxonArmy.push(saxon); + } + + randomWarrior(army) { + const warriorRandomNumber = Math.floor(Math.random() * army.length); + return [army[warriorRandomNumber], army, warriorRandomNumber]; + } + + warriorAttackDefense(warriorAttack, warriorDefense) { + + const soldierAttack = this.randomWarrior(warriorAttack)[0]; + + const soldierDefense = this.randomWarrior(warriorDefense)[0]; + const soldierDefenseArmy = this.randomWarrior(warriorDefense)[1]; + const soldierDefensePositionArray = this.randomWarrior(warriorDefense)[2]; + + const result = soldierDefense.receiveDamage(soldierAttack.strength); + + if (soldierDefense.health <= 0) { + soldierDefenseArmy.splice(soldierDefensePositionArray,1); + } + + return result; + } + + + vikingAttack() { + return this.warriorAttackDefense(this.vikingArmy, this.saxonArmy); + } + + saxonAttack() { + return this.warriorAttackDefense(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 if (this.vikingArmy.length > 0 && this.saxonArmy.length > 0){ + return `Vikings and Saxons are still in the thick of battle.`; + } + } +} From 42f99e6a9e2371071e289ad3cab00decf75d02b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miquel=20Pi=C3=B1a=20Grau?= Date: Thu, 23 Oct 2025 22:10:07 +0200 Subject: [PATCH 2/2] feat: fix: correct extra call to randomWarrior function --- src/viking.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/viking.js b/src/viking.js index 383d92cc9..de9d95597 100755 --- a/src/viking.js +++ b/src/viking.js @@ -69,13 +69,15 @@ class War { return [army[warriorRandomNumber], army, warriorRandomNumber]; } - warriorAttackDefense(warriorAttack, warriorDefense) { + warriorAttackDefense(warriorAttack, warriorDefense) { const soldierAttack = this.randomWarrior(warriorAttack)[0]; - const soldierDefense = this.randomWarrior(warriorDefense)[0]; - const soldierDefenseArmy = this.randomWarrior(warriorDefense)[1]; - const soldierDefensePositionArray = this.randomWarrior(warriorDefense)[2]; + const soldierDF = this.randomWarrior(warriorDefense); + + const soldierDefense = soldierDF[0]; + const soldierDefenseArmy = soldierDF[1]; + const soldierDefensePositionArray = soldierDF[2]; const result = soldierDefense.receiveDamage(soldierAttack.strength);