From 31fc0d23c9919661ca2839f4d497d90cddf01926 Mon Sep 17 00:00:00 2001 From: Eli Sacks Date: Wed, 12 Jun 2019 13:42:00 -0400 Subject: [PATCH 1/2] prototype-refactor.js completed --- assignments/prototype-refactor.js | 304 ++++++++++++++++++++++++++++++ 1 file changed, 304 insertions(+) diff --git a/assignments/prototype-refactor.js b/assignments/prototype-refactor.js index 91424c9fa..edb519cb1 100644 --- a/assignments/prototype-refactor.js +++ b/assignments/prototype-refactor.js @@ -7,3 +7,307 @@ Prototype Refactor 2. Your goal is to refactor all of this code to use ES6 Classes. The console.log() statements should still return what is expected of them. */ + +class GameObject +{ + constructor(gObjAttrs) + { + this.createdAt = gObjAttrs.createdAt; + this.name = gObjAttrs.name; + this.dimensions = gObjAttrs.dimensions; + } + destroy() {return `${this.name} was removed from the game`;} +} + +class CharacterStats extends GameObject +{ + constructor(charStatAttrs) + { + super(charStatAttrs); + this.healthPoints = charStatAttrs.healthPoints; + } + takeDamage() {return `${this.name} took damage`;} +} + +class Humanoid extends CharacterStats +{ + constructor(hmndAttrs) + { + super(hmndAttrs); + this.team = hmndAttrs.team; + this.weapons = hmndAttrs.weapons; + this.language = hmndAttrs.language; + } + greet() {return `${this.name} offers a greeting in ${this.language}`;} +} + +const mage = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 1, + height: 1, + }, + healthPoints: 5, + name: 'Bruce', + team: 'Mage Guild', + weapons: [ + 'Staff of Shamalama', + ], + language: 'Common Tongue', +}); + +const swordsman = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 2, + height: 2, + }, + healthPoints: 15, + name: 'Sir Mustachio', + team: 'The Round Table', + weapons: [ + 'Giant Sword', + 'Shield', + ], + language: 'Common Tongue', +}); + +const archer = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 1, + width: 2, + height: 4, + }, + healthPoints: 10, + name: 'Lilith', + team: 'Forest Kingdom', + weapons: [ + 'Bow', + 'Dagger', + ], + language: 'Elvish', +}); + +console.log(mage.createdAt); // Today's date +console.log(archer.dimensions); // { length: 1, width: 2, height: 4 } +console.log(swordsman.healthPoints); // 15 +console.log(mage.name); // Bruce +console.log(swordsman.team); // The Round Table +console.log(mage.weapons); // Staff of Shamalama +console.log(archer.language); // Elvish +console.log(archer.greet()); // Lilith offers a greeting in Elvish. +console.log(mage.takeDamage()); // Bruce took damage. +console.log(swordsman.destroy()); // Sir Mustachio was removed from the game. + +class Villain extends Humanoid +{ + constructor(vlnAttrs) + { + super(vlnAttrs); + this.xp = vlnAttrs.xp; + this.AC = vlnAttrs.AC; + this.hitBon = vlnAttrs.hitBon; + this.dmgDice = vlnAttrs.dmgDice; + this.dmgBon = vlnAttrs.dmgBon; + } + attack(heroObj) + { + let dmgCalc = function() + { + let dmg = 0; + for(let i=0; i < this.dmgDice; i++) + { + dmg +=Math.floor(1 + Math.random()*6); + } + dmg += this.dmgBon; + return dmg; + } + + let attResolve = function() + { + let hitRoll = this.hitBon + Math.floor(1+Math.random()*19); + // console.log(hitRoll); + if (hitRoll >= heroObj.AC) + { + let dmg = dmgCalc.call(this); + heroObj.healthPoints -= dmg; + if (heroObj.healthPoints <= 0) + { + console.log(`${this.name} slayed the hero!\n`); + console.log(`hero hp: ${heroObj.healthPoints}\n`); + console.log(heroObj.destroy()); + } + else + { + console.log(`${this.name} hit the hero for ${dmg}!\n`); + console.log(`hero hp: ${heroObj.healthPoints}\n`); + } + } + else + { + console.log(`${this.name} missed the hero!\n`); + } + } + attResolve.call(this); + } +} + +class Hero extends Humanoid +{ + constructor(heroAttrs) + { + super(heroAttrs); + this.xpTot = 0; + this.level = 1; + this.strength = heroAttrs.strength; + this.agility = heroAttrs.agility; + this.intellect = heroAttrs.intellect; + this.gold = heroAttrs.gold; + this.AC = heroAttrs.AC; + this.weapons = heroAttrs.weapons; + this.limit = 0; + this.hitBon = heroAttrs.hitBon; + this.dmgDice = heroAttrs.dmgDice; + this.dmgBon = heroAttrs.dmgBon; + } + mainAttack(vlnObj) + { + let limit = this.limit; + let limitString = "" + { + let dmgCalc = function() + { + let dmg = 0; + + if(limit < 4) + { + for(let i=0; i = vlnObj.AC) + { + let dmg = dmgCalc.call(this); + vlnObj.healthPoints -= dmg; + if (vlnObj.healthPoints <= 0) + { + console.log(`${this.name} slayed the ${vlnObj.name}!\n`); + console.log(`Villain hp: ${vlnObj.healthPoints}\n`); + console.log(vlnObj.destroy()); + } + else + { + console.log(`${this.name} hit the ${vlnObj.name} ${limitString}for ${dmg}!\n`); + console.log(`Villain hp: ${vlnObj.healthPoints}\n`); + } + } + else + { + console.log(`${this.name} missed the ${vlnObj.name}!\n`); + } + } + attResolve.call(this); + this.limit = limit; + } + } +} + + + +let dragon = new Villain( + { + createdAt: new Date(), + dimensions: { + length: 5, + width: 3, + height: 4 + }, + healthPoints: 150, + name: "Dragon", + team: "Dragonkin", + weapons: ["Claw", "Bite", "Breath"], + hitBon: 8, + dmgDice: 2, + dmgBon: 3, + AC: 14, + language: "Draconic", + xp: 1000 + } +); + +let greenTooth = new Hero( +{ + createdAt: new Date(), + strength: 5, + agility: 3, + intellect: 2, + gold: 0, + AC: 16, + dimensions: + { + length: 1, + width: 2, + height: 4 + }, + healthPoints: 35, + name: "Greentooth", + team: "Small Town Bumpkins", + weapons: ["Dagger", "Fist"], + language: "Common", + hitBon: 9, + dmgDice: 5, + dmgBon: 5 +}); + + + +let greenDragToggle = 0; +function fight() +{ + console.log("\n\n*******************************\n Greentooth Vs Dragon!\n\n"); + let attWait = setInterval(() => + { + if (greenTooth.healthPoints > 0 && dragon.healthPoints > 0) + { + if (greenDragToggle === 0) + { + greenTooth.mainAttack(dragon); + console.log('-----------------------\n'); + } + else + { + dragon.attack(greenTooth); + console.log('-----------------------\n'); + } + greenDragToggle === 0 ? greenDragToggle = 1 : greenDragToggle = 0; + } + else + { + if (greenTooth.healthPoints <= 0) {console.log("Game Over");} + else {console.log("Victory!");} + clearInterval(attWait); + } + }, 1000); +} +fight(); \ No newline at end of file From c55c29cde42c3d2d960710156142e5cf31e99863 Mon Sep 17 00:00:00 2001 From: Eli Sacks Date: Wed, 12 Jun 2019 15:25:41 -0400 Subject: [PATCH 2/2] MVP + Stretch complete --- assignments/lambda-classes.js | 149 ++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) diff --git a/assignments/lambda-classes.js b/assignments/lambda-classes.js index 71acfca0e..f47dd5b44 100644 --- a/assignments/lambda-classes.js +++ b/assignments/lambda-classes.js @@ -1 +1,150 @@ // CODE here for your Lambda Classes + +class Person +{ + constructor(personAttrs) + { + this.name = personAttrs.name; + this.age = personAttrs.age; + this.location = personAttrs.location; + } + speak() {console.log(`Hello, my name is ${this.name}, I am from ${this.location}.`);} +} + +class Instructor extends Person +{ + constructor(instAttrs) + { + super(instAttrs); + this.specialty = instAttrs.specialty; + this.favLanguage = instAttrs.favLanguage; + this.catchPhrase = instAttrs.catchPhrase; + } + demo(subject) {console.log(`Today we are learning about ${subject}`);} + grade(stdntObj, subject) {console.log(`${stdntObj.name} receives a perfect score on ${subject}`);} + assignGrade(stdntObj) + { + stdntObj.grade += Math.round(Math.random()*20) - 10; + if (stdntObj.grade > 100) {stdntObj.grade = 100} + if(stdntObj.grade < 0){stdntObj.grade=0}} +} + +class Student extends Person +{ + constructor(stndtAttrs) + { + super(stndtAttrs); + this.previousBackground = stndtAttrs.previousBackground; + this.className = stndtAttrs.className; + this.favSubjects = stndtAttrs.favSubjects; + this.grade = stndtAttrs.grade; + } + litstSubjects() {this.favSubjects.forEach(elem => console.log(elem));} + PRAssignment(subject) {console.log(`${this.name} has submitted a PR for ${subject}`);} + sprintChallenge(subject) {console.log(`${this.name} has begun a sprint challenge on ${subject}`);} + announceGrade() {console.log(`${this.name} has grade: ${this.grade}`);} + graduate() + { + while (this.grade < 70) + { + this.announceGrade(); + brock.assignGrade(this); + } + console.log(`${this.name} may graduate!`); + } +} + +class ProjectManager extends Instructor +{ + constructor(pjmAttrs) + { + super(pjmAttrs); + this.gradClassName = pjmAttrs.gradClassName; + this.favInstructor = pjmAttrs.favInstructor; + } + standUp(slckChan) {console.log(`${this.name} announces to ${slckChan}, @channel standy times!`);} + debugsCode(stdntObj, subject) {console.log(`${this.name} debugs ${stdntObj.name}'s code on ${subject}`);} +} + +const dan = new Instructor +({ + name: 'Dan', + location: 'Denver', + age: 30, + favLanguage: 'javaScript', + specialty: 'node', + catchPhrase: 'thread your answers' +}); + +const josh = new Instructor +({ + name: 'Josh', + location: 'Ontario', + age: 38, + favLanguage: 'PHP', + specialty: 'Back-End', + catchPhrase: `Stay on topic` +}); + +const brock = new ProjectManager +({ + name: 'Brock', + location: 'Flo-rida', + age: 35, + specialty: 'React', + favLanguage: 'go', + catchPhrase: "It's all good", + gradClassName: 'WEB10', + favInstructor: 'Dan' +}) + +const marguel = new ProjectManager +({ + name: 'Marguel', + location: 'Wisconsin', + age: 28, + specialty: 'CSS', + favLanguage: 'C++', + catchPhrase: "Hello there", + gradClassName: 'WEB12', + favInstructor: 'Josh' +}) + +const eli = new Student +({ + name: "Eli", + age: 37, + location: "Ohio", + previousBackground: "Made a video game, did some research", + className: "Web21", + favSubjects: ['JavaScript', 'CSS', 'HTML'], + // grade: Math.round(Math.random()*100) + grade: Math.round(Math.random()*100) +}); + +const guy = new Student +({ + name: "Guy", + age: 30, + location: "Earth", + previousBackground: "Welded stuff", + className: "Web21", + favSubjects: ['Flexboxes', 'React', 'Javascript'], + grade: Math.round(Math.random()*100) +}); + +console.log('\n'); +brock.standUp('web21_brock'); +marguel.debugsCode(guy, 'React'); +eli.litstSubjects(); +eli.PRAssignment('Javascript'); +guy.sprintChallenge('CSS'); +dan.demo('Class Inheritance'); +josh.grade(eli, 'HTML'); +// Stretch +eli.announceGrade(); +dan.assignGrade(eli); +eli.announceGrade(); +brock.assignGrade(eli); +eli.announceGrade(); +eli.graduate(); \ No newline at end of file