Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function manOWar(arr) {
- let pirateShip = arr.shift().split('>').map(Number);
- let warShip = arr.shift().split('>').map(Number);
- let maximumHC = Number(arr.shift());
- arr.forEach(row => {
- let [command, ...args] = row.split(' ');
- if (command == 'Retire') {
- console.log(`Pirate ship status: ${pirateShip.reduce((a, b) => a + b, 0)}`);
- console.log(`Warship status: ${warShip.reduce((a, b) => a + b, 0)}`);
- } else if (command == 'Fire') fire(...args);
- else if (command == 'Defend') defend(...args);
- else if (command == 'Repair') repair(...args);
- else if (command == 'Status') console.log(`${pirateShip.filter(x => x < (maximumHC * 0.2)).length} sections need repair.`);
- })
- function fire(index, damage) {
- index = Number(index);
- damage = Number(damage);
- if (index < warShip.length && index > -1) {
- if (warShip[index] - damage > 0) warShip.splice(index, 1, warShip[index] - damage);
- else {
- warShip[index] = 0;
- console.log(`You won! The enemy ship has sunken.`);
- }
- }
- }
- function defend(startIndex, endIndex, damage) {
- startIndex = Number(startIndex);
- endIndex = Number(endIndex);
- if (startIndex < pirateShip.length && startIndex > -1 && endIndex < pirateShip.length && endIndex > -1) {
- for (let i = startIndex; i <= endIndex; i++) pirateShip[i] = Math.max((pirateShip[i] - Number(damage)), 0);
- let setted = Array.from(new Set(pirateShip)).filter(x => x > 0);
- if (setted.length == 0) console.log(`You lost! The pirate ship has sunken.`);
- }
- }
- function repair(index, health) {
- index = Number(index);
- if (index < pirateShip.length && index > -1) pirateShip[index] = Math.min((pirateShip[index] + Number(health)), maximumHC);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement