Advertisement
dakata450

Untitled

Dec 31st, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. package midExam;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.stream.Collectors;
  7.  
  8. public class manOfWar {
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11.  
  12. List<Integer> pirateShip = Arrays.stream(scanner.nextLine().split(">")).
  13. map(Integer::parseInt).collect(Collectors.toList());
  14. List<Integer> warShip = Arrays.stream(scanner.nextLine().split(">")).
  15. map(Integer::parseInt).collect(Collectors.toList());
  16. int maxHealth = Integer.parseInt(scanner.nextLine());
  17. String comand = scanner.nextLine();
  18. int countPirate = 0;
  19. boolean isSunk = false;
  20.  
  21. while (!comand.equals("Retire")){
  22. String[] comandArr = comand.split(" ");
  23.  
  24.  
  25. if (comand.contains("Fire")){
  26. int index = Integer.parseInt(comandArr[1]);
  27. int damage = Integer.parseInt(comandArr[2]);
  28. if (index >= 0 && index <= warShip.size() -1){
  29. int curentIndexWarShip = warShip.get(index);
  30. curentIndexWarShip -= damage;
  31. warShip.set(index, curentIndexWarShip);
  32. if (curentIndexWarShip <= 0){
  33. System.out.println("You won! The enemy ship has sunken.");
  34. break;
  35. }
  36. }
  37. }else if (comand.contains("Defend")){
  38. int startIndex = Integer.parseInt(comandArr[1]);
  39. int endIndex = Integer.parseInt(comandArr[2]);
  40. int damagePirate = Integer.parseInt(comandArr[3]);
  41. if (startIndex >= 0 && startIndex < pirateShip.size() && endIndex >= 0 && endIndex < pirateShip.size()){
  42. for (int i = startIndex; i <= endIndex; i++) {
  43. int curentSection = pirateShip.get(i);
  44. curentSection -= damagePirate;
  45. if (curentSection <= 0){
  46. System.out.println("You lost! The pirate ship has sunken.");
  47. pirateShip.set(i, curentSection);
  48. isSunk = true;
  49. break;
  50. }
  51. pirateShip.set(i, curentSection);
  52. }
  53. }
  54. }else if (comand.contains("Repair")){
  55. int indexSection = Integer.parseInt(comandArr[1]);
  56. int health = Integer.parseInt(comandArr[2]);
  57. if (indexSection >= 0 && indexSection < pirateShip.size()){
  58. int valuePirateSection = pirateShip.get(indexSection);
  59. if (valuePirateSection + health >= maxHealth){
  60. valuePirateSection = maxHealth;
  61. pirateShip.set(indexSection, valuePirateSection);
  62. }else {
  63. valuePirateSection += health;
  64. pirateShip.set(indexSection, valuePirateSection);
  65. }
  66. }
  67. }else if (comand.equals("Status")){
  68. double status = maxHealth * 0.2;
  69. for (int item : pirateShip) {
  70. if (item < status){
  71. countPirate += 1;
  72. }
  73. }
  74. System.out.printf("%d sections need repair.%n", countPirate);
  75. }
  76.  
  77. comand = scanner.nextLine();
  78. }
  79. if (isSunk){
  80. return;
  81. }
  82. int pirate = 0;
  83. int war = 0;
  84. for (int item : pirateShip) {
  85. pirate += item;
  86. }
  87. for (int product : warShip) {
  88. war += product;
  89. }
  90. System.out.printf("Pirate ship status: %d%n", pirate);
  91. System.out.printf("Warship status: %d", war);
  92.  
  93. }
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement