From d3ef0b69543f7b30aca8b3cd2e5a1b2487f976fb Mon Sep 17 00:00:00 2001 From: Amrit Biswal Date: Mon, 24 Nov 2025 15:54:38 +0100 Subject: [PATCH 1/2] Viking Lab completed --- src/viking.js | 98 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 94 insertions(+), 4 deletions(-) diff --git a/src/viking.js b/src/viking.js index 9017bfc8a..3c4fa3962 100755 --- a/src/viking.js +++ b/src/viking.js @@ -1,11 +1,101 @@ // 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); + 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 { + + constructor(health, strength) { + super(health, strength); + } + + receiveDamage(damage) { + super.receiveDamage(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(viking) { + this.vikingArmy.push(viking); + } + addSaxon(saxon) { + this.saxonArmy.push(saxon); + } + vikingAttack() { + const randomSaxonIndex = Math.floor(Math.random() * this.saxonArmy.length); + const randomVikingIndex = Math.floor(Math.random() * this.vikingArmy.length); + const saxon = this.saxonArmy[randomSaxonIndex]; + const viking = this.vikingArmy[randomVikingIndex]; + const damage = viking.attack(); + const result = saxon.receiveDamage(damage); + if (saxon.health <= 0) { + this.saxonArmy.splice(randomSaxonIndex, 1); + } + return result; + } + saxonAttack() { + const randomSaxonIndex = Math.floor(Math.random() * this.saxonArmy.length); + const randomVikingIndex = Math.floor(Math.random() * this.vikingArmy.length); + const saxon = this.saxonArmy[randomSaxonIndex]; + const viking = this.vikingArmy[randomVikingIndex]; + const damage = saxon.attack(); + const result = viking.receiveDamage(damage); + if (viking.health <= 0) { + this.vikingArmy.splice(randomVikingIndex, 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."; + } + } +} \ No newline at end of file From be2db9237e4ee9f60db23c4df48c0a767f6ec7e3 Mon Sep 17 00:00:00 2001 From: Amrit Biswal Date: Mon, 24 Nov 2025 15:57:52 +0100 Subject: [PATCH 2/2] Lab updated --- src/viking.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/viking.js b/src/viking.js index 3c4fa3962..3a617ee69 100755 --- a/src/viking.js +++ b/src/viking.js @@ -22,7 +22,8 @@ class Viking extends Soldier { } receiveDamage(damage) { - super.receiveDamage(damage); + // super.receiveDamage(damage); + this.health -= damage; if (this.health > 0) { return `${this.name} has received ${damage} points of damage`; } else { @@ -43,7 +44,8 @@ class Saxon extends Soldier { } receiveDamage(damage) { - super.receiveDamage(damage); + // super.receiveDamage(damage); + this.health -= damage; if (this.health > 0) { return `A Saxon has received ${damage} points of damage`; } else {