Advertisement
yanchevilian

P!rates

May 11th, 2021
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace P_rates
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string command = Console.ReadLine();
  12.             Dictionary<string, List<int>> allCities = new Dictionary<string, List<int>>();
  13.  
  14.             while (command.ToLower() != "sail")
  15.             {
  16.                 string[] citiesArray = command.Split("||", StringSplitOptions.RemoveEmptyEntries);
  17.                 string currentCity = citiesArray[0];
  18.                 int population = int.Parse(citiesArray[1]);
  19.                 int gold = int.Parse(citiesArray[2]);
  20.  
  21.                 if (allCities.ContainsKey(currentCity))
  22.                 {
  23.                     allCities[currentCity][0] += population;
  24.                     allCities[currentCity][1] += gold;
  25.                 }
  26.                 else
  27.                 {
  28.                     allCities.Add(currentCity, new List<int>() { population, gold });
  29.                 }
  30.  
  31.                 command = Console.ReadLine();
  32.             }
  33.  
  34.             string continousCommands = Console.ReadLine();
  35.             while (continousCommands.ToLower() != "end")
  36.             {
  37.                 string[] commandsArray = continousCommands.Split("=>", StringSplitOptions.RemoveEmptyEntries);
  38.                 string commandToSwitch = commandsArray[0];
  39.                 switch (commandToSwitch)
  40.                 {
  41.                     case "Plunder":
  42.                         string town = commandsArray[1];
  43.                         int peopleToPlunder = int.Parse(commandsArray[2]);
  44.                         int goldToPlunder = int.Parse(commandsArray[3]);
  45.                         if (allCities[town][0] - peopleToPlunder > 0 && allCities[town][1] - goldToPlunder > 0)
  46.                         {
  47.                             allCities[town][0] -= peopleToPlunder;
  48.                             allCities[town][1] -= goldToPlunder;
  49.                             Console.WriteLine($"{town} plundered! {goldToPlunder} gold stolen, {peopleToPlunder} citizens killed.");
  50.                         }
  51.                         else if (allCities[town][0] - peopleToPlunder <= 0 || allCities[town][1] - goldToPlunder <= 0)
  52.                         {
  53.                             Console.WriteLine($"{town} plundered! {goldToPlunder} gold stolen, {peopleToPlunder} citizens killed.");
  54.                             allCities.Remove(town);
  55.                             Console.WriteLine($"{town} has been wiped off the map!");
  56.                         }
  57.                         break;
  58.                     case "Prosper":
  59.                         string prosperTown = commandsArray[1];
  60.                         int goldToIncrease = int.Parse(commandsArray[2]);
  61.                         if (goldToIncrease < 0)
  62.                         {
  63.                             Console.WriteLine("Gold added cannot be a negative number!");
  64.                             continousCommands = Console.ReadLine();
  65.                             continue;
  66.                         }
  67.                         else
  68.                         {
  69.                             allCities[prosperTown][1] += goldToIncrease;
  70.                             Console.WriteLine($"{goldToIncrease} gold added to the city treasury. {prosperTown} now has {allCities[prosperTown][1]} gold.");
  71.                         }
  72.                         break;
  73.                 }
  74.                 continousCommands = Console.ReadLine();
  75.             }
  76.             Console.WriteLine($"Ahoy, Captain! There are {allCities.Count} wealthy settlements to go to: ");
  77.             foreach (var gold in allCities.OrderByDescending(g => g.Value[1]).ThenBy(a => a.Key))
  78.             {
  79.                 Console.WriteLine($"{gold.Key} -> Population: {gold.Value[0]} citizens, Gold: {gold.Value[1]} kg");
  80.             }
  81.         }
  82.     }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement