Skip to content

Instantly share code, notes, and snippets.

@1travelintexan
Created September 11, 2023 08:13
Show Gist options
  • Save 1travelintexan/6ce1acf62705a2ec7926c0a63c72ab7f to your computer and use it in GitHub Desktop.
Save 1travelintexan/6ce1acf62705a2ec7926c0a63c72ab7f to your computer and use it in GitHub Desktop.
// 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} has died in act of combat`;
} else {
return `${this.name} has received ${damage} points of damage`;
}
}
battleCry() {
return "Odin Owns You All!";
}
}
// Saxon
class Saxon extends Soldier {
receiveDamage(damage) {
this.health -= damage;
if (this.health <= 0) {
return `A Saxon has died in combat`;
} else {
return `A Saxon has received ${damage} points of damage`;
}
}
}
const newSaxon = new Saxon(100, 10);
newSaxon.receiveDamage(15);
console.log(newSaxon);
// War
class War {
constructor() {
this.vikingArmy = [];
this.saxonArmy = [];
}
addViking(aNewViking) {
this.vikingArmy.push(aNewViking);
}
addSaxon(aNewSaxon) {
this.saxonArmy.push(aNewSaxon);
}
vikingAttack() {
//this finds a random index for saxon and creates a guy
const randomSaxonIndex = Math.floor(Math.random() * this.saxonArmy.length);
const randomSaxonGuy = this.saxonArmy[randomSaxonIndex];
//this finds a random index for viking and creates a guy
const randomVikingIndex = Math.floor(
Math.random() * this.vikingArmy.length
);
const randomVikingGuy = this.vikingArmy[randomVikingIndex];
const randomVikingStrength = randomVikingGuy.attack();
const responseFromReceiveDamage =
randomSaxonGuy.receiveDamage(randomVikingStrength);
//this checks the health of the saxon after he was hit
if (randomSaxonGuy.health <= 0) {
this.saxonArmy.splice(randomSaxonIndex, 1);
}
return responseFromReceiveDamage;
}
saxonAttack() {
//this finds a random index for saxon and creates a guy
const randomSaxonIndex = Math.floor(Math.random() * this.saxonArmy.length);
const randomSaxonGuy = this.saxonArmy[randomSaxonIndex];
//this finds a random index for viking and creates a guy
const randomVikingIndex = Math.floor(
Math.random() * this.vikingArmy.length
);
const randomVikingGuy = this.vikingArmy[randomVikingIndex];
const randomSaxonStrength = randomSaxonGuy.attack();
const responseFromReceiveDamage =
randomVikingGuy.receiveDamage(randomSaxonStrength);
//this checks the health of the saxon after he was hit
if (randomVikingGuy.health <= 0) {
this.vikingArmy.splice(randomVikingIndex, 1);
}
return responseFromReceiveDamage;
}
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.`;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment