This repository was archived by the owner on Mar 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
hw #53
Open
iamakimmer
wants to merge
2
commits into
advanced-js:gh-pages
Choose a base branch
from
iamakimmer:hw
base: gh-pages
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
hw #53
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
|
||
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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 theSuperhero
that you are printing out.