Advertisement
Guest User

Untitled

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