-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07.pirates.js
More file actions
102 lines (81 loc) · 2.51 KB
/
07.pirates.js
File metadata and controls
102 lines (81 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
function pirates(input) {
let cities = {};
let lineOfInput = input.shift();
while (lineOfInput !== "Sail") {
let [cityName, population, gold] = lineOfInput.split("||");
if (!cities.hasOwnProperty(cityName)) {
cities[cityName] = [Number(population), Number(gold)];
} else {
cities[cityName][0] += Number(population);
cities[cityName][1] += Number(gold);
}
lineOfInput = input.shift();
}
lineOfInput = input.shift();
while (lineOfInput !== "End") {
let [command, cityName, population, gold] = lineOfInput.split("=>");
if (command == "Plunder") {
cities[cityName][0] -= Number(population);
cities[cityName][1] -= Number(gold);
console.log(
`${cityName} plundered! ${gold} gold stolen, ${population} citizens killed.`
);
if (cities[cityName][0] <= 0 || cities[cityName][1] <= 0) {
delete cities[cityName];
console.log(`${cityName} has been wiped off the map!`);
}
} else if (command == "Prosper") {
let prosperGold = Number(population);
if (prosperGold < 0) {
console.log("Gold added cannot be a negative number!");
} else {
cities[cityName][1] += prosperGold;
console.log(
`${prosperGold} gold added to the city treasury. ${cityName} now has ${cities[cityName][1]} gold.`
);
}
}
lineOfInput = input.shift();
}
if (lineOfInput == "End") {
if (Object.keys(cities).length == 0) {
console.log(
"Ahoy, Captain! All targets have been plundered and destroyed!"
);
} else {
console.log(
`Ahoy, Captain! There are ${
Object.keys(cities).length
} wealthy settlements to go to:`
);
for (let city of Object.keys(cities)) {
console.log(
`${city} -> Population: ${cities[city][0]} citizens, Gold: ${cities[city][1]} kg`
);
}
}
}
}
pirates([
"Nassau||95000||1000",
"San Juan||930000||1250",
"Campeche||270000||690",
"Port Royal||320000||1000",
"Port Royal||100000||2000",
"Sail",
"Prosper=>Port Royal=>-200",
"Plunder=>Nassau=>94000=>750",
"Plunder=>Nassau=>1000=>150",
"Plunder=>Campeche=>150000=>690",
"End",
]);
console.log("====================");
pirates([
"Tortuga||345000||1250",
"Santo Domingo||240000||630",
"Havana||410000||1100",
"Sail",
"Plunder=>Tortuga=>75000=>380",
"Prosper=>Santo Domingo=>180",
"End",
]);