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());
- for (let row of arr) {
- 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') {
- let index = Number(args[0]);
- let damage = Number(args[1]);
- 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.`);
- break;
- }
- }
- } else if (command == 'Defend') {
- let startIndex = Number(args[0]);
- let endIndex = Number(args[1]);
- let damage = Number(args[2])
- if (startIndex < pirateShip.length && startIndex > -1 && endIndex < pirateShip.length && endIndex > -1) {
- for (let i = startIndex; i <= endIndex; i++) pirateShip[i] = Math.max((pirateShip[i] - 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.`);
- break;
- }
- }
- } else if (command == 'Repair') {
- let index = Number(args[0]);
- let health = Number(args[1])
- if (index < pirateShip.length && index > -1) pirateShip[index] = Math.min((pirateShip[index] + health), maximumHC);
- } else if (command == 'Status') {
- console.log(`${pirateShip.filter(x => x < (maximumHC * 0.2)).length} sections need repair.`);
- }
- }
- }
Add Comment
Please, Sign In to add comment