Advertisement
JulianJulianov

Exam Man-O-War

Mar 3rd, 2020
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.21 KB | None | 0 0
  1. Man-O-War
  2. Create a program that tracks the battle and either chooses a winner or prints a stalemate. On the first line you will receive the status of the pirate ship, which is a string representing integer sections separated by '>'. On the second line you will receive the same type of status, but for the warship:
  3. "{section1}>{section2}>{section3}… {sectionn}"
  4. On the third line you will receive the maximum health capacity a section of the ship can reach.
  5. The following lines represent commands until "Retire":
  6. • Fire {index} {damage} – the pirate ship attacks the warship with the given damage at that section. Check if the index is valid and if not skip the command. If the section breaks (health <= 0) the warship sinks, print the following and stop the program:
  7. "You won! The enemy ship has sunken."
  8. • Defend {startIndex} {endIndex} {damage} - the warship attacks the pirate ship with the given damage at that range (indexes are inclusive). Check if both indexes are valid and if not skip the command. If the section breaks (health <= 0) the pirate ship sinks, print the following and stop the program:
  9. "You lost! The pirate ship has sunken."
  10. • Repair {index} {health} - the crew repairs a section of the pirate ship with the given health. Check if the index is valid and if not skip the command. The health of the section cannot exceed the maximum health capacity.
  11. • Status – prints the count of all sections of the pirate ship that need repair soon, which are all sections that are lower than 20% of the maximum health capacity. Print the following:
  12. "{count} sections need repair."
  13. In the end if a stalemate occurs print the status of both ships, which is the sum of their individual sections in the following format:
  14. "Pirate ship status: {pirateShipSum}"
  15. "Warship status: {warshipSum}"
  16. Input
  17. • On the 1st line you are going to receive the status of the pirate ship (integers separated by '>')
  18. • On the 2nd line you are going to receive the status of the warship
  19. • On the 3rd line you are going receive the maximum health a section of a ship can reach.
  20. • On the next lines, until "Retire", you will be receiving commands.
  21. Output
  22. • Print the output in the format described above.
  23. Constraints
  24. • The section numbers will be integers in the range [1.1000]
  25. • The indexes will be integers [-200.200]
  26. • The damage will be an integer in the range [1.1000]
  27. • The health will be an integer in the range [1.1000]
  28. Examples
  29. Input                           Output                        Input                    Output
  30. 12>13>11>20>66                  2 sections need repair.       2>3>4>5>2                3 sections need repair.
  31. 12>22>33>44>55>32>18            Pirate ship status: 135       6>7>8>9>10>11            You lost! The pirate ship has sunken.
  32. 70                              Warship status: 205           20
  33. Fire 2 11                                                     Status
  34. Fire 8 100                                                    Fire 2 3
  35. Defend 3 6 11                                                 Defend 0 4 11
  36. Defend 0 3 5                                                  Repair 3 18
  37. Repair 1 33                                                   Retire
  38. Status
  39. Retire 
  40.  
  41.  
  42. Comments
  43. First, we receive the command "Fire 2 11" and damage the warship at section index 2 which is currently 33 and after reduction the status of the warship is the following:
  44. 12 22 22 44 55 32 18
  45. The second and third command have invalid indexes, so we skip them.
  46. The fourth command "Defend 0 3 5" damages 4 sections of the pirate ship with 5 which results in the following status:
  47. 7 8 6 15 66
  48. The fifth command "Repair 1 33" repairs the pirate ship section and adds 33 health to the current 8 which results in 41
  49. Only 2 sections of the pirate ship (7 and 6) need repair soon.
  50. In the end there is a stalemate, so we print both ship statuses (sum of all sections).
  51.  
  52. using System;
  53. using System.Linq;
  54. using System.Collections.Generic;
  55. namespace ManOWar
  56. {
  57.     class Program
  58.     {
  59.         static void Main(string[] args)
  60.         {
  61.             var statusPiShip = Console.ReadLine().Split('>', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList();
  62.             var statusWarShip = Console.ReadLine().Split('>', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList();
  63.             var maxHealth = int.Parse(Console.ReadLine());
  64.             var input = Console.ReadLine().Split().ToList();
  65.  
  66.             while (!(input[0]).Equals("Retire"))
  67.             {
  68.                 string index0 = input[0];
  69.                 if (index0 != "Status" )
  70.                 {
  71.                     var index1 = int.Parse(input[1]);
  72.                     var index2 = int.Parse(input[2]);
  73.                     if (index0 == "Fire" && index1 >= 0 && index1 < statusPiShip.Count && statusWarShip.Count >= statusPiShip.Count)
  74.                     {
  75.                         if (index2 == statusPiShip[index1])
  76.                         {
  77.                             var getElementWar = statusWarShip.GetRange(index1, 1);
  78.                             var getElementPi = statusPiShip.GetRange(index1, 1);
  79.                             int attack = getElementWar[0] - getElementPi[0];
  80.                             if (attack <= 0)
  81.                             {
  82.                                 Console.WriteLine("You won! The enemy ship has sunken.");
  83.                                 return;
  84.                             }
  85.                             statusWarShip.RemoveAt(index1);
  86.                             statusWarShip.Insert(index1, attack);
  87.                         }
  88.                     }
  89.  
  90.                     else if (index0 == "Defend" && index1 >= 0 && index1 < statusPiShip.Count && statusPiShip.Count > index2 && index2 >= index1)
  91.                     {
  92.                         var index3 = int.Parse(input[3]);
  93.                         for (int i = index1; i <= index2; i++)
  94.                         {
  95.                             var getElementPi = statusPiShip.GetRange(i, 1);
  96.                             int attack = getElementPi[0] - index3;
  97.                             if (attack <= 0)
  98.                             {
  99.                                 Console.WriteLine("You lost! The pirate ship has sunken.");
  100.                                 return;
  101.                             }
  102.                             statusPiShip.RemoveAt(i);
  103.                             statusPiShip.Insert(i, attack);
  104.                         }
  105.                     }
  106.  
  107.                     else if (index0 == "Repair" && index1 >= 0 && statusPiShip.Count > index1)
  108.                     {
  109.                         var getElementPi = statusPiShip.GetRange(index1, 1);
  110.                         int repair = getElementPi[0] + index2;
  111.                         if (repair >= maxHealth)
  112.                         {
  113.                             repair = maxHealth;
  114.                         }
  115.                         statusPiShip.RemoveAt(index1);
  116.                         statusPiShip.Insert(index1, repair);
  117.                     }
  118.                 }
  119.                 else if (index0 == "Status")
  120.                 {
  121.                     var counter = 0;
  122.                     for (int i = 0; i < statusPiShip.Count; i++)
  123.                     {
  124.                         double percent = 0;
  125.                         percent = 100 - (((double)maxHealth - statusPiShip[i]) / (double)maxHealth * 100);
  126.                         if (percent < 20)
  127.                         {
  128.                             counter++;
  129.                         }
  130.                     }
  131.                     if (counter > 0)
  132.                     {
  133.                         Console.WriteLine($"{counter} sections need repair.");
  134.                     }
  135.                 }
  136.                 input = Console.ReadLine().Split().ToList();
  137.             }
  138.             var pirateShipSum = 0;
  139.             for (int i = 0; i < statusPiShip.Count; i++)
  140.             {
  141.                 pirateShipSum += statusPiShip[i];
  142.             }
  143.             var warShipSum = 0;
  144.             for (int i = 0; i < statusWarShip.Count; i++)
  145.             {
  146.                 warShipSum += statusWarShip[i];
  147.             }
  148.             Console.WriteLine($"Pirate ship status: {pirateShipSum}");
  149.             Console.WriteLine($"Warship status: {warShipSum}");
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement