From c2e814312a5fb07417f130d836185bb5859d33b2 Mon Sep 17 00:00:00 2001 From: rbc33 Date: Sat, 20 Sep 2025 11:19:31 +0200 Subject: [PATCH 1/3] iteration 0 --- src/viking.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/viking.js b/src/viking.js index 9017bfc8a..97564b6ce 100755 --- a/src/viking.js +++ b/src/viking.js @@ -1,5 +1,10 @@ // Soldier -class Soldier {} +class Soldier { + constructor(health, strength) { + this.health = health + this.strength = strength + } +} // Viking class Viking {} From c7828fcbe318b6bae34f72f91d91f4ad0633eeb8 Mon Sep 17 00:00:00 2001 From: rbc33 Date: Sat, 20 Sep 2025 11:24:28 +0200 Subject: [PATCH 2/3] iteration 1 --- src/viking.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/viking.js b/src/viking.js index 97564b6ce..606e05edf 100755 --- a/src/viking.js +++ b/src/viking.js @@ -4,6 +4,12 @@ class Soldier { this.health = health this.strength = strength } + attack() { + return this.strength + } + receiveDamage(damage) { + this.health -= damage + } } // Viking From 91ff83e778d2a72e1dd57b52953acd47b487234e Mon Sep 17 00:00:00 2001 From: rbc33 Date: Sat, 20 Sep 2025 12:16:56 +0200 Subject: [PATCH 3/3] bonus --- src/viking.js | 71 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 3 deletions(-) diff --git a/src/viking.js b/src/viking.js index 606e05edf..3b0ab9fae 100755 --- a/src/viking.js +++ b/src/viking.js @@ -13,10 +13,75 @@ class Soldier { } // Viking -class Viking {} +class Viking extends Soldier { + constructor(name, health, strength) { + super(health, strength) + this.name = name + } + + receiveDamage(damage) { + this.health -= 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) { + this.health -= 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) { + this.vikingArmy.push(viking) + } + addSaxon(saxon) { + this.saxonArmy.push(saxon) + } + vikingAttack() { + const saxonIndex = Math.floor(Math.random() * this.saxonArmy.length) + const vikingIndex = Math.floor(Math.random() * this.vikingArmy.length) + const strength = this.vikingArmy[vikingIndex].strength + const attack = this.saxonArmy[saxonIndex].receiveDamage(strength) + if (attack === 'A Saxon has died in combat') { + this.saxonArmy.splice(saxonIndex, 1) + } + + return attack + } + saxonAttack() { + const saxonIndex = Math.floor(Math.random() * this.saxonArmy.length) + const vikingIndex = Math.floor(Math.random() * this.vikingArmy.length) + const strength = this.saxonArmy[saxonIndex].strength + const viking = this.vikingArmy[vikingIndex] + const attack = viking.receiveDamage(strength) + if (attack === `${viking.name} has died in act of combat`) { + this.vikingArmy.splice(vikingIndex, 1) + } + return attack + } + + 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.' + } + } +}