borovaneca

StickyFingers

May 26th, 2023
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.71 KB | None | 0 0
  1. package Advance.Exams.Exam25June2022;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class StickyFingers {
  6.  
  7.     private static final String DILLINGER = "D";
  8.     private static final String REGULAR_POSITION = "+";
  9.     private static final String HOUSE = "$";
  10.     private static final String POLICE = "P";
  11.     private static final String JAIL = "#";
  12.     private static final String CANNOT_LEAVE_TOWN = "You cannot leave the town, there is police outside!";
  13.     private static final String STOLE_MONEY = "You successfully stole %s$.";
  14.     private static final String ARRESTED = "You got caught with %s$, and you are going to jail.";
  15.     private static final String GOT_ARESTED = "Your last theft has finished successfully with %s$ in your pocket.";
  16.     private static final String NOT_ARESTED = "";
  17.  
  18.     public static void main(String[] args) {
  19.         Scanner scanner = new Scanner(System.in);
  20.  
  21.         int n = Integer.parseInt(scanner.nextLine());
  22.         String[][] town = new String[n][];
  23.         String[] commands = scanner.nextLine().split(",");
  24.         for (int i = 0; i < town.length; i++) {
  25.             String[] current = scanner.nextLine().split("\\s+");
  26.             town[i] = current;
  27.         }
  28.  
  29.         int[] currentPosition = getCurrentPosition(town);
  30.         int stolenMoney = 0;
  31.         boolean arested = false;
  32.         for (String command : commands) {
  33.             if (arested) {
  34.                 break;
  35.             }
  36.             switch (command) {
  37.                 case "up":
  38.                     if (currentPosition[0] - 1 < 0) {
  39.                         System.out.println(CANNOT_LEAVE_TOWN);
  40.                         continue;
  41.                     }
  42.                     if (town[currentPosition[0] - 1][currentPosition[1]].equals(HOUSE)) {
  43.                         int calcMoney = (currentPosition[0] - 1) * currentPosition[1];
  44.                         stolenMoney += calcMoney;
  45.                         System.out.printf(STOLE_MONEY + "\n", calcMoney);
  46.                         town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
  47.                         town[currentPosition[0] - 1][currentPosition[1]] = DILLINGER;
  48.                         currentPosition[0] = currentPosition[0] - 1;
  49.                     } else if (town[currentPosition[0] - 1][currentPosition[1]].equals(POLICE)) {
  50.                         town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
  51.                         town[currentPosition[0] - 1][currentPosition[1]] = JAIL;
  52.                         arested = true;
  53.                     } else {
  54.                         town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
  55.                         town[currentPosition[0] - 1][currentPosition[1]] = DILLINGER;
  56.                         currentPosition[0] = currentPosition[0] - 1;
  57.                     }
  58.                     break;
  59.                 case "down":
  60.                     if (currentPosition[0] + 1 > town.length - 1) {
  61.                         System.out.println(CANNOT_LEAVE_TOWN);
  62.                         continue;
  63.                     }
  64.                     if (town[currentPosition[0] + 1][currentPosition[1]].equals(HOUSE)) {
  65.                         int calcMoney = (currentPosition[0] + 1) * currentPosition[1];
  66.                         stolenMoney += calcMoney;
  67.                         System.out.printf(STOLE_MONEY + "\n", calcMoney);
  68.                         town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
  69.                         town[currentPosition[0] + 1][currentPosition[1]] = DILLINGER;
  70.                         currentPosition[0] = currentPosition[0] + 1;
  71.                     } else if (town[currentPosition[0] + 1][currentPosition[1]].equals(POLICE)) {
  72.                         town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
  73.                         town[currentPosition[0] + 1][currentPosition[1]] = JAIL;
  74.                         arested = true;
  75.                     } else {
  76.                         town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
  77.                         town[currentPosition[0] + 1][currentPosition[1]] = DILLINGER;
  78.                         currentPosition[0] = currentPosition[0] + 1;
  79.                     }
  80.                     break;
  81.                 case "right":
  82.                     if (currentPosition[1] + 1 > town.length - 1) {
  83.                         System.out.println(CANNOT_LEAVE_TOWN);
  84.                         continue;
  85.                     }
  86.                     if (town[currentPosition[0]][currentPosition[1] + 1].equals(HOUSE)) {
  87.                         int calcMoney = currentPosition[0] * (currentPosition[1] + 1);
  88.                         stolenMoney += calcMoney;
  89.                         System.out.printf(STOLE_MONEY + "\n", calcMoney);
  90.                         town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
  91.                         town[currentPosition[0]][currentPosition[1] + 1] = DILLINGER;
  92.                         currentPosition[1] = currentPosition[1] + 1;
  93.                     } else if (town[currentPosition[0]][currentPosition[1] + 1].equals(POLICE)) {
  94.                         town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
  95.                         town[currentPosition[0]][currentPosition[1] + 1] = JAIL;
  96.                         arested = true;
  97.                     } else {
  98.                         town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
  99.                         town[currentPosition[0]][currentPosition[1] + 1] = DILLINGER;
  100.                         currentPosition[1] = currentPosition[1] + 1;
  101.                     }
  102.                     break;
  103.                 case "left":
  104.                     if (currentPosition[1] - 1 < 0) {
  105.                         System.out.println(CANNOT_LEAVE_TOWN);
  106.                         continue;
  107.                     }
  108.                     if (town[currentPosition[0]][currentPosition[1] - 1].equals(HOUSE)) {
  109.                         int calcMoney = currentPosition[0] * (currentPosition[1] - 1);
  110.                         stolenMoney += calcMoney;
  111.                         System.out.printf(STOLE_MONEY + "\n", calcMoney);
  112.                         town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
  113.                         town[currentPosition[0]][currentPosition[1] - 1] = DILLINGER;
  114.                         currentPosition[1] = currentPosition[1] - 1;
  115.                     } else if (town[currentPosition[0]][currentPosition[1] - 1].equals(POLICE)) {
  116.                         town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
  117.                         town[currentPosition[0]][currentPosition[1] - 1] = JAIL;
  118.                         arested = true;
  119.                     } else {
  120.                         town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
  121.                         town[currentPosition[0]][currentPosition[1] - 1] = DILLINGER;
  122.                         currentPosition[1] = currentPosition[1] - 1;
  123.                     }
  124.                     break;
  125.             }
  126.         }
  127.  
  128.         if (!arested) {
  129.             System.out.printf(GOT_ARESTED + "\n", stolenMoney);
  130.         } else {
  131.             System.out.printf(ARRESTED + "\n", stolenMoney);
  132.         }
  133.  
  134.         for (int row = 0; row < town.length; row++) {
  135.             System.out.println(String.join(" ", town[row]));
  136.         }
  137.     }
  138.  
  139.     private static int[] getCurrentPosition(String[][] town) {
  140.         int[] temporary = new int[2];
  141.  
  142.         for (int row = 0; row < town.length; row++) {
  143.             for (int col = 0; col < town[row].length; col++) {
  144.                 if (town[row][col].equals(DILLINGER)) {
  145.                     temporary[0] = row;
  146.                     temporary[1] = col;
  147.                 }
  148.             }
  149.         }
  150.         return temporary;
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment