Advertisement
desislava_topuzakova

Untitled

Jun 20th, 2023
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. import java.io.Console;
  2. import java.util.*;
  3. import java.util.stream.Collectors;
  4.  
  5. public class Ex2 {
  6.  
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10. String[] dimensions = scanner.nextLine()
  11. .split(",", -1);
  12.  
  13. int[] dimentions = new int[dimensions.length];
  14. for (int i = 0; i < dimensions.length; i++) {
  15. dimentions[i] = Integer.parseInt(dimensions[i]);
  16. }
  17.  
  18. String[][] cupboard = new String[dimentions[0]][dimentions[1]];
  19. int mouseRow = -1;
  20. int mouseCol = -1;
  21. int totalCheeseNumber = 0;
  22.  
  23. for (int i = 0; i < cupboard.length; i++) {
  24. String newRow = scanner.nextLine();
  25. for (int j = 0; j < cupboard[i].length; j++) {
  26. cupboard[i][j] = String.valueOf(newRow.charAt(j));
  27.  
  28. if (newRow.charAt(j) == 'M') {
  29. mouseRow = i;
  30. mouseCol = j;
  31. cupboard[mouseRow][mouseCol] = "*";
  32. }
  33. if (cupboard[i][j].equals("C")) {
  34. totalCheeseNumber++;
  35. }
  36. }
  37. }
  38. String command;
  39. while (!(command = scanner.nextLine()).equals("danger")) {
  40. if ((command.equals("left") && mouseCol == 0) ||
  41. (command.equals("right") && mouseCol == cupboard[0].length - 1) ||
  42. (command.equals("up") && mouseRow == 0) ||
  43. (command.equals("down") && mouseRow == cupboard.length - 1)) {
  44. System.out.println("No more cheese for tonight!");
  45. break;
  46. } else {
  47. if ((command.equals("left") && cupboard[mouseRow][mouseCol - 1].equals("@")) ||
  48. (command.equals("right") && cupboard[mouseRow][mouseCol + 1].equals("@")) ||
  49. (command.equals("up") && cupboard[mouseRow - 1][mouseCol].equals("@")) ||
  50. (command.equals("down") && cupboard[mouseRow + 1][mouseCol].equals("@"))) {
  51. continue;
  52. } else {
  53. if (command.equals("left")) {
  54. mouseCol--;
  55. } else if (command.equals("right")) {
  56. mouseCol++;
  57. } else if (command.equals("up")) {
  58. mouseRow--;
  59. } else if (command.equals("down")) {
  60. mouseRow++;
  61. }
  62.  
  63. if (cupboard[mouseRow][mouseCol].equals("C")) {
  64. totalCheeseNumber--;
  65. cupboard[mouseRow][mouseCol] = "*";
  66. if (totalCheeseNumber == 0) {
  67. cupboard[mouseRow][mouseCol] = "M";
  68. System.out.println("Happy mouse! All the cheese is eaten, good night!");
  69. break;
  70. }
  71. continue;
  72. }
  73. if (cupboard[mouseRow][mouseCol].equals("T")) {
  74. System.out.println("Mouse is trapped!");
  75. break;
  76. }
  77. }
  78. }
  79. }
  80.  
  81. if (command.equals("danger")) {
  82. System.out.println("Mouse will come back later!");
  83. }
  84. cupboard[mouseRow][mouseCol] = "M";
  85.  
  86. for (int i = 0; i < cupboard.length; i++) {
  87. for (int j = 0; j < cupboard[i].length; j++) {
  88. System.out.print(cupboard[i][j]);
  89. }
  90. System.out.println();
  91. }
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement