Advertisement
gospod1978

Middle Ex/Treasure Hunt

Oct 31st, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.15 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace fundamental14
  6. {
  7.     class MainClass
  8.     {
  9.         public static void Main()
  10.         {
  11.             List<string> chest = Console.ReadLine().Split("|").ToList();
  12.             string input = string.Empty;
  13.             List<string> stolen = new List<string>();
  14.  
  15.             while ((input = Console.ReadLine()) != "Yohoho!")
  16.             {
  17.                 List<string> commandArgs = input.Split().ToList();
  18.                 string command = commandArgs[0];
  19.  
  20.                 switch (command)
  21.                 {
  22.                     case "Loot":
  23.                         string[] items = commandArgs.Skip(1).ToArray();
  24.                         LootCHest(items, chest);
  25.                         break;
  26.                     case "Drop":
  27.                         int index = int.Parse(commandArgs[1]);
  28.                         DropChest(index, chest);
  29.                         break;
  30.                     case "Steal":
  31.                         int count = int.Parse(commandArgs[1]);
  32.                         StealChest(count, chest);
  33.                         break;
  34.                 }
  35.             }
  36.  
  37.             double average = GetAverageChest(chest);
  38.  
  39.             if (chest.Count != 0)
  40.             {
  41.                 Console.WriteLine($"Average treasure gain: {average:f2} pirate credits.");
  42.             }
  43.             else
  44.             {
  45.                 Console.WriteLine("Failed treasure hunt.");
  46.             }
  47.  
  48.             //Console.WriteLine(string.Join(" ", chest));
  49.         }
  50.  
  51.         private static double GetAverageChest(List<string> chest)
  52.         {
  53.             double sum = 0;
  54.             foreach (var item in chest)
  55.             {
  56.                 sum += item.Length;
  57.             }
  58.             double average = sum / chest.Count;
  59.             return average;
  60.         }
  61.  
  62.         private static void StealChest(int count, List<string> chest)
  63.         {
  64.             int index = chest.Count - count;
  65.             string[] deletedItems = null;
  66.  
  67.             if (index >= 0)
  68.             {
  69.                 deletedItems = chest.Skip(index).ToArray();
  70.                 chest.RemoveRange(index, count);
  71.             }
  72.             else
  73.             {
  74.                 deletedItems = chest.ToArray();
  75.                 chest.Clear();
  76.             }
  77.            
  78.             Console.WriteLine(string.Join(", ", deletedItems));
  79.            
  80.         }
  81.  
  82.         private static void DropChest(int index, List<string> chest)
  83.         {
  84.             if (IsValidIndex(index, chest))
  85.             {
  86.                 string item = chest[index];
  87.                 chest.RemoveAt(index);
  88.                 chest.Add(item);
  89.             }
  90.         }
  91.  
  92.         private static bool IsValidIndex(int index, List<string> list)
  93.         {
  94.             return index < list.Count && index >= 0;
  95.         }
  96.  
  97.         private static void LootCHest(string[] items, List<string> chest)
  98.         {
  99.             foreach (var item in items)
  100.             {
  101.                 if (!chest.Contains(item))
  102.                 {
  103.                     chest.Insert(0, item);
  104.                 }
  105.                
  106.             }
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement