Advertisement
desislava_topuzakova

02. Sticky Fingers

May 21st, 2023
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. package FunctionalProgramming;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class StickyFingers_02 {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. int size = Integer.parseInt(scanner.nextLine()); //брой редове = брой колони
  9. String [] directions = scanner.nextLine().split(",");
  10. //"up,right,down,down,left".split(",") -> ["up", "right", "down", "down", "left"]
  11.  
  12. //1. приготвяме полето (матрица)
  13. String [][] matrix = new String[size][size];
  14. fillMatrix(matrix, scanner);
  15.  
  16. //2. намираме от къде започва да се движи крадецът
  17. int rowThief = -1; //текущ ред на крадец
  18. int colThief = -1; //текущ колона на крадец
  19.  
  20. for (int row = 0; row < size; row++) {
  21. for (int col = 0; col < size; col++) {
  22. if (matrix[row][col].equals("D")) {
  23. rowThief = row;
  24. colThief = col;
  25. break;
  26. }
  27. }
  28. }
  29.  
  30. int totalStolenMoney = 0; //общо откраднати пари
  31. //3. изпълняваме посоките
  32. for (String direction : directions) {
  33. //direction = left/right/up/down
  34. switch(direction) {
  35. case "left":
  36. if (colThief - 1 < 0) {
  37. //излизам от матрицата
  38. System.out.println("You cannot leave the town, there is police outside!");
  39. } else {
  40. //мога да се придвижа
  41. matrix[rowThief][colThief] = "+";
  42. //премествам
  43. colThief--;
  44. }
  45. break;
  46. case "right":
  47. if (colThief + 1 >= size) {
  48. //излизам от матрицата
  49. System.out.println("You cannot leave the town, there is police outside!");
  50. } else {
  51. //мога да се придвижа
  52. matrix[rowThief][colThief] = "+";
  53. //премествам
  54. colThief++;
  55. }
  56. break;
  57. case "up":
  58. if (rowThief - 1 < 0) {
  59. //излизам от матрицата
  60. System.out.println("You cannot leave the town, there is police outside!");
  61. } else {
  62. //мога да се придвижа
  63. matrix[rowThief][colThief] = "+";
  64. //премествам
  65. rowThief--;
  66. }
  67. break;
  68. case "down":
  69. if (rowThief + 1 >= size) {
  70. //излизам от матрицата
  71. System.out.println("You cannot leave the town, there is police outside!");
  72. } else {
  73. //мога да се придвижа
  74. matrix[rowThief][colThief] = "+";
  75. //преместваме
  76. rowThief++;
  77. }
  78. break;
  79. }
  80. //знам къде се е преместил крадецът
  81. //ред: rowThief, колона: colThief
  82.  
  83. String currentPosition = matrix[rowThief][colThief]; //на какво е стъпил моя крадец
  84. if (currentPosition.equals("$")) {
  85. //стигнали сме къща -> ограбваме
  86. int stolenMoney = rowThief * colThief; //колко пари са откраднати от тази къща
  87. System.out.printf("You successfully stole %d$.%n", stolenMoney);
  88. totalStolenMoney += stolenMoney;
  89. matrix[rowThief][colThief] = "D";
  90. } else if (currentPosition.equals("P")) {
  91. //стигнали полиция -> хващат ни и прекратяваме
  92. System.out.printf("You got caught with %d$, and you are going to jail.%n", totalStolenMoney);
  93. matrix[rowThief][colThief] = "#";
  94. printMatrix(matrix);
  95. return;
  96. } else if (currentPosition.equals("+")) {
  97. matrix[rowThief][colThief] = "D";
  98. }
  99.  
  100. }
  101. //минаваме през всички посоки и не сме хванати
  102. System.out.printf("Your last theft has finished successfully with %d$ in your pocket.%n", totalStolenMoney);
  103. printMatrix(matrix);
  104. }
  105.  
  106. private static void fillMatrix(String[][] matrix, Scanner scanner) {
  107. for (int row = 0; row < matrix.length ; row++) {
  108. //scanner.nextLine() -> "1 2 3"
  109. //scanner.nextLine().split(" ") -> ["1", "2", "3"]
  110. matrix[row] = scanner.nextLine().split("\\s+");
  111. }
  112. }
  113.  
  114. private static void printMatrix(String[][] matrix) {
  115. for (int row = 0; row < matrix.length; row++) {
  116. for (int col = 0; col < matrix.length; col++) {
  117. System.out.print(matrix[row][col] + " ");
  118. }
  119. System.out.println(); //свали курсора на следващия ред
  120. }
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement