Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- function heroes(arr) {
- //Define a hero
- class Hero {
- constructor(name, hp, mp) {
- this.name = name;
- this.health = hp;
- this.mana = mp;
- }
- CastSpell = castSpell;
- TakeDamage = takeDamage;
- Recharge = recharge;
- Heal = heal;
- }
- //Define variables and constants
- let partyMembersNumber = Number(arr.shift());
- const MAX_MANA = 200;
- const MAX_HEALTH = 100;
- const partyMembers = {};
- //Create new party members
- let index;
- let l = partyMembersNumber;
- for (index = 0; index < l; index++) {
- let [heroName, hp, mp] = arr[index].split(' ');
- hp = Number(hp);
- mp = Number(mp);
- let newHero = new Hero(heroName, hp, mp);
- partyMembers[heroName] = newHero;
- }
- //Logic
- let input = arr[index];
- while (input != 'End') {
- //Get command
- let [spell, heroName, ...args] = arr[index].split(' - ');
- partyMembers[heroName][spell](args);
- //Delete dead members
- if (partyMembers[heroName].health <= 0) {
- delete partyMembers[heroName];
- }
- //Get next input
- index++;
- input = arr[index];
- }
- //Sort and output
- const keys = Object.keys(partyMembers);
- keys
- .sort((nameA, nameB) => {
- return nameA.localeCompare(nameB);
- })
- .sort((keyA, keyB) => {
- return partyMembers[keyB].health - partyMembers[keyA].health;
- });
- const output = [];
- keys.forEach((key) => {
- let name = key;
- let hp = ' HP: ' + partyMembers[key].health;
- let mp = ' MP: ' + partyMembers[key].mana;
- output.push(name, hp, mp);
- });
- console.log(output.join('\n'));
- //Declarations
- function castSpell(params) {
- let [manaNeeded, spell] = params;
- manaNeeded = Number(manaNeeded);
- if (this.mana >= manaNeeded) {
- //Casts the spell
- this.mana -= manaNeeded;
- console.log(
- `${this.name} has successfully cast ${spell} and now has ${this.mana} MP!`
- );
- return;
- }
- console.log(`${this.name} does not have enough MP to cast ${spell}!`);
- }
- function takeDamage(params) {
- let [damage, attacker] = params;
- damage = Number(damage);
- this.health -= damage;
- if (this.health > 0) {
- console.log(
- `${this.name} was hit for ${damage} HP by ${attacker} and now has ${this.health} HP left!`
- );
- return;
- }
- console.log(`${this.name} has been killed by ${attacker}!`);
- }
- function recharge(params) {
- let recharged = params;
- recharged = Number(recharged);
- let oldMana = this.mana;
- this.mana =
- this.mana + recharged > MAX_MANA ? MAX_MANA : this.mana + recharged;
- console.log(`${this.name} recharged for ${this.mana - oldMana} MP!`);
- }
- function heal(params) {
- let [healed] = params;
- healed = Number(healed);
- let oldHealth = this.health;
- this.health =
- this.health + healed > MAX_HEALTH ? MAX_HEALTH : this.health + healed;
- console.log(`${this.name} healed for ${this.health - oldHealth} HP!`);
- }
- }
- let result = heroes([
- '2',
- 'Solmyr 85 120',
- 'Kyrre 99 50',
- 'Heal - Solmyr - 10',
- 'Recharge - Solmyr - 50',
- 'TakeDamage - Kyrre - 66 - Orc',
- 'CastSpell - Kyrre - 15 - ViewEarth',
- 'End',
- ]);
- console.log(result);
Add Comment
Please, Sign In to add comment