Advertisement
Guest User

Untitled

a guest
Jun 24th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 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. playerRow -= 1;
  23. playerCol += 0;
  24. if (checkIndex(matrix, playerRow, playerCol)) {
  25. if (!matrix[playerRow][playerCol].equals("-")) {
  26. String toAdd = matrix[playerRow][playerCol];
  27. initialString += toAdd;
  28. matrix[playerRow][playerCol] = "P";
  29. } else {
  30. initialString += "";
  31. //matrix[playerRow][playerCol] = "P";
  32. }
  33. } else {
  34. int end = initialString.length() - 1;
  35. initialString = initialString.substring(0, end);
  36. }
  37. break;
  38. case "down":
  39. playerRow += 1;
  40. playerCol += 0;
  41. if (checkIndex(matrix, playerRow, playerCol)) {
  42. if (!matrix[playerRow][playerCol].equals("-")) {
  43. String toAdd = matrix[playerRow][playerCol];
  44. initialString += toAdd;
  45. matrix[playerRow][playerCol] = "P";
  46. } else {
  47. initialString += "";
  48. //matrix[playerRow][playerCol] = "P";
  49. }
  50. } else {
  51. int end = initialString.length() - 1;
  52. initialString = initialString.substring(0, end);
  53. }
  54. break;
  55. case "left":
  56. playerRow += 0;
  57. playerCol -= 1;
  58. if (checkIndex(matrix, playerRow, playerCol)) {
  59. if (!matrix[playerRow][playerCol].equals("-")) {
  60. String toAdd = matrix[playerRow][playerCol];
  61. initialString += toAdd;
  62. matrix[playerRow][playerCol] = "P";
  63. } else {
  64. initialString += "";
  65. //matrix[playerRow][playerCol] = "P";
  66. }
  67. } else {
  68. int end = initialString.length() - 1;
  69. initialString = initialString.substring(0, end);
  70. }
  71. break;
  72. case "right":
  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. int end = initialString.length() - 1;
  86. initialString = initialString.substring(0, end);
  87. }
  88. break;
  89. }
  90. }
  91. System.out.println(initialString);
  92. printMatrix(matrix);
  93. }
  94.  
  95. private static void readMatrix(String[][] matrix, Scanner scanner) {
  96. for (int r = 0; r < matrix.length; r++) {
  97. String input = scanner.nextLine();
  98. for (int c = 0; c < matrix[r].length; c++) {
  99. matrix[r][c] = input.charAt(c) + "";
  100. if (matrix[r][c].equals("P")) {
  101. playerRow = r;
  102. playerCol = c;
  103. matrix[r][c]="-";//TODO check
  104. }
  105. }
  106. }
  107. }
  108. private static void printMatrix(String[][] matrix) {
  109. for (int i = 0; i < matrix.length; i++) {
  110. for (int j = 0; j < matrix[i].length; j++) {
  111. System.out.print(matrix[i][j] + "");
  112. }
  113. System.out.println();
  114. }
  115. }
  116. private static boolean checkIndex(String[][] matrix, int a, int b) {
  117. return a >= 0 && a < matrix.length && b >= 0 && b < matrix[a].length;
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement