Advertisement
cankocanko

02. BookWorm

Aug 6th, 2020
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6. public static void main(String[] args) {
  7. Scanner sc = new Scanner(System.in);
  8.  
  9. StringBuilder word = new StringBuilder(sc.nextLine());
  10. int size = Integer.parseInt(sc.nextLine());
  11.  
  12. String[][] matrix = new String[size][size];
  13.  
  14. for (int rows = 0; rows < matrix.length; rows++) {
  15. String[] arr = sc.nextLine().split("");
  16. matrix[rows] = arr;
  17. }
  18. String command = sc.nextLine();
  19. int playerRow = 0;
  20. int playerCol = 0;
  21.  
  22. while (!"end".equals(command)) {
  23. for (int rows = 0; rows < matrix.length; rows++) {
  24. for (int cols = 0; cols < matrix[rows].length; cols++) {
  25. if (matrix[rows][cols].equals("P")) {
  26. playerRow = rows;
  27. playerCol = cols;
  28. }
  29. }
  30. }
  31. int newField = 0;
  32. switch (command) {
  33. case "up":
  34. newField = moveUp(matrix, playerRow, playerCol);
  35. if (newField == playerRow) {
  36. word.deleteCharAt(word.length() - 1);
  37. } else {
  38. if (!matrix[newField][playerCol].equals("-")) {
  39. word.append(matrix[newField][playerCol]);
  40. matrix[playerRow][playerCol] = "-";
  41. matrix[newField][playerCol] = "P";
  42. } else {
  43. matrix[playerRow][playerCol] = "-";
  44. matrix[newField][playerCol] = "P";
  45. }
  46. }
  47. break;
  48. case "down":
  49. newField = moveDown(matrix, playerRow, playerCol);
  50. if (newField == playerRow) {
  51. word.deleteCharAt(word.length() - 1);
  52. } else {
  53. if (!matrix[newField][playerCol].equals("-")) {
  54. word.append(matrix[newField][playerCol]);
  55. matrix[playerRow][playerCol] = "-";
  56. matrix[newField][playerCol] = "P";
  57. } else {
  58. matrix[playerRow][playerCol] = "-";
  59. matrix[newField][playerCol] = "P";
  60. }
  61. }
  62. break;
  63. case "left":
  64. newField = moveLeft(matrix, playerRow, playerCol);
  65. if (newField == playerCol) {
  66. word.deleteCharAt(word.length() - 1);
  67. } else {
  68. if (!matrix[playerRow][newField].equals("-")) {
  69. word.append(matrix[playerRow][newField]);
  70. matrix[playerRow][playerCol] = "-";
  71. matrix[playerRow][newField] = "P";
  72. } else {
  73. matrix[playerRow][playerCol] = "-";
  74. matrix[playerRow][newField] = "P";
  75. }
  76. }
  77. break;
  78. case "right":
  79. newField = moveRight(matrix, playerRow, playerCol);
  80. if (newField == playerCol) {
  81. word.deleteCharAt(word.length() - 1);
  82. } else {
  83. if (!matrix[playerRow][newField].equals("-")) {
  84. word.append(matrix[playerRow][newField]);
  85. matrix[playerRow][playerCol] = "-";
  86. matrix[playerRow][newField] = "P";
  87. } else {
  88. matrix[playerRow][playerCol] = "-";
  89. matrix[playerRow][newField] = "P";
  90. }
  91. }
  92. break;
  93. }
  94. command = sc.nextLine();
  95. if ("end".equals(command)) {
  96. System.out.println(word);
  97. for (int rows = 0; rows < matrix.length; rows++) {
  98. for (int cols = 0; cols < matrix[rows].length; cols++) {
  99. System.out.print(matrix[rows][cols]);
  100. }
  101. System.out.println();
  102. }
  103. }
  104. }
  105.  
  106. }
  107.  
  108. public static int moveUp(String[][] matrix, int playerRow, int playerCol) {
  109. if (playerRow > 0) {
  110. playerRow -= 1;
  111. }
  112. return playerRow;
  113. }
  114.  
  115. public static int moveDown(String[][] matrix, int playerRow, int playerCol) {
  116. if (playerRow < matrix.length - 1) {
  117. playerRow += 1;
  118. }
  119. return playerRow;
  120. }
  121.  
  122. public static int moveLeft(String[][] matrix, int playerRow, int playerCol) {
  123. if (playerCol > 0) {
  124. playerCol -= 1;
  125. }
  126. return playerCol;
  127. }
  128.  
  129. public static int moveRight(String[][] matrix, int playerRow, int playerCol) {
  130. if (playerCol < matrix.length - 1) {
  131. playerCol += 1;
  132. }
  133. return playerCol;
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement