Advertisement
Tsuki11

Untitled

Jun 24th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class fbBookWorm {
  4.  
  5. public static int playerRow;
  6. public static int playerCol;
  7.  
  8. public static void main(String[] args) {
  9. Scanner sc = new Scanner(System.in);
  10.  
  11. StringBuilder initialString = new StringBuilder(sc.nextLine());
  12. Integer sizeSquareM = Integer.parseInt(sc.nextLine());
  13.  
  14. String[][] matrix = new String[sizeSquareM][sizeSquareM];
  15.  
  16. readMatrix(matrix, sc);
  17. String powerBook;
  18. while (!(powerBook = sc.nextLine()).equals("end")) {
  19. int moveToRow = playerRow; // --> прехвърляме тук към нови променливи, за да правим проверките, без да местим истинските индекси на играча
  20. int moveToCol = playerCol;
  21. String currentLetter;
  22. switch (powerBook) {
  23. case "up":
  24. moveToRow -= 1;
  25. if (checkIndex(matrix, moveToCol, moveToCol)) {
  26. if (!matrix[moveToRow][moveToCol].equals("-")) {
  27. String toAdd = matrix[moveToRow][moveToCol];
  28. initialString.append(toAdd);
  29. }
  30. matrix[moveToRow][moveToCol] = "P";
  31. matrix[playerRow][playerCol] = "-"; // ---> сетваме там където е бил тиренце, за да му махнем буквата
  32. playerRow = moveToRow;
  33. playerCol = moveToCol; // ---> местим му индексите
  34. //else {
  35. // initialString += "";
  36. // //matrix[playerRow][playerCol] = "P"; не ти трябва това махам от всички кейсове
  37. // }
  38. } else {
  39. // int end = initialString.length() - 1;
  40. //initialString = new StringBuilder(initialString.substring(0, end));
  41. initialString.deleteCharAt(initialString.length() -1);
  42.  
  43. }
  44. break;
  45. case "down":
  46. moveToRow += 1;
  47. if (checkIndex(matrix, moveToRow, moveToCol)) {
  48. if (!matrix[moveToRow][moveToCol].equals("-")) {
  49. String toAdd = matrix[moveToRow][moveToCol];
  50. initialString.append(toAdd);
  51. }
  52. matrix[moveToRow][moveToCol] = "P";
  53. matrix[playerRow][playerCol] = "-";
  54. playerRow = moveToRow;
  55. playerCol = moveToCol;
  56.  
  57. } else {
  58. //int end = initialString.length() - 1;
  59. // initialString = new StringBuilder(initialString.substring(0, end));
  60. initialString.deleteCharAt(initialString.length() -1);
  61. }
  62. break;
  63. case "left":
  64. moveToCol -= 1;
  65. if (checkIndex(matrix, moveToRow, moveToCol)) {
  66. if (!matrix[moveToRow][moveToCol].equals("-")) {
  67. String toAdd = matrix[moveToRow][moveToCol];
  68. initialString.append(toAdd);
  69. }
  70. matrix[moveToRow][moveToCol] = "P";
  71. matrix[playerRow][playerCol] = "-";
  72. playerRow = moveToRow;
  73. playerCol = moveToCol;
  74. //else {
  75. // initialString += "";
  76. // //matrix[playerRow][playerCol] = "P";
  77. // }
  78. } else {
  79. //int end = initialString.length() - 1;
  80. //initialString = new StringBuilder(initialString.substring(0, end));
  81. initialString.deleteCharAt(initialString.length() -1);
  82. }
  83. break;
  84. case "right":
  85. moveToCol += 1;
  86. if (checkIndex(matrix, moveToRow, moveToCol)) {
  87. if (!matrix[moveToRow][moveToCol].equals("-")) {
  88. String toAdd = matrix[moveToRow][moveToCol];
  89. initialString.append(toAdd);
  90. }
  91. matrix[moveToRow][moveToCol] = "P";
  92. matrix[playerRow][playerCol] = "-";
  93. playerRow = moveToRow;
  94. playerCol = moveToCol;
  95. //else {
  96. // initialString += "";
  97. // //matrix[playerRow][playerCol] = "P";
  98. // }
  99. } else {
  100. // int end = initialString.length() - 1;
  101. //
  102. initialString.deleteCharAt(initialString.length() -1);
  103. }
  104. break;
  105. }
  106. }
  107. System.out.println(initialString.toString());
  108. printMatrix(matrix);
  109. }
  110.  
  111. private static void readMatrix(String[][] matrix, Scanner scanner) {
  112. for (int r = 0; r < matrix.length; r++) {
  113. String input = scanner.nextLine();
  114. for (int c = 0; c < matrix[r].length; c++) {
  115. matrix[r][c] = input.charAt(c) + "";
  116. if (matrix[r][c].equals("P")) {
  117. playerRow = r;
  118. playerCol = c;
  119. // matrix[r][c]="-";//TODO check оставяме си играча с буквата, като го занулиш го губиш, сред другите
  120. }
  121. }
  122. }
  123. }
  124.  
  125. private static void printMatrix(String[][] matrix) {
  126. for (int i = 0; i < matrix.length; i++) {
  127. for (int j = 0; j < matrix[i].length; j++) {
  128. System.out.print(matrix[i][j] + "");
  129. }
  130. System.out.println();
  131. }
  132. }
  133.  
  134. private static boolean checkIndex(String[][] matrix, int a, int b) {
  135. return a >= 0 && a < matrix.length && b >= 0 && b < matrix[a].length;
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement