From 6c76f03d73ea835b56069705018962e6b572dbe3 Mon Sep 17 00:00:00 2001 From: jopen Date: Fri, 1 Aug 2025 19:32:27 +0200 Subject: [PATCH 1/3] me falta el bonus --- src/viking.js | 94 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 88 insertions(+), 6 deletions(-) diff --git a/src/viking.js b/src/viking.js index 9017bfc8a..7489fcdb6 100755 --- a/src/viking.js +++ b/src/viking.js @@ -1,11 +1,93 @@ // Soldier -class Soldier {} +class Soldier { + constructor(health, strength) { + this.health = health; + this.strength = strength; + } + attack() { + return this.strength; + } + receiveDamage(damage) { + if (damage > this.health) { + return "game over" + } else { + this.health -= damage; + } + } + } -// Viking -class Viking {} +class Viking extends Soldier { + super(health, strength, name) { + this.name = name; + } + receiveDamage(damage) { + if (damage < this.health) { + this.health -= damage; + return `${this.name} has received ${damage} points of damage` + } else { + return `${this.name} has died in act of combat` + } + } + battleCry() { + return "Odins Owns You All!!!!" + } +} + +class Saxon extends Soldier { + receiveDamage(damage) { + if (damage < this.health) { + this.health -= damage; + return `this Saxon has received &{damage} points of damage` + } else { return "A Saxon has died in combat"} + } +} + + +// Vikingno -// Saxon -class Saxon {} // War -class War {} +class War { + constructor(vikingArmy, saxonArmy) { + this.vikingArmy = []; + this.saxonArmy = []; + } + addViking(viking) { + this.vikingArmy.push(saxon); + } + addSaxon() { + this.saxonArmy.push(viking); + } + vikingAttack() { + const randomSaxon = this.SaxonArmy[Math.floor(this.SaxonArmy.length * Math.random)]; + const randomViking = this.vikingArmy[Math.floor(this.vikingArmy.length * Math.random)]; + const result = randomSaxon.receiveDamage(randomViking.strength); + + if (randomSaxon.health > result) { + randomSaxon.health -= result; + } else { console.log("se ha eliminado un saxon random") + return this.saxonArmy.splice([Math.floor(this.SaxonArmy.length * Math.random)], 1) + } + } + saxonAttack() { + const randomSaxon = this.SaxonArmy[Math.floor(this.SaxonArmy.length * Math.random)]; + const randomViking = this.vikingArmy[Math.floor(this.vikingArmy.length * Math.random)]; + const result = randomViking.receiveDamage(randomSaxon.strength); + + if (randomViking.health > result) { + randomViking.health -= result; + } else { console.log("se ha eliminado un saxon random") + return this.VikingArmy.splice([Math.floor(this.VikingArmy.length * Math.random)], 1); + } + } + showStatus() { + if (this.saxonArmy.length = 0) { + return "Vikings have won the war of the century!" + } else if (this.vikingArmy.length = 0) { + return "Saxon have fought for their lives and survived another day.." + } else if (this.vikingArmy > 0 && this.saxonArmy > 0) { + return "Vikings and Saxons are still in the thick of battle." + } + } + } + From b48cfc66c69500dafd29c125b42570da4112da2d Mon Sep 17 00:00:00 2001 From: jopen Date: Fri, 1 Aug 2025 20:06:24 +0200 Subject: [PATCH 2/3] falta bonus --- src/viking.js | 54 ++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/viking.js b/src/viking.js index 7489fcdb6..f0bcfb58f 100755 --- a/src/viking.js +++ b/src/viking.js @@ -9,21 +9,20 @@ class Soldier { } receiveDamage(damage) { if (damage > this.health) { - return "game over" - } else { - this.health -= damage; - } - } + this.health -= damage } +} +} + class Viking extends Soldier { super(health, strength, name) { this.name = name; } receiveDamage(damage) { - if (damage < this.health) { - this.health -= damage; - return `${this.name} has received ${damage} points of 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` } @@ -35,10 +34,11 @@ class Viking extends Soldier { class Saxon extends Soldier { receiveDamage(damage) { - if (damage < this.health) { - this.health -= damage; - return `this Saxon has received &{damage} points of damage` - } else { return "A Saxon has died in combat"} + this.health -= damage; + if (this.health > 0) { + return `this Saxon has received ${damage} points of damage` + } else { + return "A Saxon has died in combat"} } } @@ -53,39 +53,41 @@ class War { this.saxonArmy = []; } addViking(viking) { - this.vikingArmy.push(saxon); + this.vikingArmy.push(viking); } addSaxon() { - this.saxonArmy.push(viking); + this.saxonArmy.push(saxon); } vikingAttack() { const randomSaxon = this.SaxonArmy[Math.floor(this.SaxonArmy.length * Math.random)]; const randomViking = this.vikingArmy[Math.floor(this.vikingArmy.length * Math.random)]; const result = randomSaxon.receiveDamage(randomViking.strength); - if (randomSaxon.health > result) { - randomSaxon.health -= result; - } else { console.log("se ha eliminado un saxon random") - return this.saxonArmy.splice([Math.floor(this.SaxonArmy.length * Math.random)], 1) + if (randomSaxon.health <= 0) { + this.SaxonArmy.splice([Math.floor(this.SaxonArmy.length * Math.random)], 1); + console.log("se ha eliminado un viking random"); + } else { + return resultAttack; } } saxonAttack() { const randomSaxon = this.SaxonArmy[Math.floor(this.SaxonArmy.length * Math.random)]; const randomViking = this.vikingArmy[Math.floor(this.vikingArmy.length * Math.random)]; - const result = randomViking.receiveDamage(randomSaxon.strength); + const resultAttack = randomViking.receiveDamage(randomSaxon.strength); - if (randomViking.health > result) { - randomViking.health -= result; - } else { console.log("se ha eliminado un saxon random") - return this.VikingArmy.splice([Math.floor(this.VikingArmy.length * Math.random)], 1); + if (randomViking.health <= 0) { + this.VikingArmy.splice([Math.floor(this.VikingArmy.length * Math.random)], 1); + console.log("se ha eliminado un saxon random"); + } else { + return resultAttack; } } showStatus() { - if (this.saxonArmy.length = 0) { + if (this.saxonArmy.length === 0) { return "Vikings have won the war of the century!" - } else if (this.vikingArmy.length = 0) { + } else if (this.vikingArmy.length === 0) { return "Saxon have fought for their lives and survived another day.." - } else if (this.vikingArmy > 0 && this.saxonArmy > 0) { + } else { return "Vikings and Saxons are still in the thick of battle." } } From e22e86ee54ff3798c5a30cbffeb14c112ea382ff Mon Sep 17 00:00:00 2001 From: jopen Date: Sat, 2 Aug 2025 10:50:38 +0200 Subject: [PATCH 3/3] ejercicio terminado con bonus 5 --- src/viking.js | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/src/viking.js b/src/viking.js index f0bcfb58f..5b098aa72 100755 --- a/src/viking.js +++ b/src/viking.js @@ -59,9 +59,14 @@ class War { this.saxonArmy.push(saxon); } vikingAttack() { - const randomSaxon = this.SaxonArmy[Math.floor(this.SaxonArmy.length * Math.random)]; - const randomViking = this.vikingArmy[Math.floor(this.vikingArmy.length * Math.random)]; - const result = randomSaxon.receiveDamage(randomViking.strength); + const indexSaxon = Math.floor(Math.random() * this.saxonArmy.length) + const indexViking = Math.floor(Math.random() * this.vikingArmy.length) + + const randomSaxon = this.SaxonArmy[indexSaxon]; + const randomViking = this.vikingArmy[indexViking]; + const resultAttack = randomSaxon.receiveDamage(randomViking.strength); + + if (randomSaxon.health <= 0) { this.SaxonArmy.splice([Math.floor(this.SaxonArmy.length * Math.random)], 1); @@ -71,8 +76,12 @@ class War { } } saxonAttack() { - const randomSaxon = this.SaxonArmy[Math.floor(this.SaxonArmy.length * Math.random)]; - const randomViking = this.vikingArmy[Math.floor(this.vikingArmy.length * Math.random)]; + + const indexSaxon = Math.floor(Math.random() * this.saxonArmy.length) + const indexViking = Math.floor(Math.random() * this.vikingArmy.length) + + const randomSaxon = this.SaxonArmy[indexSaxon]; + const randomViking = this.vikingArmy[indexViking]; const resultAttack = randomViking.receiveDamage(randomSaxon.strength); if (randomViking.health <= 0) { @@ -93,3 +102,26 @@ class War { } } + // BONUS 5 hacer el metodo para el ataque genérico, y luego se elige atacante con el metodo especifico de cada uno. + genericAttack(attackerArmy, defenderArmy) { + + const indexAttacker = Math.floor(Math.random() * attackerArmy.length); + const indexDefender = Math.floor(Math.random() * defenderArmy.length); + + + const randomAttacker = attackerArmy[indexAttacker]; + const randomDefender = defenderArmy[indexDefender]; + const resultAttack = randomDefender.receiveDamage(randomAttacker.strength); + + if (randomDefender.health <= 0) { + defenderArmy.splice(indexDefender, 1); + } return resultAttack; + } + + vikingAttack() { + return this.genericAttack(this.vikingArmy, this.saxonArmy); + } + + saxonAttack() { + return this.saxonAttack(this.saxonArmy, this.vikingArmy) + }