Advertisement
Guest User

Untitled

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