AngelVasilev

SoftUni Fundamentals - Man-O-War

Feb 28th, 2020
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.54 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Man_O_War
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] pirateShip = Console.ReadLine().Split(">").Select(int.Parse).ToArray();
  11.             int[] warship = Console.ReadLine().Split(">").Select(int.Parse).ToArray();
  12.             int maximumHealth = int.Parse(Console.ReadLine());
  13.             string input = Console.ReadLine();
  14.  
  15.             while (input != "Retire")
  16.             {
  17.                 string[] tokens = input.Split();
  18.                 string command = tokens[0];
  19.  
  20.                 switch (command)
  21.                 {
  22.                     case "Fire":
  23.                         int indexOfFire = int.Parse(tokens[1]);
  24.  
  25.                         if (indexOfFire >= 0 && indexOfFire < warship.Length)
  26.                         {
  27.                             int damage = int.Parse(tokens[2]);
  28.                             warship[indexOfFire] -= damage;
  29.  
  30.                             if (warship[indexOfFire] <= 0)
  31.                             {
  32.                                 Console.WriteLine("You won! The enemy ship has sunken.");
  33.                                 return;
  34.                             }
  35.                         }
  36.                         break;
  37.  
  38.                     case "Defend":
  39.                         int startIndex = int.Parse(tokens[1]);
  40.                         int endIndex = int.Parse(tokens[2]);
  41.  
  42.                         if (startIndex >= 0 && startIndex <= pirateShip.Length)
  43.                         {
  44.                             if (endIndex >= startIndex && endIndex <= pirateShip.Length)
  45.                             {
  46.                                 int damage = int.Parse(tokens[3]);
  47.  
  48.                                 for (int i = startIndex; i <= endIndex; i++)
  49.                                 {
  50.                                     pirateShip[i] -= damage;
  51.  
  52.                                     if (pirateShip[i] <= 0)
  53.                                     {
  54.                                         Console.WriteLine("You lost! The pirate ship has sunken.");
  55.                                         return;
  56.                                     }
  57.                                 }
  58.                             }
  59.                         }
  60.                         break;
  61.  
  62.                     case "Repair":
  63.                         int repairIndex = int.Parse(tokens[1]);
  64.                         int health = int.Parse(tokens[2]);
  65.  
  66.                         if (repairIndex >= 0 && repairIndex < pirateShip.Length)
  67.                         {
  68.                             pirateShip[repairIndex] = Math.Min(maximumHealth, pirateShip[repairIndex] + health);
  69.                         }
  70.                         break;
  71.  
  72.                     case "Status":
  73.                         int counter = 0;
  74.  
  75.                         for (int i = 0; i < pirateShip.Length; i++)
  76.                         {
  77.                             if (pirateShip[i] < maximumHealth * 0.2)
  78.                             {
  79.                                 counter++;
  80.                             }
  81.                         }
  82.  
  83.                         Console.WriteLine($"{counter} sections need repair.");
  84.                         break;
  85.                 }
  86.  
  87.                 input = Console.ReadLine();
  88.             }
  89.  
  90.             int pirateShipSum = pirateShip.Sum();
  91.             int warshipSum = warship.Sum();
  92.  
  93.             Console.WriteLine($"Pirate ship status: {pirateShipSum}");
  94.             Console.WriteLine($"Warship status: {warshipSum}");
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment