Advertisement
didkoslawow

Untitled

Apr 1st, 2023
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function heroesOfCodeAndLogic(input) {
  2.    const initNumber = Number(input.shift());
  3.    const players = {};
  4.  
  5.    input.splice(0, initNumber).forEach((p) => {
  6.      const [player, hp, mp] = p.split(' ');
  7.  
  8.      players[player] = {
  9.        hp: Number(hp),
  10.        mp: Number(mp),
  11.      };
  12.    });
  13.  
  14.    let line = input.shift();
  15.  
  16.    while (line !== 'End') {
  17.      const [command, heroName, ...params] = line.split(' - ');
  18.  
  19.      switch (command) {
  20.        case 'CastSpell':
  21.          const [mp, spellName] = params;
  22.          
  23.          if(players[heroName].mp - Number(mp) >= 0) {
  24.              players[heroName].mp -= Number(mp);
  25.              console.log(`${heroName} has successfully cast ${spellName} and now has ${players[heroName].mp} MP!`);
  26.          } else {
  27.              console.log(`${heroName} does not have enough MP to cast ${spellName}!`);
  28.          }
  29.          break;
  30.        case 'TakeDamage':
  31.          const [damage, attacker] = params;
  32.          players[heroName].hp -= Number(damage);
  33.  
  34.          if (players[heroName].hp > 0) {
  35.              console.log(`${heroName} was hit for ${damage} HP by ${attacker} and now has ${players[heroName].hp} HP left!`);
  36.          } else {
  37.              console.log(`${heroName} has been killed by ${attacker}!`);
  38.              delete players[heroName];
  39.          }
  40.          break;
  41.        case 'Recharge':
  42.          const [amount] = params;
  43.  
  44.          if (players[heroName].mp + Number(amount) > 200) {
  45.              const rechargedAmount = 200 - players[heroName].mp;
  46.              players[heroName].mp = 200;
  47.              console.log(`${heroName} recharged for ${rechargedAmount} MP!`);
  48.          } else {
  49.              players[heroName].mp += Number(amount)
  50.              console.log(`${heroName} recharged for ${Number(amount)} MP!`);
  51.          }
  52.          break;
  53.        case 'Heal':
  54.          const [health] = params;
  55.  
  56.          if (players[heroName].hp + Number(health) > 100) {
  57.              const healedAmount = 100 - players[heroName].hp;
  58.              players[heroName].hp = 100;
  59.              console.log(`${heroName} healed for ${healedAmount} HP!`);
  60.          } else {
  61.              players[heroName].hp += Number(health)
  62.              console.log(`${heroName} healed for ${Number(health)} HP!`);
  63.          }
  64.          break;
  65.      }
  66.  
  67.      line = input.shift();
  68.    }
  69.  
  70.    for (const hero in players) {
  71.      console.log(`${hero}
  72.    HP: ${players[hero].hp}
  73.    MP: ${players[hero].mp}`);
  74.    }
  75.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement