diff --git a/js/super_hero.js b/js/super_hero.js index a68037d..9d8c0f8 100644 --- a/js/super_hero.js +++ b/js/super_hero.js @@ -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); +}; + +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; + } + 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; +} + + +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!'); diff --git a/js/tests.js b/js/tests.js index 445444e..17fd105 100644 --- a/js/tests.js +++ b/js/tests.js @@ -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