Advertisement
dragonbs

P!rates

Mar 27th, 2023
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Town
  6. {
  7.     public string Name { get; set; }
  8.     public int People { get; set; }
  9.     public int Gold { get; set; }
  10.  
  11.     public Town(string name, int people, int gold)
  12.     {
  13.         this.Name = name;
  14.         this.People = people;
  15.         this.Gold = gold;
  16.     }
  17. }
  18.  
  19. class Program
  20. {
  21.     public static List<Town> towns = new List<Town>();
  22.  
  23.     static void Main()
  24.     {
  25.         ReadInputTownInfo();
  26.         string input;
  27.         while ((input = Console.ReadLine()) != "End")
  28.         {
  29.             string[] cmd = input.Split("=>");
  30.             if (cmd[0] == "Plunder") Plunder(cmd);
  31.             else if (cmd[0] == "Prosper") Prosper(cmd);
  32.         }
  33.         if (towns.Count > 0)
  34.         {
  35.             Console.WriteLine($"Ahoy, Captain! There are {towns.Count} wealthy settlements to go to:");
  36.             Console.WriteLine(String.Join("\n", towns
  37.                 .Select(x => $"{x.Name} -> Population: {x.People} citizens, Gold: {x.Gold} kg")));
  38.         }
  39.         else
  40.             Console.WriteLine("Ahoy, Captain! All targets have been plundered and destroyed!");
  41.     }
  42.  
  43.     private static void ReadInputTownInfo()
  44.     {
  45.         string input;
  46.         while ((input = Console.ReadLine()) != "Sail")
  47.         {
  48.             string[] tokens = input.Split("||");
  49.             string name = tokens[0];
  50.             int people = int.Parse(tokens[1]);
  51.             int gold = int.Parse(tokens[2]);
  52.             if (!towns.Any(x => x.Name == name))
  53.                 towns.Add(new Town(name, people, gold));
  54.             else
  55.             {
  56.                 Town town = towns.Find(x => x.Name == name);
  57.                 town.People += people;
  58.                 town.Gold += gold;
  59.             }
  60.         }
  61.     }
  62.  
  63.     private static void Plunder(string[] cmd)
  64.     {
  65.         Town town = towns.Find(x => x.Name == cmd[1]);
  66.         int plunderPeople = int.Parse(cmd[2]);
  67.         int plunderGold = int.Parse(cmd[3]);
  68.         town.People -= plunderPeople;
  69.         town.Gold -= plunderGold;
  70.         Console.WriteLine($"{town.Name} plundered! {plunderGold} gold stolen, {plunderPeople} citizens killed.");
  71.         if (town.People <= 0 || town.Gold <= 0)
  72.         {
  73.             Console.WriteLine($"{town.Name} has been wiped off the map!");
  74.             towns.Remove(town);
  75.         }
  76.     }
  77.  
  78.     private static void Prosper(string[] cmd)
  79.     {
  80.         Town town = towns.Find(x => x.Name == cmd[1]);
  81.         int goldIncrease = int.Parse(cmd[2]);
  82.         if (goldIncrease < 0)
  83.             Console.WriteLine("Gold added cannot be a negative number!");
  84.         else
  85.         {
  86.             town.Gold += goldIncrease;
  87.             Console.WriteLine($"{goldIncrease} gold added to the city treasury. {town.Name} now has {town.Gold} gold.");
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement