Advertisement
Guest User

BookWorm

a guest
Aug 2nd, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class LootBox {
  6.  
  7.  
  8. public static void main(String[] args) throws IOException {
  9. BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11. String initialString = bf.readLine();
  12.  
  13. StringBuilder stringBuilder = new StringBuilder(initialString);
  14.  
  15. int n = Integer.parseInt(bf.readLine());
  16.  
  17. String[][] matrix = new String[n][n];
  18. int playerRows = 0;
  19. int playerCows = 0;
  20.  
  21. for (int i = 0; i < matrix.length; i++) {
  22. matrix[i] = bf.readLine().split("");
  23. for (int j = 0; j < matrix[i].length; j++) {
  24. if (matrix[i][j].equals("P")) {
  25. playerRows = i;
  26. playerCows = i;
  27. }
  28. }
  29. }
  30. matrix[playerRows][playerCows] = "-";
  31.  
  32.  
  33. String command;
  34. while (!(command = bf.readLine()).equals("end")) {
  35.  
  36. switch (command) {
  37. case "up":
  38. playerRows--;
  39. if (isValid(matrix, playerRows, playerCows)) {
  40. String current = "";
  41. if (!matrix[playerRows][playerCows].equals("-")) {
  42. current+=matrix[playerRows][playerCows];
  43. stringBuilder.append(current);
  44. matrix[playerRows][playerCows] = "-";
  45. }
  46. } else {
  47. playerCows++;
  48. if(stringBuilder.length()>0) {
  49.  
  50. stringBuilder.deleteCharAt(stringBuilder.length()-1);
  51. }
  52. }
  53. break;
  54. case "down":
  55. playerRows++;
  56. if (isValid(matrix, playerRows, playerCows)) {
  57. String current = "";
  58. if (!matrix[playerRows][playerCows].equals("-")) {
  59. current+=matrix[playerRows][playerCows];
  60. stringBuilder.append(current);
  61. matrix[playerRows][playerCows] = "-";
  62. }
  63. } else {
  64. playerCows++;
  65. if(stringBuilder.length()>0) {
  66.  
  67. stringBuilder.deleteCharAt(stringBuilder.length()-1);
  68. }
  69. }
  70. break;
  71. case "left":
  72. playerCows--;
  73. if (isValid(matrix, playerRows, playerCows)) {
  74. String current = "";
  75. if (!matrix[playerRows][playerCows].equals("-")) {
  76. current+=matrix[playerRows][playerCows];
  77. stringBuilder.append(current);
  78. matrix[playerRows][playerCows] = "-";
  79. }
  80. } else {
  81. playerCows++;
  82. if(stringBuilder.length()>0) {
  83.  
  84. stringBuilder.deleteCharAt(stringBuilder.length()-1);
  85. }
  86. }
  87. break;
  88. case "right":
  89. playerCows++;
  90. if (isValid(matrix, playerRows, playerCows)) {
  91. String current = "";
  92. if (!matrix[playerRows][playerCows].equals("-")) {
  93. current+=matrix[playerRows][playerCows];
  94. stringBuilder.append(current);
  95. matrix[playerRows][playerCows] = "-";
  96. }
  97. } else {
  98. playerCows++;
  99. if(stringBuilder.length()>0) {
  100.  
  101. stringBuilder.deleteCharAt(stringBuilder.length()-1);
  102. }
  103. }
  104. break;
  105. }
  106. }
  107. matrix[playerRows][playerCows] = "P";
  108. System.out.println(stringBuilder);
  109. printMatrix(matrix);
  110. }
  111.  
  112. public static void printMatrix(String[][] mx) {
  113. for (int i = 0; i < mx.length; i++) {
  114. for (int j = 0; j < mx[i].length; j++) {
  115. System.out.print(mx[i][j]);
  116. }
  117. System.out.println();
  118. }
  119. }
  120.  
  121. public static boolean isValid(String[][] matrix, int row, int col) {
  122. return row >= 0 && row < matrix.length && col >= 0 && col < matrix.length;
  123. }
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement