From 293e4034850ec8126857c07119e7fe33a6383b7f Mon Sep 17 00:00:00 2001 From: A4Revolution <91364746+AdeDeepFishing@users.noreply.github.com> Date: Fri, 6 Jun 2025 10:26:34 +0900 Subject: [PATCH 1/2] done --- index.html | 10 ++++ src/viking.js | 110 +++++++++++++++++++++++++++++++++++++++++-- tests/viking.spec.js | 2 +- 3 files changed, 117 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index 342f117..709aec0 100644 --- a/index.html +++ b/index.html @@ -5,8 +5,18 @@ LAB | JS Vikings + + + + + + + + + + diff --git a/src/viking.js b/src/viking.js index fe1febf..1f9c673 100644 --- a/src/viking.js +++ b/src/viking.js @@ -1,14 +1,116 @@ // 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 { + 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) { + this.health -= 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() { + if (this.saxonArmy.length === 0) return "No Saxons left to attack"; + + const randomSaxonIndex = Math.floor(Math.random() * this.saxonArmy.length); + const randomSaxon = this.saxonArmy[randomSaxonIndex]; + + const damage = randomSaxon.receiveDamage(this.vikingArmy[0].attack()); + + if (randomSaxon.health <= 0) { + this.saxonArmy.splice(randomSaxonIndex, 1); + } + + return damage; + } + + saxonAttack() { + if (this.vikingArmy.length === 0) return "No Vikings left to attack"; + + const randomVikingIndex = Math.floor(Math.random() * this.vikingArmy.length); + const randomViking = this.vikingArmy[randomVikingIndex]; + + const damage = randomViking.receiveDamage(this.saxonArmy[0].attack()); + + if (randomViking.health <= 0) { + this.vikingArmy.splice(randomVikingIndex, 1); + } + + return 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 still in the thick of battle."; + } + } +} diff --git a/tests/viking.spec.js b/tests/viking.spec.js index 597f8fb..5b41448 100644 --- a/tests/viking.spec.js +++ b/tests/viking.spec.js @@ -1,4 +1,4 @@ -const { Soldier, Viking, Saxon, War } = require('./../src/viking'); +// const { Soldier, Viking, Saxon, War } = require('./../src/viking'); describe('Soldier', () => { let soldier; From 5761b0e46b09b3d8c842bb6d5bf844cafdff51ae Mon Sep 17 00:00:00 2001 From: yanwen <91364746+AdeDeepFishing@users.noreply.github.com> Date: Fri, 6 Jun 2025 10:28:03 +0900 Subject: [PATCH 2/2] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 07007dd..6e066e5 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,9 @@ We have learned Object-oriented programming and how `class` and inheritance work ## Submission - + done: + Screenshot 2025-06-06 at 10 25 47 AM + - Upon completion, run the following commands: