From a8985c1ace957296c907276cbd0e16fcc9bd9d8e Mon Sep 17 00:00:00 2001 From: Gail Matsushima Date: Tue, 24 Nov 2015 19:08:50 -1000 Subject: [PATCH 1/3] almost there on final one --- constructors.js | 52 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/constructors.js b/constructors.js index d0bf11a..a496540 100644 --- a/constructors.js +++ b/constructors.js @@ -10,7 +10,11 @@ * @property {string} description * @method printDetails */ - + function Spell (name, cost, description) { + this.name = name; + this.cost = cost; + this.description = description; + } /** * Returns a string of all of the spell's details. * The format doesn't matter, as long as it contains the spell name, cost, and description. @@ -19,6 +23,9 @@ * @return {string} details containing all of the spells information. */ + Spell.prototype.getDetails = function () { + return this.name + ", " + this.cost + ", " + this.description; + }; /** * A spell that deals damage. * We want to keep this code DRY (Don't Repeat Yourself). @@ -43,6 +50,15 @@ * @property {number} damage * @property {string} description */ +function DamageSpell (name, cost, damage, description) { + Spell.call(this, name, cost, description); + this.damage = damage; +} +DamageSpell.prototype = Object.create(Spell.prototype, { + constructor: { + value: DamageSpell + } +}); /** * Now that you've created some spells, let's create @@ -60,6 +76,14 @@ * @method spendMana * @method invoke */ +function Spellcaster (name, health, mana) { + this.name = name; + this.health = health; + this.mana = mana; + this.isAlive = true; +} + + /** * @method inflictDamage @@ -72,6 +96,14 @@ * @param {number} damage Amount of damage to deal to the spellcaster */ +Spellcaster.prototype.inflictDamage = function (damage){ + + this.health -= damage; //x = x - y + if (this.health <= 0) { + this.health = 0; + this.isAlive = false; + } +}; /** * @method spendMana * @@ -81,7 +113,14 @@ * @param {number} cost The amount of mana to spend. * @return {boolean} success Whether mana was successfully spent. */ - + Spellcaster.prototype.spendMana = function (cost) { + if (this.mana >= cost) { + this.mana -= cost; + return true; + }else{ + return false; + } + }; /** * @method invoke * @@ -108,3 +147,12 @@ * @param {Spellcaster} target The spell target to be inflicted. * @return {boolean} Whether the spell was successfully cast. */ + Spellcaster.prototype.invoke = function (spell, target) { + //testng to see if first parameter is 'Spell' + //instanceof for e.g. cars - porsche, toyota, cars the object and toyota and porsche are instances + if (!(spell === Spell)) { + return false; + } + } + + }; From c47c4d053f1a369345a37d2ad7ff07c2a260c4bf Mon Sep 17 00:00:00 2001 From: Gail Matsushima Date: Wed, 25 Nov 2015 22:59:38 -1000 Subject: [PATCH 2/3] completed js-constructors --- constructors.js | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/constructors.js b/constructors.js index a496540..f2a9c6a 100644 --- a/constructors.js +++ b/constructors.js @@ -147,12 +147,31 @@ Spellcaster.prototype.inflictDamage = function (damage){ * @param {Spellcaster} target The spell target to be inflicted. * @return {boolean} Whether the spell was successfully cast. */ - Spellcaster.prototype.invoke = function (spell, target) { - //testng to see if first parameter is 'Spell' - //instanceof for e.g. cars - porsche, toyota, cars the object and toyota and porsche are instances - if (!(spell === Spell)) { + Spellcaster.prototype.invoke = function (spell, target) { + //testng to see if first parameter is 'Spell' + //draw a tree 'flow chart', branches, and ask the right questions + //instanceof for e.g. cars - porsche, toyota, cars the object and toyota and porsche are instances + if (!(spell instanceof Spell)) { + return false; + } + //this only checks if 'is' a DamageSpell, but not if it isn't, which is still acceptable + if (spell instanceof DamageSpell && target instanceof Spellcaster) { + + if (this.spendMana(spell.cost)){ + target.inflictDamage(spell.damage); + return true; + } else { + return false; + } + } + //if NOT a DamageSpell then what? - check if enough mana + if (!(spell instanceof DamageSpell)) { + if(this.spendMana(spell.cost)) { + return true; + } else{ return false; } - } - - }; + } + //DamageSpell with NO valid target, returns false + return false; + }; From c4bf33a4a24384e28c1c206ff1b47125d3444bd9 Mon Sep 17 00:00:00 2001 From: Gail Matsushima Date: Thu, 26 Nov 2015 21:06:00 -1000 Subject: [PATCH 3/3] revised constructors.js --- constructors.js | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/constructors.js b/constructors.js index f2a9c6a..b0dba99 100644 --- a/constructors.js +++ b/constructors.js @@ -150,28 +150,23 @@ Spellcaster.prototype.inflictDamage = function (damage){ Spellcaster.prototype.invoke = function (spell, target) { //testng to see if first parameter is 'Spell' //draw a tree 'flow chart', branches, and ask the right questions + //target = object, a person, the spellcaster //instanceof for e.g. cars - porsche, toyota, cars the object and toyota and porsche are instances if (!(spell instanceof Spell)) { return false; } - //this only checks if 'is' a DamageSpell, but not if it isn't, which is still acceptable - if (spell instanceof DamageSpell && target instanceof Spellcaster) { - - if (this.spendMana(spell.cost)){ - target.inflictDamage(spell.damage); - return true; - } else { - return false; - } + //this only checks if 'is' a DamageSpell, but not if it is a NON-DamageSpell, which is still acceptable + if (spell instanceof DamageSpell && (!(target instanceof Spellcaster))) { + return false; } - //if NOT a DamageSpell then what? - check if enough mana - if (!(spell instanceof DamageSpell)) { - if(this.spendMana(spell.cost)) { - return true; - } else{ - return false; + //assumes either a DamageSpell or NON, also checking 'target' + if (this.spendMana(spell.cost)) { + if(target instanceof Spellcaster) { + target.inflictDamage(spell.damage); } + return true; } - //DamageSpell with NO valid target, returns false return false; }; + //can also relook at wanting to return spendMana + //can also think of all questions that would return false