Advertisement
Didart

Sticky Fingers

Jan 26th, 2023
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. package MultidimensionalArrays2;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class StickyFingers {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int size = Integer.parseInt(scanner.nextLine());
  10.         String[] directions = scanner.nextLine().split(",");
  11.  
  12.         String[][] matrix = new String[size][size];
  13.         fillMatrix(matrix, scanner);
  14.  
  15.         int rowThief = -1;
  16.         int colThief = -1;
  17.  
  18.         for (int row = 0; row < size; row++) {
  19.             for (int col = 0; col < size; col++) {
  20.                 if (matrix[row][col].equals("D")) {
  21.                     rowThief = row;
  22.                     colThief = col;
  23.                     break;
  24.                 }
  25.             }
  26.         }
  27.         int totalStolenMoney = 0;
  28.         for (String direction : directions) {
  29.             switch (direction) {
  30.                 case "left":
  31.                     if (colThief - 1 < 0) {
  32.                         System.out.println("You cannot leave the town, there is police outside!");
  33.                     } else {
  34.                         matrix[rowThief][colThief] = "+";
  35.                         colThief--;
  36.                     }
  37.                     break;
  38.                 case "right":
  39.                     if (colThief + 1 >= size) {
  40.                         System.out.println("You cannot leave the town, there is police outside!");
  41.                     } else {
  42.                         matrix[rowThief][colThief] = "+";
  43.                         colThief++;
  44.                     }
  45.                     break;
  46.                 case "up":
  47.                     if (rowThief - 1 < 0) {
  48.                         System.out.println("You cannot leave the town, there is police outside!");
  49.                     } else {
  50.                         matrix[rowThief][colThief] = "+";
  51.                         rowThief--;
  52.                     }
  53.                     break;
  54.                 case "down":
  55.                     if (rowThief + 1 >= size) {
  56.                         System.out.println("You cannot leave the town, there is police outside!");
  57.                     } else {
  58.                         matrix[rowThief][colThief] = "+";
  59.                         rowThief++;
  60.                     }
  61.                     break;
  62.             }
  63.  
  64.             String currentPosition = matrix[rowThief][colThief];
  65.             if (currentPosition.equals("$")) {
  66.                 int stolenMoney = rowThief * colThief;
  67.                 System.out.printf("You successfully stole %d$.%n", stolenMoney);
  68.                 totalStolenMoney += stolenMoney;
  69.                 matrix[rowThief][colThief] = "D";
  70.             } else if (currentPosition.equals("P")) {
  71.                 System.out.printf("You got caught with %d$, and you are going to jail.%n", totalStolenMoney);
  72.                 matrix[rowThief][colThief] = "#";
  73.                 printMatrix(matrix);
  74.                 return;
  75.             } else if (currentPosition.equals("+")) {
  76.                 matrix[rowThief][colThief] = "D";
  77.             }
  78.         }
  79.  
  80.         System.out.printf("Your last theft has finished successfully with %d$ in your pocket.%n", totalStolenMoney);
  81.         printMatrix(matrix);
  82.     }
  83.  
  84.     private static void fillMatrix(String[][] matrix, Scanner scanner) {
  85.         for (int row = 0; row < matrix.length; row++) {
  86.             matrix[row] = scanner.nextLine().split("\\s+");
  87.         }
  88.     }
  89.  
  90.     private static void printMatrix(String[][] matrix) {
  91.         for (int row = 0; row < matrix.length; row++) {
  92.             for (int col = 0; col < matrix.length; col++) {
  93.                 System.out.print(matrix[row][col] + " ");
  94.             }
  95.             System.out.println();
  96.         }
  97.     }
  98. }
  99.  
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement