Advertisement
AnetaKoseva

Man o war

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