desislava_topuzakova

Untitled

Oct 8th, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. package FunctionalProgramming_Exercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ThroneConquering_02 {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. int energy = Integer.parseInt(scanner.nextLine());
  9. int rows = Integer.parseInt(scanner.nextLine());
  10. char [][] field = new char[rows][rows];
  11.  
  12. //въвеждаме полето
  13. for (int row = 0; row < rows; row++) {
  14. field[row] = scanner.nextLine().toCharArray(); //"--H--".toCharArray() -> ['-', '-', 'H', '-', '-']
  15. }
  16.  
  17. //намираме къде е Парис първоначално
  18. int parisRow = 0;
  19. int parisCol = 0;
  20. for (int row = 0; row < rows; row++) {
  21. for (int col = 0; col < field[row].length; col++) {
  22. if (field[row][col] == 'P') {
  23. parisRow = row;
  24. parisCol = col;
  25. }
  26. }
  27. }
  28.  
  29. while (true) {
  30. String command = scanner.nextLine();
  31. //"{direction} {row have spartan} {col have spartan}".split(" ") -> ["dir", "row", "col"]
  32. String direction = command.split(" ")[0]; //"up", "down", "left", "right"
  33. int enemyRow = Integer.parseInt(command.split(" ")[1]);
  34. int enemyCol = Integer.parseInt(command.split(" ")[2]);
  35.  
  36. field[parisRow][parisCol] = '-';
  37. field[enemyRow][enemyCol] = 'S';
  38. //преместване
  39. switch (direction) {
  40. case "up":
  41. if (parisRow - 1 >= 0) {
  42. parisRow--;
  43. }
  44. break;
  45. case "down":
  46. if (parisRow + 1 < field.length) {
  47. parisRow++;
  48. }
  49. break;
  50. case "left":
  51. if (parisCol - 1 >= 0) {
  52. parisCol--;
  53. }
  54. break;
  55. case "right":
  56. if(parisCol + 1 < field.length) {
  57. parisCol++;
  58. }
  59.  
  60. break;
  61. }
  62. //след преместване
  63. //1. намаляме енергията с 1
  64. energy--;
  65. //2. проверка дали е умрял
  66. if (energy <= 0) {
  67. parisDead(field, parisRow, parisCol);
  68. return;
  69. }
  70.  
  71. //3. има ли спартанец там където се е преместил
  72. if (field[parisRow][parisCol] == 'S') {
  73. //3.1. победа над спартанеца -> намаляме енергията с 2
  74. energy -= 2;
  75. //3.2 проверка дали е умрял -> ако енергията <= 0
  76. if (energy <= 0) {
  77. parisDead(field, parisRow, parisCol);
  78. return;
  79. } else {
  80. field[parisRow][parisCol] = '-';
  81. }
  82. }
  83. //4. има ли Елена там където се е преместил
  84. else if (field[parisRow][parisCol] == 'H') {
  85. //4.1. на мястото, където е Елена -> слагаме -
  86. field[parisRow][parisCol] = '-';
  87. //4.2. отпечатване -> останала енергия
  88. System.out.printf("Paris has successfully abducted Helen! Energy left: %d%n", energy);
  89. //4.3. отпечатваме матрицата
  90. printField(field);
  91. return;
  92. }
  93. }
  94. }
  95.  
  96. public static void parisDead(char[][] field, int parisRow, int parisCol) {
  97. //Парис умира
  98. //1. там където е бил слагаме X
  99. field[parisRow][parisCol] = 'X';
  100. //2. отпечатване -> къде е умрял
  101. System.out.printf("Paris died at %d;%d.%n", parisRow, parisCol);
  102. //3. отпечатваме матрицата
  103. printField(field);
  104. }
  105.  
  106. private static void printField(char[][] field) {
  107. for (int row = 0; row < field.length; row++) {
  108. for (int col = 0; col < field[row].length; col++) {
  109. System.out.print(field[row][col]);
  110. }
  111. System.out.println();
  112. }
  113. }
  114. }
  115.  
Add Comment
Please, Sign In to add comment