From b37b87b68cf324b3109b0578c29c21e429b7329d Mon Sep 17 00:00:00 2001 From: Barbara Goldbeck Date: Thu, 26 Jun 2025 12:23:07 +0200 Subject: [PATCH 1/4] Viking Lab partly solved --- src/viking.js | 94 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 90 insertions(+), 4 deletions(-) diff --git a/src/viking.js b/src/viking.js index 9017bfc8a..793e46e4d 100755 --- a/src/viking.js +++ b/src/viking.js @@ -1,11 +1,97 @@ // Soldier -class Soldier {} +class Soldier { + constructor(health, strength) { + this.health = health; + this.strength = strength; + } + + attack() { + return this.strength; + } + + receiveDamage(damage) { + this.health = this.health -= damage; + + } + +} // Viking -class Viking {} + + +class Viking extends Soldier { + constructor(name, health, strength) { + super(health, strength); + this.name = name; + } + + /*attack() { + return this.strength; + } */ + + 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 { + constructor() { + this.vikingArmy = []; + this.saxonArmy =[]; + } + addViking(viking) { + this.vikingArmy.push(viking); + } + addSaxon(saxon) { + this.saxonArmy.push(saxon); + } +// Saxon chosen at random +//Saxon receiveDamage() equal to strength of a Viking +//Remove Dead Saxon from army +//return result of calling receiveDamage() of a Saxon + vikingAttack() { + let randomSaxonIndex = Math.floor(Math.random()*this.saxonArmy.length)+1; + let randomSaxon = this.saxonArmy[randomSaxonIndex]; + + let randomVikingIndex = Math.floor(Math.random()*this.vikingArmy.length)+1; + let randomViking = this.vikingArmy[randomVikingIndex]; + + randomSaxon.receiveDamage(randomViking.strength); + + if (randomSaxon.health <= 0) { + this.saxonArmy.splice(randomSaxonIndex, 1); + } else { + return randomSaxon.health; + } + } + +//Saxon version of vikingAttack +//Viking receives damage equal to the strength of a Saxon + saxonAttack() { + + } +} From 4fc916389aeb19c6366b373b4feb0c629b4bcd9b Mon Sep 17 00:00:00 2001 From: Barbara Goldbeck Date: Thu, 26 Jun 2025 12:38:54 +0200 Subject: [PATCH 2/4] added some changes to the last two iterations --- src/viking.js | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/viking.js b/src/viking.js index 793e46e4d..36f112329 100755 --- a/src/viking.js +++ b/src/viking.js @@ -74,10 +74,10 @@ class Saxon extends Soldier { //Remove Dead Saxon from army //return result of calling receiveDamage() of a Saxon vikingAttack() { - let randomSaxonIndex = Math.floor(Math.random()*this.saxonArmy.length)+1; + let randomSaxonIndex = Math.floor(Math.random()*this.saxonArmy.length); let randomSaxon = this.saxonArmy[randomSaxonIndex]; - let randomVikingIndex = Math.floor(Math.random()*this.vikingArmy.length)+1; + let randomVikingIndex = Math.floor(Math.random()*this.vikingArmy.length); let randomViking = this.vikingArmy[randomVikingIndex]; randomSaxon.receiveDamage(randomViking.strength); @@ -85,13 +85,35 @@ class Saxon extends Soldier { if (randomSaxon.health <= 0) { this.saxonArmy.splice(randomSaxonIndex, 1); } else { - return randomSaxon.health; + return receiveDamage(damage); } } //Saxon version of vikingAttack -//Viking receives damage equal to the strength of a Saxon saxonAttack() { + let randomSaxonIndex = Math.floor(Math.random()*this.saxonArmy.length); + let randomSaxon = this.saxonArmy[randomSaxonIndex]; + + let randomVikingIndex = Math.floor(Math.random()*this.vikingArmy.length); + let randomViking = this.vikingArmy[randomVikingIndex]; - } + randomViking.receiveDamage(randomSaxon.strength); + + if (randomViking.health <= 0) { + this.vikingArmy.splice(randomVikingIndex, 1); + } else { + return receiveDamage(damage); + } + + } + + 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 sill in the thick of battle.` + } + } } From 58cac9bcaf5cc86d26c2217a3f486d958cc3b3e3 Mon Sep 17 00:00:00 2001 From: Barbara Goldbeck Date: Thu, 26 Jun 2025 12:45:23 +0200 Subject: [PATCH 3/4] added corrections after feedback --- src/viking.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/viking.js b/src/viking.js index 36f112329..fb3c22d64 100755 --- a/src/viking.js +++ b/src/viking.js @@ -69,10 +69,7 @@ class Saxon extends Soldier { addSaxon(saxon) { this.saxonArmy.push(saxon); } -// Saxon chosen at random -//Saxon receiveDamage() equal to strength of a Viking -//Remove Dead Saxon from army -//return result of calling receiveDamage() of a Saxon + vikingAttack() { let randomSaxonIndex = Math.floor(Math.random()*this.saxonArmy.length); let randomSaxon = this.saxonArmy[randomSaxonIndex]; @@ -80,16 +77,15 @@ class Saxon extends Soldier { let randomVikingIndex = Math.floor(Math.random()*this.vikingArmy.length); let randomViking = this.vikingArmy[randomVikingIndex]; - randomSaxon.receiveDamage(randomViking.strength); + let result = randomSaxon.receiveDamage(randomViking.strength); if (randomSaxon.health <= 0) { this.saxonArmy.splice(randomSaxonIndex, 1); } else { - return receiveDamage(damage); + return result; } } -//Saxon version of vikingAttack saxonAttack() { let randomSaxonIndex = Math.floor(Math.random()*this.saxonArmy.length); let randomSaxon = this.saxonArmy[randomSaxonIndex]; @@ -97,12 +93,12 @@ class Saxon extends Soldier { let randomVikingIndex = Math.floor(Math.random()*this.vikingArmy.length); let randomViking = this.vikingArmy[randomVikingIndex]; - randomViking.receiveDamage(randomSaxon.strength); + let result = randomViking.receiveDamage(randomSaxon.strength); if (randomViking.health <= 0) { this.vikingArmy.splice(randomVikingIndex, 1); } else { - return receiveDamage(damage); + return result; } } @@ -113,7 +109,7 @@ class Saxon extends Soldier { } else if (this.vikingArmy.length === 0) { return `Saxons have fought for their lives and survived another day...` } else { - return `Vikings and Saxons are sill in the thick of battle.` + return `Vikings and Saxons are still in the thick of battle.` } } } From 873a7b46bd2dce02a6e40e8d1f1f2cdf5f7c124f Mon Sep 17 00:00:00 2001 From: Barbara Goldbeck Date: Thu, 26 Jun 2025 12:49:53 +0200 Subject: [PATCH 4/4] last changes --- src/viking.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/viking.js b/src/viking.js index fb3c22d64..3f50b7e3a 100755 --- a/src/viking.js +++ b/src/viking.js @@ -81,9 +81,8 @@ class Saxon extends Soldier { if (randomSaxon.health <= 0) { this.saxonArmy.splice(randomSaxonIndex, 1); - } else { - return result; } + return result; } saxonAttack() { @@ -97,10 +96,8 @@ class Saxon extends Soldier { if (randomViking.health <= 0) { this.vikingArmy.splice(randomVikingIndex, 1); - } else { - return result; } - + return result; } showStatus() {