Man O War
function war(arr) {
let pirateShipStatus = arr.shift().split(">").map(Number);
let warshipStatus = arr.shift().split(">").map(Number);
let hpCapacity = Number(arr.shift());
let command = arr.shift();
let count = 0;
while (command != "Retire") {
let tokens = command.split(" ");
let action = tokens[0];
if (action == "Fire") {
let idx = Number(tokens[1]);
let power = Number(tokens[2]);
if (idx >= 0 && idx < warshipStatus.length) {
let attack = (warshipStatus[idx] -= power);
if (attack > 0) {
warshipStatus[idx] = attack;
} else {
console.log("You won! The enemy ship has sunken.");
return;
}
}
} else if (action == "Defend") {
let startIdx = Number(tokens[1]);
let endIdx = Number(tokens[2]);
let damage = Number(tokens[3]);
if (
(startIdx >= 0 &&
startIdx < pirateShipStatus.length) &&
(endIdx >= startIdx &&
endIdx < pirateShipStatus.length)
) {
for (let i = startIdx; i <= endIdx; i++) {
pirateShipStatus[i] -= damage;
if (pirateShipStatus[i] <= 0) {
console.log("You lost! The pirate ship has sunken.");
return;
}
}
}
} else if (action == "Repair") {
let idx = Number(tokens[1]);
let givenHp = Number(tokens[2]);
if (idx >= 0 && idx < pirateShipStatus.length) {
let hp = (pirateShipStatus[idx] += givenHp);
if (hp > hpCapacity) {
hp = hpCapacity;
}
pirateShipStatus[idx] = hp;
}
} else if (action == "Status") {
for (let i = 0; i < pirateShipStatus.length; i++) {
if (pirateShipStatus[i] < hpCapacity * 0.2) {
count++;
}
}
console.log(`${count} sections need repair.`);
}
command = arr.shift();
}
let sum1 = 0;
let sum2 = 0;
for(let el of pirateShipStatus){
sum1 += el;
}
for(let el of warshipStatus){
sum2 += el;
}
console.log(`Pirate ship status: ${sum1}`);
console.log(`Warship status: ${sum2}`);
}
Получаван 80 от 100 в judge - защо?
https://judge.softuni.org/Contests/Practice/Index/1773#2