Advertisement
Guest User

Untitled

a guest
Jun 24th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ONEmBookWorm {
  4.  
  5. public static int playerRow = 0;
  6. public static int playerCol = 0;
  7.  
  8. public static void main(String[] args) {
  9. Scanner sc = new Scanner(System.in);
  10.  
  11. String initialString = 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. String currentLetter;
  20. switch (powerBook) {
  21. case "up":
  22. matrix[playerRow][playerCol] = "-";
  23. int A = playerRow;
  24. int B = playerCol;
  25. playerRow -= 1;
  26. playerCol += 0;
  27. if (checkIndex(matrix, playerRow, playerCol)) {
  28. if (!matrix[playerRow][playerCol].equals("-")) {
  29. String toAdd = matrix[playerRow][playerCol];
  30. initialString += toAdd;
  31. matrix[playerRow][playerCol] = "P";
  32. } else {
  33. initialString += "";
  34. matrix[playerRow][playerCol] = "P";
  35. }
  36. } else {
  37. if (initialString.length() > 0) {
  38. int end = initialString.length() - 1;
  39. initialString = initialString.substring(0, end);
  40. matrix[A][B] = "P";
  41. //matrix[playerRow][playerCol] = "-";
  42. }
  43. }
  44. break;
  45. case "down":
  46. matrix[playerRow][playerCol] = "-";
  47. int A1 = playerRow;
  48. int B1= playerCol;
  49. playerRow += 1;
  50. playerCol += 0;
  51. if (checkIndex(matrix, playerRow, playerCol)) {
  52. if (!matrix[playerRow][playerCol].equals("-")) {
  53. String toAdd = matrix[playerRow][playerCol];
  54. initialString += toAdd;
  55. matrix[playerRow][playerCol] = "P";
  56. } else {
  57. initialString += "";
  58. matrix[playerRow][playerCol] = "P";
  59. }
  60. } else {
  61. if (initialString.length() > 0) {
  62. int end = initialString.length() - 1;
  63. initialString = initialString.substring(0, end);
  64. matrix[A1][B1] = "P";
  65. //matrix[playerRow][playerCol] = "-";
  66. }
  67. }
  68. break;
  69. case "left":
  70. matrix[playerRow][playerCol] = "-";
  71. int A2 = playerRow;
  72. int B2 = playerCol;
  73. playerRow += 0;
  74. playerCol -= 1;
  75. if (checkIndex(matrix, playerRow, playerCol)) {
  76. if (!matrix[playerRow][playerCol].equals("-")) {
  77. String toAdd = matrix[playerRow][playerCol];
  78. initialString += toAdd;
  79. matrix[playerRow][playerCol] = "P";
  80. } else {
  81. initialString += "";
  82. matrix[playerRow][playerCol] = "P";
  83. }
  84. } else {
  85. if (initialString.length() > 0) {
  86. int end = initialString.length() - 1;
  87. initialString = initialString.substring(0, end);
  88. matrix[A2][B2] = "P";
  89. //matrix[playerRow][playerCol] = "-";
  90. }
  91. }
  92. break;
  93. case "right":
  94. matrix[playerRow][playerCol] = "-";
  95. int A3 = playerRow;
  96. int B3 = playerCol;
  97. playerRow += 0;
  98. playerCol += 1;
  99. if (checkIndex(matrix, playerRow, playerCol)) {
  100. if (!matrix[playerRow][playerCol].equals("-")) {
  101. String toAdd = matrix[playerRow][playerCol];
  102. initialString += toAdd;
  103. matrix[playerRow][playerCol] = "P";
  104. } else {
  105. initialString += "";
  106. matrix[playerRow][playerCol] = "P";
  107. }
  108. } else {
  109. if (initialString.length() > 0) {
  110. int end = initialString.length() - 1;
  111. initialString = initialString.substring(0, end);
  112. matrix[A3][B3] = "P";
  113. //matrix[playerRow][playerCol] = "-";
  114. }
  115. }
  116. break;
  117. }
  118. }
  119.  
  120. System.out.println(initialString);
  121. printMatrix(matrix);
  122. }
  123.  
  124. private static void readMatrix(String[][] matrix, Scanner scanner) {
  125. for (int r = 0; r < matrix.length; r++) {
  126. String input = scanner.nextLine();
  127. for (int c = 0; c < matrix[r].length; c++) {
  128. matrix[r][c] = input.charAt(c) + "";
  129. if (matrix[r][c].equals("P")) {
  130. playerRow = r;
  131. playerCol = c;
  132. matrix[r][c] = "-";//TODO check
  133. }
  134. }
  135. }
  136. }
  137.  
  138. private static void printMatrix(String[][] matrix) {
  139. for (int i = 0; i < matrix.length; i++) {
  140. for (int j = 0; j < matrix[i].length; j++) {
  141. System.out.print(matrix[i][j] + "");
  142. }
  143. System.out.println();
  144. }
  145. }
  146.  
  147. private static boolean checkIndex(String[][] matrix, int a, int b) {
  148. return a >= 0 && a < matrix.length && b >= 0 && b < matrix[a].length;
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement