Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

hw #53

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open

hw #53

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
78 changes: 77 additions & 1 deletion js/super_hero.js
Original file line number Diff line number Diff line change
@@ -1 +1,77 @@
// var SuperHero = ...
var Superhero = function(name, dmgPoints, hitPoints) {
if (dmgPoints <= 0) {
throw new Error('Damage cannot be less than 0');
}
if (hitPoints <= 0) {
throw new Error('Health cannot be less than 0');
}
this.name = name;
this.dmgPoints = dmgPoints;
this.hitPoints = hitPoints || 100;
this.currentHitPoints = this.hitPoints;
console.log(this + ' enters the arena! HEALTH: ' + this.hitPoints + ', POWER: ' + this.dmgPoints);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might consider adding a toString method for helping to identify the Superhero that you are printing out.

};

Superhero.prototype.toString = function() {
return this.name;
};

Superhero.prototype.isDead = function() {
return this.currentHitPoints <= 0;
};

Superhero.prototype.revive = function() {
this.currentHitPoints = this.hitPoints;
console.log(this + ' revived to ' + this.currentHitPoints + ' hit points.');
};

Superhero.prototype.attack = function(otherHero) {
if (otherHero === this) {
console.log(this + ' tried attacking themself but can\'t do that!');
return false;
}
if (this.isDead()) {
console.log(this + ' can\'t attack. Already dead!');
return false;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice handling of edge cases!

var remaining = otherHero.currentHitPoints - this.dmgPoints;
otherHero.currentHitPoints = remaining <= 0 ? 0 : remaining;
console.log(this + ' attacked ' + otherHero + ' for ' + this.dmgPoints + ' hit points. ' + otherHero + ' has ' + otherHero.currentHitPoints + ' health remaining.');

if (otherHero.currentHitPoints <= 0) {
console.log(otherHero + ' died.');
}
return true;
};

function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, if you took this code from elsewhere, please reference it!



var luke = new Superhero('Luke', 20, 100);
var leia = new Superhero('Leia', 30, 50);
var yoda = new Superhero('Yoda', 35, 150);
var vader = new Superhero('Vader', 25, 200);

var heroes = [luke,leia,yoda,vader];

var hero1,hero2;

while (heroes.length > 1) {
shuffleArray(heroes);
hero1 = heroes[0];
hero2 = heroes[1];
hero1.attack(hero2);
if (hero2.isDead()) {
heroes.splice(1, 1);
}
}

console.log(heroes[0] + ' is the victor!');
58 changes: 56 additions & 2 deletions js/tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
QUnit.test("hello test", function(assert) {
assert.strictEqual(1 + 1, 2, "One plus one is two");
QUnit.test("hit point test", function(assert) {
var hero = new Superhero('name', 50, 100);
assert.strictEqual(hero.hitPoints, 100);
assert.strictEqual(hero.currentHitPoints, 100);
assert.strictEqual(hero.toString(), 'name');
});

QUnit.test("damage ability test", function(assert) {
var hero = new Superhero('name', 50, 100);
assert.strictEqual(hero.dmgPoints, 50);
});

QUnit.test("hit point less than 0", function(assert) {
throws(function() { var h = new Superhero('name', 50, -10);}, new Error('Health cannot be less than 0'));
});

QUnit.test("dmg ability less than 0", function(assert) {
throws(function() { var h = new Superhero('name', -50, 0);}, new Error('Damage cannot be less than 0'));
});

QUnit.test("test attack", function(assert) {
var hero1 = new Superhero('1', 50, 100);
var hero2 = new Superhero('2', 50, 100);
hero1.attack(hero2);
assert.strictEqual(hero1.attack(hero1), false);
assert.strictEqual(hero1.currentHitPoints, 100);
assert.strictEqual(hero1.hitPoints, 100);
assert.strictEqual(hero2.hitPoints, 100);
assert.strictEqual(hero2.currentHitPoints, 50);
assert.strictEqual(hero2.isDead(), false);
assert.strictEqual(hero1.isDead(), false);
});

QUnit.test("test kill", function(assert) {
var hero1 = new Superhero('1', 100, 100);
var hero2 = new Superhero('2', 100, 100);
hero1.attack(hero2);
assert.strictEqual(hero1.currentHitPoints, 100);
assert.strictEqual(hero1.hitPoints, 100);
assert.strictEqual(hero2.hitPoints, 100);
assert.strictEqual(hero2.currentHitPoints, 0);
assert.strictEqual(hero2.isDead(), true);
assert.strictEqual(hero1.isDead(), false);
assert.strictEqual(hero2.attack(hero1), false);
});

QUnit.test("test revive", function(assert) {
var hero1 = new Superhero('1', 100, 100);
var hero2 = new Superhero('2', 100, 100);
hero1.attack(hero2);
assert.strictEqual(hero2.isDead(), true);
hero2.revive();
assert.strictEqual(hero2.isDead(), false);
assert.strictEqual(hero2.currentHitPoints, 100);
});



// ADD TESTS HERE