Advertisement
vladovip

ManOnWar V1 JS FUND MIDEX PREP

Aug 19th, 2022
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function manOnWar(inputArr) {
  2.   let pirateShipStatus = inputArr.shift().split(">"); // "12>13>11>20>66",  ->  [ '12', '13', '11', '20', '66' ]
  3.   let warShipStatus = inputArr.shift().split(">"); //  "12>22>33>44>55>32>18",
  4.  
  5.   let maximHealhPerSection = Number(inputArr.shift()); // "70",
  6.   let commandLineInfo = inputArr.shift();
  7.  
  8.   while (commandLineInfo !== "Retire") {
  9.  
  10.     let tokens = commandLineInfo.split(" ");
  11.     let command = tokens[0];
  12.     let param1 = tokens[1];
  13.     let param2 = tokens[2];
  14.     let param3 = tokens[3];
  15.  
  16.     if (command === "Fire") {
  17.       //    "Fire {index} {damage}"
  18.       //     "Fire 2 11",
  19.       let attackIndex = Number(param1);
  20.       let damage = Number(param2);
  21.       if (attackIndex >= 0 && attackIndex < warShipStatus.length) {
  22.         warShipStatus[attackIndex] -= damage;
  23.         if (warShipStatus[attackIndex] <= 0) {
  24.           console.log(`"You won! The enemy ship has sunken.`);
  25.          break;
  26.        }
  27.      }
  28.    }
  29.  
  30.    if (command == "Defend") {
  31.      // "Defend {startIndex} {endIndex} {damage}"
  32.      // "Defend 3 6 11",
  33.      let startindex = Number(param1);
  34.      let endIndex = Number(param2);
  35.      let damage = Number(param3);
  36.      if (
  37.        startindex >= 0 &&
  38.        startindex < pirateShipStatus.length &&
  39.        endIndex >= 0 &&
  40.        endIndex < pirateShipStatus.length &&
  41.        startindex <= endIndex
  42.      ) {
  43.        for (let i = startindex; i <= endIndex; i++) {
  44.          pirateShipStatus[i] -= damage;
  45.        }
  46.      }
  47.      for (let el of pirateShipStatus) {
  48.        if (el <= 0) {
  49.          console.log(`You lost! The pirate ship has sunken.`);
  50.          break;
  51.        }
  52.      }
  53.    }
  54.  
  55.    if (command === "Repair") {
  56.      //  "Repair 1 33",
  57.      //  "Repair {index} {health}"
  58.      let index = Number(param1);
  59.      let health = Number(param2);
  60.      if (index >= 0 && index < pirateShipStatus.length) {
  61.        pirateShipStatus[index] += health;
  62.        if (pirateShipStatus[index] >= maximHealhPerSection) {
  63.          pirateShipStatus[index] = maximHealhPerSection;
  64.        }
  65.      }
  66.    }
  67.  
  68.    if (command == "Status") {
  69.      let counterNeedRepair = 0;
  70.      for (let el of pirateShipStatus) {
  71.        if (el < 0.2 * maximHealhPerSection) {
  72.          counterNeedRepair++;
  73.        }
  74.      }
  75.      console.log(`${counterNeedRepair} sections need repair.`);
  76.    }
  77.  
  78.    commandLineInfo = inputArr.shift();
  79.  }
  80.  
  81.  let pirateSum = 0;
  82.  let warShipSum = 0;
  83.  for (let el of pirateShipStatus) {
  84.    pirateSum += Number(el);
  85.  }
  86.  for (let section of warShipStatus) {
  87.    warShipSum += Number(section);
  88.  }
  89.  
  90.  if (warShipSum > 0 && pirateSum > 0) {
  91.    console.log(`Pirate ship status: ${pirateSum}`);
  92.    console.log(`Warship status: ${warShipSum}`);
  93.  }
  94. }
  95.  
  96.  
  97.  
  98. manOnWar([
  99.  "12>13>11>20>66",
  100.  "12>22>33>44>55>32>18",
  101.  "70",
  102.  "Fire 2 11",
  103.  "Fire 8 100",
  104.  "Defend 3 6 11",
  105.  "Defend 0 3 5",
  106.  "Repair 1 33",
  107.  "Status",
  108.  "Retire",
  109. ]);
  110.  
  111. console.log(`*******`);
  112.  
  113. manOnWar([
  114.  "2>3>4>5>2",
  115.  "6>7>8>9>10>11",
  116.  "20",
  117.  "Status",
  118.  "Fire 2 3",
  119.  "Defend 0 4 11",
  120.  "Repair 3 18",
  121.  "Retire",
  122. ]);
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement