dilyana2001

Untitled

Jul 9th, 2021 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function manOWar(arr) {
  2.     let pirateShip = arr.shift().split('>').map(Number);
  3.     let warShip = arr.shift().split('>').map(Number);
  4.     let maximumHC = Number(arr.shift());
  5.  
  6.     for (let row of arr) {
  7.         let [command, ...args] = row.split(' ');
  8.         if (command == 'Retire') {
  9.             console.log(`Pirate ship status: ${pirateShip.reduce((a, b) => a + b, 0)}`);
  10.             console.log(`Warship status: ${warShip.reduce((a, b) => a + b, 0)}`);
  11.         } else if (command == 'Fire') {
  12.             let index = Number(args[0]);
  13.             let damage = Number(args[1]);
  14.             if (index < warShip.length && index > -1) {
  15.                 if (warShip[index] - damage > 0) warShip.splice(index, 1, warShip[index] - damage);
  16.                 else {
  17.                     warShip[index] = 0;
  18.                     console.log(`You won! The enemy ship has sunken.`);
  19.                     break;
  20.                 }
  21.             }
  22.         } else if (command == 'Defend') {
  23.             let startIndex = Number(args[0]);
  24.             let endIndex = Number(args[1]);
  25.             let damage = Number(args[2])
  26.             if (startIndex < pirateShip.length && startIndex > -1 && endIndex < pirateShip.length && endIndex > -1) {
  27.                 for (let i = startIndex; i <= endIndex; i++) pirateShip[i] = Math.max((pirateShip[i] - damage), 0);
  28.                 let setted = Array.from(new Set(pirateShip)).filter(x => x > 0);
  29.                 if (setted.length == 0) {
  30.                     console.log(`You lost! The pirate ship has sunken.`);
  31.                     break;
  32.                 }
  33.             }
  34.         } else if (command == 'Repair') {
  35.             let index = Number(args[0]);
  36.             let health = Number(args[1])
  37.             if (index < pirateShip.length && index > -1) pirateShip[index] = Math.min((pirateShip[index] + health), maximumHC);
  38.         } else if (command == 'Status') {
  39.             console.log(`${pirateShip.filter(x => x < (maximumHC * 0.2)).length} sections need repair.`);
  40.         }
  41.     }
  42. }
Add Comment
Please, Sign In to add comment