Advertisement
gospod1978

Middle Ex/Man O War

Oct 30th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.65 KB | None | 0 0
  1.  
  2.  
  3. using System;
  4. using System.Linq;
  5. using System.Collections.Generic;
  6.  
  7. namespace fundamental14
  8. {
  9.     class MainClass
  10.     {
  11.         public static void Main()
  12.         {
  13.             List<int> pirateship = Console.ReadLine().Split(">").Select(int.Parse).ToList();
  14.             List<int> warship = Console.ReadLine().Split(">").Select(int.Parse).ToList();
  15.  
  16.             int helty = int.Parse(Console.ReadLine());
  17.             int result = 0;
  18.  
  19.             string input = string.Empty;
  20.  
  21.             while ((input = Console.ReadLine()) != "Retire")
  22.             {
  23.                 string[] array = input.Split();
  24.                 string command = array[0];
  25.  
  26.                 if (command == "Fire")
  27.                 {
  28.                     int index = int.Parse(array[1]);
  29.                     int damage = int.Parse(array[2]);
  30.                     if (index < warship.Count && index >= 0)
  31.                     {
  32.                         if (damage > 0)
  33.                         {
  34.  
  35.                             warship[index] -= damage;
  36.                             if (warship[index] <= 0)
  37.                             {
  38.                                 Console.WriteLine("You won! The enemy ship has sunken.");
  39.                                 result++;
  40.                                 break;
  41.                             }
  42.  
  43.                         }
  44.                     }
  45.                 }
  46.                 else if (command == "Defend")
  47.                 {
  48.                     int startIndex = int.Parse(array[1]);
  49.                     int endIndex = int.Parse(array[2]);
  50.                     int damage = int.Parse(array[3]);
  51.                     if (damage > 0)
  52.                     {
  53.                         if (startIndex < pirateship.Count && startIndex >= 0 && endIndex < pirateship.Count && endIndex >= 0 && endIndex > startIndex)
  54.                         {
  55.                             for (int i = startIndex; i <= endIndex; i++)
  56.                             {
  57.                                 pirateship[i] -= damage;
  58.                                 if (pirateship[i] <= 0)
  59.                                 {
  60.                                     Console.WriteLine("You lost! The pirate ship has sunken.");
  61.                                     result++;
  62.                                     break;
  63.                                 }
  64.                             }
  65.                         }
  66.                     }
  67.                 }
  68.                 else if (command == "Repair")
  69.                 {
  70.                     int index = int.Parse(array[1]);
  71.                     int repair = int.Parse(array[2]);
  72.  
  73.                     if(index < pirateship.Count && index >= 0)
  74.                     {
  75.                         pirateship[index] += repair;
  76.                         if (pirateship[index] > helty)
  77.                         {
  78.                             pirateship[index] = helty;
  79.                         }
  80.                     }
  81.                 }
  82.                 else if (command == "Status")
  83.                 {
  84.                     int needToRepaera = (int)(helty * 0.2);
  85.                     int sum = 0;
  86.                     for (int i = 0; i < pirateship.Count; i++)
  87.                     {
  88.                         if (needToRepaera > pirateship[i])
  89.                         {
  90.                             sum++;
  91.                         }
  92.                     }
  93.  
  94.                     Console.WriteLine($"{sum} sections need repair.");
  95.                 }
  96.             }
  97.  
  98.             if (result == 0)
  99.             {
  100.                 Console.WriteLine("Pirate ship status: {0}", pirateship.Sum());
  101.                 Console.WriteLine("Warship status: {0}", warship.Sum());
  102.             }
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement