Advertisement
desislava_topuzakova

Untitled

Feb 1st, 2023
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. package solution_1;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Test {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. int size = Integer.parseInt(scanner.nextLine());
  9. char[][] matrix = new char[size][size];
  10. int[] currPosition = new int[2];
  11. fillTheMatrix (matrix, size, scanner, currPosition);
  12. int[] money = new int[1];
  13. int[] isOut = new int[1];
  14. int[] areMoneyEnough = new int[1];
  15.  
  16. while(true) {
  17. String direction = scanner.nextLine();
  18. move(matrix, currPosition, money, direction, isOut, areMoneyEnough);
  19. if (isOut[0] == 1 || areMoneyEnough[0] == 1){
  20. break;
  21. }
  22. }
  23. if (isOut[0] == 1) {
  24. System.out.println("I do not need more swords!");
  25. }
  26.  
  27. if(isOut[0] == 0 && money[0] >= 65) {
  28. System.out.println("Very nice swords, I will come back for more!");
  29. }
  30. // System.out.printf("Money: %d%n", money[0]);
  31. System.out.printf("The king paid %d gold coins. %n", money[0]);
  32. printTheMatrix(matrix);
  33. }
  34.  
  35. private static void printTheMatrix(char[][] matrix) {
  36. for (int row = 0; row < matrix.length; row++) {
  37. for (int col = 0; col < matrix.length; col++) {
  38. System.out.print(matrix[row][col]);
  39. }
  40. System.out.println();
  41. }
  42. }
  43.  
  44. private static void move(char[][] matrix, int[] currPosition, int[] money, String direction, int[] isOut, int[] areMoneyEnough) {
  45. int newRowPosition = 0;
  46. int newColPosition = 0;
  47. switch (direction) {
  48. case "up":
  49. if (currPosition[0] == 0) {
  50. isOut[0] = 1;
  51. matrix[currPosition[0]][currPosition[1]] = '-';
  52. } else {
  53. newRowPosition = currPosition[0] - 1;
  54. }
  55. newColPosition = currPosition[1];
  56. break;
  57. case "down":
  58. if (currPosition[0] == matrix.length - 1) {
  59. isOut[0] = 1;
  60. matrix[currPosition[0]][currPosition[1]] = '-';
  61. } else {
  62. newRowPosition = currPosition[0] + 1;
  63. }
  64. newColPosition = currPosition[1];
  65. break;
  66. case "left":
  67. if (currPosition[1] == 0) {
  68. isOut[0] = 1;
  69. matrix[currPosition[0]][currPosition[1]] = '-';
  70. } else {
  71. newColPosition = currPosition[1] - 1;
  72. }
  73. newRowPosition = currPosition[0];
  74. break;
  75. case "right":
  76. if (currPosition[1] == matrix.length - 1) {
  77. isOut[0] = 1;
  78. matrix[currPosition[0]][currPosition[1]] = '-';
  79. } else {
  80. newColPosition = currPosition[1] + 1;
  81. }
  82. newRowPosition = currPosition[0];
  83. break;
  84. }
  85. if (isOut[0] == 1) {
  86. return;
  87. }
  88. if (Character.isDigit(matrix[newRowPosition][newColPosition])) {
  89. money[0] += Character.getNumericValue(matrix[newRowPosition][newColPosition]);
  90. matrix[currPosition[0]][currPosition[1]] = '-';
  91. matrix[newRowPosition][newColPosition] = 'A';
  92. currPosition[0] = newRowPosition;
  93. currPosition[1] = newColPosition;
  94. } else if (matrix[newRowPosition][newColPosition] == 'M') {
  95. matrix[currPosition[0]][currPosition[1]] = '-';
  96. matrix[newRowPosition][newColPosition] = '-';
  97. int[] newPosition = findOtherPillar(matrix);
  98. currPosition[0] = newPosition[0];
  99. currPosition[1] = newPosition[1];
  100. matrix[currPosition[0]][currPosition[1]] = 'A';
  101. } else {
  102. matrix[currPosition[0]][currPosition[1]] = '-';
  103. matrix[newRowPosition][newColPosition] = 'A';
  104. currPosition[0] = newRowPosition;
  105. currPosition[1] = newColPosition;
  106. }
  107. if (money[0] >= 65) {
  108. areMoneyEnough[0] = 1;
  109. }
  110. }
  111.  
  112. private static int[] findOtherPillar(char[][] matrix) {
  113. int[] otherPillarPosition = new int[2];
  114. for (int row = 0; row < matrix.length; row++) {
  115. for (int col = 0; col < matrix[row].length; col++) {
  116. char currChar = matrix[row][col];
  117. if (currChar == 'M') {
  118. otherPillarPosition[0] = row;
  119. otherPillarPosition[1] = col;
  120. }
  121. }
  122. }
  123. return otherPillarPosition;
  124. }
  125.  
  126. private static void fillTheMatrix(char[][] matrix, int size, Scanner scanner, int[] currPosition) {
  127. for (int row = 0; row < size; row++) {
  128. String currLine = scanner.nextLine();
  129. for (int col = 0; col < size; col++) {
  130. char currChar = currLine.charAt(col);
  131. matrix[row][col] = currChar;
  132. if (currChar == 'A') {
  133. currPosition[0] = row;
  134. currPosition[1] = col;
  135. }
  136. }
  137. }
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement