Skip to content

ft-rmt-2504-es Mackenzie #4156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 168 additions & 9 deletions src/viking.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,170 @@
// Soldier
class Soldier {}
//Soldier
class Soldier {
constructor(health, strength) {
this.health = health;
this.strength = strength;
}

attack() {
return this.strength;
}

receiveDamage(damage) {
this.health -= damage;
}
}

//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} ha recibido ${damage} puntos de daño`;
} else {
return `${this.name} ha muerto en acto de combate`;
}
}

battleCry() {
return "¡Odin es dueño de todos ustedes!";
}
}

//Saxon
class Saxon extends Soldier {
receiveDamage(damage) {
this.health -= damage;
if (this.health > 0) {
return `Un sajón ha recibido ${damage} puntos de daño`;
} else {
return `Un sajón ha muerto en combate`;
}
}
}

//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 || this.vikingArmy.length === 0) return '';

const vikingIndex = Math.floor(Math.random() * this.vikingArmy.length);
const saxonIndex = Math.floor(Math.random() * this.saxonArmy.length);

const viking = this.vikingArmy[vikingIndex];
const saxon = this.saxonArmy[saxonIndex];

const damage = viking.attack();
const result = saxon.receiveDamage(damage);

if (saxon.health <= 0) {
this.saxonArmy.splice(saxonIndex, 1);
}
return result;
}

saxonAttack() {
if (this.saxonArmy.length === 0 || this.vikingArmy.length === 0) return '';

const vikingIndex = Math.floor(Math.random() * this.vikingArmy.length);
const saxonIndex = Math.floor(Math.random() * this.saxonArmy.length);

const viking = this.vikingArmy[vikingIndex];
const saxon = this.saxonArmy[saxonIndex];

const damage = saxon.attack();
const result = viking.receiveDamage(damage);

if (viking.health <= 0) {
this.vikingArmy.splice(vikingIndex, 1);
}
return result;
}

showStatus() {
if (this.saxonArmy.length === 0) {
return "¡Los vikingos han ganado la guerra del siglo!";
} else if (this.vikingArmy.length === 0) {
return "Los sajones han luchado por sus vidas y sobrevivieron otro día...";
} else {
return "Los vikingos y los sajones todavía están en el fragor de la batalla";
}
}

// Iteración 5
attack(attackingArmy, defendingArmy) {
if (defendingArmy.length === 0 || attackingArmy.length === 0) return '';

const attackerIndex = Math.floor(Math.random() * attackingArmy.length);
const defenderIndex = Math.floor(Math.random() * defendingArmy.length);

const attacker = attackingArmy[attackerIndex];
const defender = defendingArmy[defenderIndex];
const damage = attacker.attack();
const result = defender.receiveDamage(damage);

if (defender.health <= 0) {
defendingArmy.splice(defenderIndex, 1);
}
return result;
}

vikingAttackRefactor() {
return this.attack(this.vikingArmy, this.saxonArmy);
}

saxonAttackRefactor() {
return this.attack(this.saxonArmy, this.vikingArmy);
}
}


// Viking
class Viking {}
const guerra = new War();

const viking1 = new Viking("Ragnar", 100, 20);
const viking2 = new Viking("Bjorn", 120, 15);
const saxon1 = new Saxon(80, 10);
const saxon2 = new Saxon(90, 12);

guerra.addViking(viking1);
guerra.addViking(viking2);
guerra.addSaxon(saxon1);
guerra.addSaxon(saxon2);

console.log(guerra.showStatus());
console.log(guerra.vikingAttack());
console.log(guerra.showStatus());
console.log(guerra.saxonAttack());
console.log(guerra.showStatus());


// Saxon
class Saxon {}

// War
class War {}
const guerraRefactor = new War();
guerraRefactor.addViking(viking1);
guerraRefactor.addViking(viking2);
guerraRefactor.addSaxon(saxon1);
guerraRefactor.addSaxon(saxon2);

console.log("Refactorizado:");
console.log(guerraRefactor.showStatus());
console.log(guerraRefactor.vikingAttackRefactor());
console.log(guerraRefactor.showStatus());
console.log(guerraRefactor.saxonAttackRefactor());
console.log(guerraRefactor.showStatus());