Advertisement
Guest User

Untitled

a guest
May 23rd, 2020
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function manOwar(input = []) {
  2.  
  3.   let pirateShip = [];
  4.   pirateShip = input.shift().split(`>`).map(Number);
  5.   let warShip = [];
  6.   warShip = input.shift().split(`>`).map(Number);
  7.   let maxHealth = Number(input.shift());
  8.   let token = input.shift();
  9.   let isDead = false;
  10.  
  11.   while (token != `Retire`) {
  12.     if (isDead === true) {
  13.       break;
  14.     }
  15.     let splittedToken = token.split(` `);
  16.     let comand = splittedToken[0];
  17.     let indexOne = splittedToken[1];
  18.     let indexTwo = splittedToken[2];
  19.     let indexThree = splittedToken[3];
  20.  
  21.     if (comand === `Fire`) {
  22.       fire(indexOne, indexTwo);
  23.     } else if (comand === `Defend`) {
  24.       defend(indexOne, indexTwo, indexThree);
  25.     } else if (comand === `Repair`) {
  26.       repair(indexOne, indexTwo);
  27.     } else if (comand === `Status`) {
  28.       status();
  29.     }
  30.  
  31.     token = input.shift();
  32.   }
  33.  
  34.   if (!isDead) {
  35.     let warShipPoint = warShip.reduce((acc, el) => acc + el, 0);
  36.     let pirateShipPoints = pirateShip.reduce((acc, el) => acc + el, 0);
  37.  
  38.     console.log(`Pirate ship status: ${pirateShipPoints}`);
  39.     console.log(`Warship status: ${warShipPoint}`);
  40.   }
  41.  
  42.   function fire(index, damage) {
  43.     index = Number(index);
  44.     damage = Number(damage);
  45.     if (index >= 0 && index < warShip.length) {
  46.       warShip[index] = warShip[index] - damage;
  47.       if (warShip[index] <= 0) {
  48.         isDead = true;
  49.         console.log(`You won! The enemy ship has sunken.`);
  50.         return;
  51.       }
  52.     }
  53.   }
  54.  
  55.   function defend(startIndex, endIndex, damage) {
  56.     startIndex = Number(startIndex);
  57.     endIndex = Number(endIndex);
  58.     damage = Number(damage);
  59.     if ((startIndex >= 0 && startIndex < pirateShip.length) && (endIndex >= startIndex && endIndex <= pirateShip.length - 1)) {
  60.       for (let i = startIndex; i <= endIndex; i++) {
  61.         pirateShip[i] -= damage;
  62.         if (pirateShip[i] <= 0) {
  63.           isDead = true;
  64.           console.log(`You lost! The pirate ship has sunken.`);
  65.           return;
  66.         }
  67.       }
  68.     }
  69.   }
  70.  
  71.   function repair(index, health) {
  72.     index = Number(index);
  73.     health = Number(health);
  74.     if (index >= 0 && index < pirateShip.length) {
  75.       if (pirateShip[index] + health < maxHealth) {
  76.         pirateShip[index] = pirateShip[index] + health;
  77.       } else {
  78.         pirateShip[index] = maxHealth;
  79.       }
  80.  
  81.     }
  82.   }
  83.  
  84.   function status() {
  85.     let lessThan20Perc = maxHealth * 0.2;
  86.     let coundSections = 0;
  87.     for (let i = 0; i < pirateShip.length; i++) {
  88.       if (pirateShip[i] < lessThan20Perc) {
  89.         coundSections++;
  90.       }
  91.     }
  92.     console.log(`${coundSections} sections need repair.`);
  93.  
  94.   }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement