Advertisement
Guest User

manOwar

a guest
May 7th, 2020
299
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.             return;
  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.  
  32.  
  33.         token = input.shift();
  34.     }
  35.  
  36.     let warShipPoint = warShip.reduce((acc,el) => acc + el,0)
  37.     let pirateShipPoints = pirateShip.reduce((acc,el) => acc + el,0)
  38.  
  39.     console.log(`Pirate ship status: ${pirateShipPoints}`)
  40.     console.log(`Warship status: ${warShipPoint}`)
  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.     }
  85.  
  86.     function status() {
  87.         let lessThan20Perc = maxHealth * 0.2
  88.         let coundSections = 0;
  89.         for(let i=0;i<pirateShip.length;i++) {
  90.             if(pirateShip[i]<lessThan20Perc) {
  91.                 coundSections++;
  92.             }
  93.         }
  94.         console.log(`${coundSections} sections need repair.`)
  95.  
  96.     }
  97.  
  98.  
  99. }
  100. manOwar([
  101.     '12>13>11>20>66',
  102.     '12>22>33>44>55>32>18',
  103.     '70',
  104.     'Fire 2 11',
  105.     'Fire 8 100',
  106.     'Defend 3 6 11',
  107.     'Defend 0 3 5',
  108.     'Repair 1 33',
  109.     'Status',
  110.     'Retire'
  111.   ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement