From 5823c7342fa9feac22bb142cc8d4dbff99262c96 Mon Sep 17 00:00:00 2001 From: mattkim Date: Wed, 17 Feb 2016 16:23:02 -0500 Subject: [PATCH 1/2] hw --- js/super_hero.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++- js/tests.js | 58 ++++++++++++++++++++++++++++++++++-- 2 files changed, 132 insertions(+), 3 deletions(-) diff --git a/js/super_hero.js b/js/super_hero.js index a68037d..b43cac4 100644 --- a/js/super_hero.js +++ b/js/super_hero.js @@ -1 +1,76 @@ -// 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..3e6450e 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 From e805f6c60db9d752135072147119aa395e1f3811 Mon Sep 17 00:00:00 2001 From: mattkim Date: Wed, 17 Feb 2016 16:27:58 -0500 Subject: [PATCH 2/2] fixing jshint erros --- js/super_hero.js | 11 ++++++----- js/tests.js | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/js/super_hero.js b/js/super_hero.js index b43cac4..9d8c0f8 100644 --- a/js/super_hero.js +++ b/js/super_hero.js @@ -14,16 +14,16 @@ var Superhero = function(name, dmgPoints, hitPoints) { 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) { @@ -42,7 +42,7 @@ Superhero.prototype.attack = function(otherHero) { console.log(otherHero + ' died.'); } return true; -} +}; function shuffleArray(array) { for (var i = array.length - 1; i > 0; i--) { @@ -66,7 +66,8 @@ var hero1,hero2; while (heroes.length > 1) { shuffleArray(heroes); - hero1 = heroes[0], hero2 = heroes[1]; + hero1 = heroes[0]; + hero2 = heroes[1]; hero1.attack(hero2); if (hero2.isDead()) { heroes.splice(1, 1); diff --git a/js/tests.js b/js/tests.js index 3e6450e..17fd105 100644 --- a/js/tests.js +++ b/js/tests.js @@ -11,11 +11,11 @@ QUnit.test("damage ability test", function(assert) { }); 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')); + 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')); + throws(function() { var h = new Superhero('name', -50, 0);}, new Error('Damage cannot be less than 0')); }); QUnit.test("test attack", function(assert) {