zaryana

TreasureHunt

Feb 28th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace TreasureHunt
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<string> treasureChest = Console.ReadLine()
  12.                 .Split("|")
  13.                 .ToList();
  14.  
  15.             string command;
  16.             while ((command = Console.ReadLine()) != "Yohoho!")
  17.             {
  18.                 string[] commandArgs = command.Split(' ').ToArray();
  19.                 string commandType = commandArgs[0];
  20.  
  21.                 if (commandType == "Loot")
  22.                 {
  23.                     for (int i = 1; i < commandArgs.Length; i++)
  24.                     {
  25.                         string itemToInsert = commandArgs[i];
  26.                         if (treasureChest.Contains(itemToInsert))
  27.                         {
  28.                             continue;
  29.                         }
  30.                         else
  31.                         {
  32.                             treasureChest.Insert(0, itemToInsert);
  33.                         }
  34.                     }
  35.                 }
  36.                 if (commandType == "Drop")
  37.                 {
  38.                     int index = int.Parse(commandArgs[1]);
  39.  
  40.                     if (index < 0 || index >= treasureChest.Count)
  41.                     {
  42.                         continue;
  43.                     }
  44.                     else
  45.                     {
  46.                         string temp = treasureChest[index];
  47.                         treasureChest.RemoveAt(index);
  48.                         treasureChest.Add(temp);
  49.                     }
  50.                 }
  51.                 if (commandType == "Steal")
  52.                 {
  53.                     int count = int.Parse(commandArgs[1]);
  54.  
  55.                     if (count > treasureChest.Count)
  56.                     {
  57.                         count = treasureChest.Count;
  58.                         List<string> stolenLoot = new List<string>();
  59.  
  60.                         for (int i = 0; i < count; i++)
  61.                         {
  62.                             stolenLoot.Add(treasureChest[treasureChest.Count - 1]);
  63.                             treasureChest.Remove(treasureChest[treasureChest.Count - 1]);
  64.                         }
  65.                         stolenLoot.Reverse();
  66.                         Console.WriteLine(string.Join(", ", stolenLoot));
  67.                     }
  68.                     else
  69.                     {
  70.                         List<string> stolenLoot = new List<string>();
  71.  
  72.                         for (int i = 0; i < count; i++)
  73.                         {
  74.                             stolenLoot.Add(treasureChest[treasureChest.Count - 1]);
  75.                             treasureChest.Remove(treasureChest[treasureChest.Count - 1]);
  76.                         }
  77.                         stolenLoot.Reverse();
  78.                         Console.WriteLine(string.Join(", ", stolenLoot));
  79.                     }
  80.                 }
  81.             }
  82.             if (treasureChest.Count == 0)
  83.             {
  84.                 Console.WriteLine($"Failed treasure hunt.");
  85.             }
  86.             else
  87.             {
  88.                 double sum = 0.0;
  89.                 for (int i = 0; i < treasureChest.Count; i++)
  90.                 {
  91.                     sum += treasureChest[i].Count();
  92.                 }
  93.                 double averageGain = sum / treasureChest.Count();
  94.                 Console.WriteLine($"Average treasure gain: {averageGain:F2} pirate credits.");
  95.             }
  96.         }
  97.     }
  98. }
Add Comment
Please, Sign In to add comment