Advertisement
Guest User

Untitled

a guest
Oct 28th, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.67 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using System;
  6.  
  7. namespace ConsoleApp5
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<int> pirateStatus = Console.ReadLine().Split('>').Select(int.Parse).ToList();
  14.  
  15.             List<int> warshipStatus = Console.ReadLine().Split('>').Select(int.Parse).ToList();
  16.  
  17.             int maxHealth = int.Parse(Console.ReadLine());
  18.  
  19.             int currentValue = 0; int currentHealth = 0; int currentIndex = 0; int damage = 0;
  20.  
  21.             string command = ""; var array = command.Split();
  22.  
  23.             bool sunken1 = false;
  24.  
  25.             bool sunken2 = false;
  26.             for (int i = 1; i < pirateStatus.Count - 1; i++)
  27.             {
  28.                 for (int k = 1; k < warshipStatus.Count - 1; k++)
  29.                 {
  30.                     if (sunken1 == true)
  31.                     {
  32.                         break;
  33.                     }
  34.                     else if (sunken2 == true)
  35.                     {
  36.                         break;
  37.                     }
  38.                     command = Console.ReadLine();
  39.                     double sum1 = pirateStatus.Sum(x => Convert.ToInt32(x));
  40.                     double sum2 = warshipStatus.Sum(x => Convert.ToInt32(x));
  41.  
  42.                     array = command.Split();
  43.  
  44.                     if (array[0] == "Fire")
  45.                     {
  46.                         int index = int.Parse(array[1]);
  47.                         damage = int.Parse(array[2]);
  48.                         if (damage < maxHealth)
  49.                         {
  50.                             if (index <= warshipStatus.Count)
  51.                             {
  52.                                 currentValue = warshipStatus.ElementAt(index);
  53.                                 currentHealth = currentValue - damage;
  54.                                 if (currentValue - damage <= 0)
  55.                                 {
  56.                                     Console.WriteLine("You won! The enemy ship has sunken.");
  57.                                     sunken1 = true;
  58.                                     break;
  59.                                 }
  60.                                 else
  61.                                 {
  62.                                     warshipStatus.RemoveAt(index);
  63.                                     warshipStatus.Insert(index, currentHealth);
  64.                                 }
  65.                             }
  66.                         }
  67.                     }
  68.                     else if (array[0] == "Defend")
  69.                     {
  70.                         if (int.Parse(array[1]) <= pirateStatus.Count && int.Parse(array[2]) <= pirateStatus.Count)
  71.                         {
  72.                             int startIndex = int.Parse(array[1]);
  73.                             int endIndex = int.Parse(array[2]);
  74.                             for (int j = startIndex; j <= endIndex; j++)
  75.                             {
  76.                                 damage = int.Parse(array[3]);
  77.                                 currentValue = pirateStatus.ElementAt(j);
  78.                                 currentHealth = currentValue - damage;
  79.                                 if (currentValue - damage <= 0)
  80.                                 {
  81.                                     Console.WriteLine("You lost! The pirate ship has sunken.");
  82.                                     sunken2 = true;
  83.                                     break;
  84.                                 }
  85.                                 else
  86.                                 {
  87.                                     pirateStatus.RemoveAt(j);
  88.                                     pirateStatus.Insert(j, currentHealth);
  89.                                 }
  90.                             }
  91.                         }
  92.                     }
  93.                     else if (array[0] == "Repair")
  94.                     {
  95.                         if (int.Parse(array[1]) <= pirateStatus.Count - 1 && int.Parse(array[2]) <= maxHealth)
  96.                         {
  97.                             currentIndex = int.Parse(array[1]);
  98.                             int repHealth = int.Parse(array[2]);
  99.                             currentValue = pirateStatus.ElementAt(currentIndex);
  100.                             if (currentValue + repHealth > maxHealth)
  101.                             {
  102.                                 currentHealth = maxHealth;
  103.                             }
  104.                             else
  105.                             {
  106.                                 currentHealth = currentValue + repHealth;
  107.                             }
  108.                             pirateStatus.RemoveAt(currentIndex);
  109.                             pirateStatus.Insert(currentIndex, currentHealth);
  110.  
  111.                         }
  112.                     }
  113.                     else if (array[0] == "Status")
  114.                     {
  115.                         double condition = maxHealth * 0.20;
  116.                         int sectorsForRepair = 0;
  117.                         for (int y = 0; y < pirateStatus.Count; y++)
  118.                         {
  119.                             if (pirateStatus.ElementAt(y) < condition)
  120.                             {
  121.                                 sectorsForRepair++;
  122.                             }
  123.                         }
  124.                         Console.WriteLine($"{sectorsForRepair} sections need repair.");
  125.                     }
  126.                     if (command == "Retire")
  127.                     {
  128.  
  129.                         Console.WriteLine($"Pirate ship status: {sum1}");
  130.                         Console.WriteLine($"Warship status: {sum2}");
  131.                         break;
  132.                     }
  133.                 }
  134.             }
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement