Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function thePirates(input){
- let sailHasPassed = 0;
- function createCity(cityName, population, gold){
- let city = {
- name: cityName,
- population: population,
- gold: gold,
- plunder(town, people, gold){
- let buffPeople = this.population - people;
- let buffGold = this.gold - gold;
- if (buffGold <= 0 || buffPeople <= 0 || buffGold <= 0 && buffPeople <= 0){
- console.log(`${town} plundered! ${gold} gold stolen, ${people} citizens killed.`);
- console.log(`${town} has been wiped off the map!`);
- map.delete(this.name);
- } else {
- this.population -= people;
- this.gold -= gold;
- console.log(`${town} plundered! ${gold} gold stolen, ${people} citizens killed.`);
- }
- },
- propser(town, gold){
- if (gold < 0){
- console.log("Gold added cannot be a negative number!");
- } else {
- this.gold += gold;
- console.log(`${gold} gold added to the city treasury. ${town} now has ${this.gold} gold.`);
- }
- },
- }
- return city;
- }
- let indexOfSail = input.indexOf('Sail');
- let indexOFEnd = input.indexOf('End');
- let map = new Map();
- for (let i = 0; i < indexOfSail; i++){
- let tokens= input[i].split('||');
- let cityName = tokens[0];
- let population = Number(tokens[1]);
- let gold = Number(tokens[2]);
- let city = createCity(cityName, population, gold);
- if (map.has(cityName)){
- let city = map.get(cityName);
- city.population += population;
- city.gold += gold;
- } else{
- map.set(cityName, city);
- }
- }
- for (let i = indexOfSail + 1; i <= indexOFEnd; i++){
- let tokens = input[i].split('=>');
- let command = tokens.shift();
- let name = tokens.shift();
- let values = tokens;
- switch(command){
- case 'Plunder':{
- let city = map.get(name);
- let people = Number(values[0]);
- let gold = Number(values[1]);
- city.plunder(name, people, gold);
- break;
- }
- case 'Prosper':{
- let city = map.get(name);
- let gold = Number(values);
- city.propser(name, gold);
- break;
- }
- case 'End':
- console.log(`Ahoy, Captain! There are ${map.size} wealthy settlements to go to: `);
- for(let city of map){
- console.log(`${city[1].name} -> Population: ${city[1].population} citizens, Gold: ${city[1].gold} kg`)
- }
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement