Created
July 29, 2019 04:32
-
-
Save dancomanlive/bd6f164d24a49d68002f014a9f82a383 to your computer and use it in GitHub Desktop.
Pokemon
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
class Pokemon { | |
constructor(_pokemon) { | |
this.name = _pokemon.name || 'unknown'; | |
this.power = _pokemon.power || 1; | |
this.attack = _pokemon.attack || 1; | |
this.defense = _pokemon.defense || 1; | |
} | |
toString() { | |
return `${this.name} - power: ${this.power}; attack: ${ | |
this.attack | |
}; defense: ${this.defense}`; | |
} | |
calculateMultiplier() { | |
//Step 1 - Common | |
return (1 / 2) * this.power * Math.random(); | |
} | |
showDamage(damage) { | |
// Step 3 - Common | |
console.log('Pokemon damage is:', damage); | |
} | |
calculateDamage() { | |
const multipliers = this.calculateMultiplier(); //Step 1; | |
const damage = this.calculateImpact(multipliers); //Step 2; | |
this.showDamage(damage); //Step 3; | |
} | |
} | |
// ********************************************* | |
class FightingPokemon extends Pokemon { | |
constructor(_pokemon) { | |
super(_pokemon); | |
} | |
calculateImpact(multipliers) { | |
return Math.floor((this.attack / this.defense) * multipliers) + 1; | |
} | |
} | |
class PoisonPokemon extends Pokemon { | |
constructor(_pokemon) { | |
super(_pokemon); | |
} | |
calculateImpact(multipliers) { | |
return Math.floor((this.attack - this.defense) * multipliers) + 1; | |
} | |
} | |
class GroundPokemon extends Pokemon { | |
constructor(_pokemon) { | |
super(_pokemon); | |
} | |
calculateImpact(multipliers) { | |
return Math.floor((this.attack + this.defense) * multipliers) + 1; | |
} | |
} | |
// ************************************************** | |
const passimian = new FightingPokemon({ | |
name: 'Passimian', | |
attack: 10, | |
power: 10, | |
defense: 10 | |
}); | |
console.log(passimian.toString()); | |
passimian.calculateDamage(); | |
const poipole = new PoisonPokemon({ | |
name: 'Poipole', | |
attack: 10, | |
power: 10, | |
defense: 10 | |
}); | |
console.log(poipole.toString()); | |
poipole.calculateDamage(); | |
const mudsdale = new GroundPokemon({ | |
name: 'Mudsdale', | |
attack: 10, | |
power: 10, | |
defense: 10 | |
}); | |
console.log(mudsdale.toString()); | |
mudsdale.calculateDamage(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment