Skip to content
Open

done #62

Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ We have learned Object-oriented programming and how `class` and inheritance work

## Submission


done:
<img width="1440" alt="Screenshot 2025-06-06 at 10 25 47 AM" src="https://github.com/user-attachments/assets/00164ba4-c80e-4234-befc-d10c27fff54a" />


- Upon completion, run the following commands:

Expand Down
10 changes: 10 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LAB | JS Vikings</title>

<!-- Jasmine framework -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jasmine-core@4.5.0/lib/jasmine-core/jasmine.css">
<script src="https://cdn.jsdelivr.net/npm/jasmine-core@4.5.0/lib/jasmine-core/jasmine.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jasmine-core@4.5.0/lib/jasmine-core/jasmine-html.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jasmine-core@4.5.0/lib/jasmine-core/boot0.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jasmine-core@4.5.0/lib/jasmine-core/boot1.js"></script>


</head>
<body>
<script src="./src/viking.js"></script>
<script src="./tests/viking.spec.js"></script>
</body>
</html>
110 changes: 106 additions & 4 deletions src/viking.js
Original file line number Diff line number Diff line change
@@ -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.";
}
}
}



Expand Down
2 changes: 1 addition & 1 deletion tests/viking.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Soldier, Viking, Saxon, War } = require('./../src/viking');
// const { Soldier, Viking, Saxon, War } = require('./../src/viking');

describe('Soldier', () => {
let soldier;
Expand Down