Teo_Yanchev

C# Fundamentals - 03. ManOWar - Mid Exam 06.09.2020

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