Advertisement
Guest User

Pirates

a guest
Apr 5th, 2020
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.42 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4. using System.Collections.Generic;
  5. using System.Text;
  6.  
  7. namespace _03._Pirates
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string input = Console.ReadLine();
  14.             SortedDictionary<string, List<int>> city = new SortedDictionary<string, List<int>>();
  15.  
  16.             while (input != "Sail")
  17.             {
  18.                 string[] cities = input.Split("||").ToArray();
  19.                 string cityname = cities[0];
  20.                 int population = int.Parse(cities[1]);
  21.                 int gold = int.Parse(cities[2]);
  22.                 if (!city.ContainsKey(cityname))
  23.                 {
  24.                     city.Add(cityname, new List<int>(2));
  25.                     city[cityname].Add(population);
  26.                     city[cityname].Add(gold);
  27.  
  28.                 }
  29.                 else
  30.                 {
  31.                     city[cityname][0] += population;
  32.                     city[cityname][1] += gold;
  33.                 }
  34.                 input = Console.ReadLine();
  35.             }
  36.  
  37.             string events = Console.ReadLine();
  38.             while (events != "End")
  39.             {
  40.                 string[] command = events.Split("=>").ToArray();
  41.                 switch (command[0])
  42.                 {
  43.                     case "Plunder":
  44.                         int people = int.Parse(command[2]);
  45.                         int golds = int.Parse(command[3]);
  46.                         if (city.ContainsKey(command[1]))
  47.                         {
  48.                             city[command[1]][0] -= people;
  49.                             city[command[1]][1] -= golds;
  50.                             Console.WriteLine($"{command[1]} plundered! {golds} gold stolen, {people} citizens killed.");
  51.  
  52.                             if ((city[command[1]][0] <= 0) || (city[command[1]][1] <= 0))
  53.                             {
  54.                                 Console.WriteLine($"{command[1]} has been wiped off the map!");
  55.                                 city.Remove(command[1]);
  56.                             }
  57.                         }
  58.                         break;
  59.                     case "Prosper":
  60.                         int goldy = int.Parse(command[2]);
  61.                         if (goldy < 0)
  62.                         {
  63.                             Console.WriteLine("Gold added cannot be a negative number!");
  64.                             break;
  65.                         }
  66.                         else
  67.                         {
  68.                             city[command[1]][1] += goldy;
  69.                             Console.WriteLine($"{goldy} gold added to the city treasury. {command[1]} now has {city[command[1]][1]} gold.");
  70.                         }
  71.  
  72.                         break;
  73.                 }
  74.  
  75.                 events = Console.ReadLine();
  76.             }
  77.             if (city.Count > 0)
  78.             {
  79.                 Console.WriteLine($"Ahoy, Captain! There are {city.Count} wealthy settlements to go to:");
  80.                 foreach (var item in city.OrderByDescending(x => x.Value[1]))
  81.                 {
  82.                     Console.WriteLine($"{item.Key} -> Population: {item.Value[0]} citizens, Gold: {item.Value[1]} kg");
  83.                 }
  84.             }
  85.             else
  86.             {
  87.                 Console.WriteLine("Ahoy, Captain! All targets have been plundered and destroyed!");
  88.             }
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement