Advertisement
PowerCell46

Man-O-War JS

Dec 9th, 2022
1,038
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function manOfWar(array) {
  2.     let pirateShipArray = (array[0]).split(">");
  3.     let warshipArray = (array[1]).split(">");
  4.     warshipArray = warshipArray.map(x => Number(x));
  5.     pirateShipArray = pirateShipArray.map(x => Number(x));
  6.  
  7.     let maximumHealthCapacity = Number(array[2]);
  8.     let index = 3;
  9.     let currentInput = array[index];
  10.     let theProgramMustBeStopped = false;
  11.  
  12.     while (currentInput !== "Retire" && theProgramMustBeStopped === false) {
  13.         currentInput = currentInput.split(" ");
  14.         let currentCommand = currentInput.shift();
  15.  
  16.         switch (currentCommand) {
  17.  
  18.             case "Fire":
  19.                 let fireIndex = Number(currentInput[0]);
  20.                 let fireDamage = Number(currentInput[1]);
  21.                 if (fireIndex >= 0 && fireIndex < Number(warshipArray.length)) {
  22.                     let fireHealth = warshipArray[fireIndex] - fireDamage;
  23.                     if (fireHealth <= 0) {
  24.                         console.log("You won! The enemy ship has sunken.");
  25.                         theProgramMustBeStopped = true;
  26.                         break;
  27.                     }
  28.                     else {
  29.                         warshipArray.splice(fireIndex, 1, fireHealth);
  30.                     }
  31.                 } break;
  32.  
  33.             case "Defend":
  34.                 let startIndex = Number(currentInput[0]);
  35.                 let endIndex = Number(currentInput[1]);
  36.                 let defendDamage = Number(currentInput[2]);
  37.                 if (startIndex >= 0 && startIndex < Number(pirateShipArray.length) && endIndex >= 0 && endIndex < Number(pirateShipArray.length)) {
  38.                     while (startIndex <= endIndex) {
  39.                         let currentDefend = pirateShipArray[startIndex] - defendDamage;
  40.                         if (currentDefend <= 0) {
  41.                             console.log("You lost! The pirate ship has sunken.");
  42.                             theProgramMustBeStopped = true;
  43.                             break;
  44.                         } else {
  45.                             pirateShipArray.splice(startIndex, 1, currentDefend);
  46.                         }
  47.                         startIndex++;
  48.                     }
  49.                 } break;
  50.  
  51.             case "Repair":
  52.                 let repairIndex = Number(currentInput[0]);
  53.                 let repairHealth = Number(currentInput[1]);
  54.                 if (repairIndex >= 0 && repairIndex < Number(pirateShipArray.length)) {
  55.                     let newHealth = pirateShipArray[repairIndex] + repairHealth;
  56.                     if (newHealth > maximumHealthCapacity) {
  57.                         newHealth = maximumHealthCapacity;
  58.                     }
  59.                     pirateShipArray.splice(repairIndex, 1, newHealth);
  60.                 } break;
  61.  
  62.             case "Status":
  63.                 let minimumHealthCapacity = (maximumHealthCapacity / 100) * 20;
  64.                 let statusArray = pirateShipArray.filter(x => x < minimumHealthCapacity);
  65.                 console.log(`${statusArray.length} sections need repair.`);
  66.                 break;
  67.         }
  68.  
  69.         index++;
  70.         currentInput = array[index];
  71.     }
  72.  
  73.     if (theProgramMustBeStopped === false) {
  74.         let pirateStatus = 0;
  75.         for (let index = 0; index < Number(pirateShipArray.length); index++) {
  76.             let currentStatus = pirateShipArray[index];
  77.             pirateStatus += currentStatus;
  78.         }
  79.  
  80.         let warshipStatus = 0;
  81.         for (let index = 0; index < Number(warshipArray.length); index++) {
  82.             let currentStatus = warshipArray[index];
  83.             warshipStatus += currentStatus;
  84.         }
  85.  
  86.         console.log(`Pirate ship status: ${pirateStatus}`);
  87.         console.log(`Warship status: ${warshipStatus}`);
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement