Advertisement
Guest User

Untitled

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