Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. package Excercises;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6.  
  7. public class ManOWar {
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. String[] pirateShip = scanner.nextLine().split(">");
  12. List<Integer> pirateSection = new ArrayList<>();
  13.  
  14. for (int i = 0; i < pirateShip.length; i++) {
  15. int section = Integer.parseInt(pirateShip[i]);
  16. pirateSection.add(section);
  17. }
  18.  
  19. String[] warship = scanner.nextLine().split(">");
  20. List<Integer> warshipSection = new ArrayList<>();
  21.  
  22. for (int i = 0; i < warship.length; i++) {
  23. int section = Integer.parseInt(warship[i]);
  24. warshipSection.add(section);
  25. }
  26.  
  27. int maxHealthSection = Integer.parseInt(scanner.nextLine());
  28. boolean isGameOver = false;
  29.  
  30. String input = scanner.nextLine();
  31. while (!"Retire".equals(input) || isGameOver) {
  32. String[] token = input.split(" ");
  33. String command = token[0];
  34.  
  35. switch (command) {
  36. case "Fire": {
  37. int index = Integer.parseInt(token[1]);
  38. int damage = Integer.parseInt(token[2]);
  39. if (index < 0 || index >= warshipSection.size()) {
  40. break;
  41. }
  42. int section = warshipSection.get(index);
  43. int sectionAfterFire = section - damage;
  44. warshipSection.set(index, sectionAfterFire);
  45. if (sectionAfterFire <= 0) {
  46. System.out.println("You won! The enemy ship has sunken.");
  47. isGameOver = true;
  48. }
  49. break;
  50. }
  51. case "Defend": {
  52. int startIndex = Integer.parseInt(token[1]);
  53. int endIndex = Integer.parseInt(token[2]);
  54. int damage = Integer.parseInt(token[3]);
  55. if (startIndex < 0 || startIndex >= pirateSection.size()) {
  56. break;
  57. }
  58. if (endIndex < 0 || endIndex >= pirateSection.size()) {
  59. break;
  60. }
  61. for (int i = startIndex; i <= endIndex; i++) {
  62. int section = pirateSection.get(i);
  63. int pirateShipAfterFire = section - damage;
  64. pirateSection.set(i, pirateShipAfterFire);
  65. if (pirateShipAfterFire <= 0) {
  66. System.out.println("You lost! The pirate ship has sunken.");
  67. isGameOver = true;
  68. break;
  69. }
  70. }
  71. break;
  72. }
  73. case "Repair": {
  74. int index = Integer.parseInt(token[1]);
  75. int health = Integer.parseInt(token[2]);
  76. if (index < 0 || index >= warshipSection.size()) {
  77. break;
  78. }
  79. int section = pirateSection.get(index);
  80. int repaired = section + health;
  81. if (repaired > maxHealthSection) {
  82. repaired = maxHealthSection;
  83. }
  84. pirateSection.set(index, repaired);
  85. break;
  86. }
  87. case "Status": {
  88. int counter = 0;
  89. for (int i = 0; i < pirateSection.size(); i++) {
  90. double percentage = (pirateSection.get(i) * 1.0 / maxHealthSection) * 100;
  91. if (percentage < 20) {
  92. counter++;
  93. }
  94. }
  95. System.out.printf("%d sections need repair.%n", counter);
  96. break;
  97. }
  98. }
  99. input = scanner.nextLine();
  100. }
  101. int myShipSum = 0;
  102. int warshipSum = 0;
  103. for (int s :
  104. pirateSection) {
  105. myShipSum += s;
  106. }
  107. for (int z :
  108. warshipSection) {
  109. warshipSum += z;
  110. }
  111. System.out.printf("Pirate ship status: %d%n", myShipSum);
  112. System.out.printf("Warship status: %d", warshipSum);
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement