Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Advance.Exams.Exam25June2022;
- import java.util.Scanner;
- public class StickyFingers {
- private static final String DILLINGER = "D";
- private static final String REGULAR_POSITION = "+";
- private static final String HOUSE = "$";
- private static final String POLICE = "P";
- private static final String JAIL = "#";
- private static final String CANNOT_LEAVE_TOWN = "You cannot leave the town, there is police outside!";
- private static final String STOLE_MONEY = "You successfully stole %s$.";
- private static final String ARRESTED = "You got caught with %s$, and you are going to jail.";
- private static final String GOT_ARESTED = "Your last theft has finished successfully with %s$ in your pocket.";
- private static final String NOT_ARESTED = "";
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- String[][] town = new String[n][];
- String[] commands = scanner.nextLine().split(",");
- for (int i = 0; i < town.length; i++) {
- String[] current = scanner.nextLine().split("\\s+");
- town[i] = current;
- }
- int[] currentPosition = getCurrentPosition(town);
- int stolenMoney = 0;
- boolean arested = false;
- for (String command : commands) {
- if (arested) {
- break;
- }
- switch (command) {
- case "up":
- if (currentPosition[0] - 1 < 0) {
- System.out.println(CANNOT_LEAVE_TOWN);
- continue;
- }
- if (town[currentPosition[0] - 1][currentPosition[1]].equals(HOUSE)) {
- int calcMoney = (currentPosition[0] - 1) * currentPosition[1];
- stolenMoney += calcMoney;
- System.out.printf(STOLE_MONEY + "\n", calcMoney);
- town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
- town[currentPosition[0] - 1][currentPosition[1]] = DILLINGER;
- currentPosition[0] = currentPosition[0] - 1;
- } else if (town[currentPosition[0] - 1][currentPosition[1]].equals(POLICE)) {
- town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
- town[currentPosition[0] - 1][currentPosition[1]] = JAIL;
- arested = true;
- } else {
- town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
- town[currentPosition[0] - 1][currentPosition[1]] = DILLINGER;
- currentPosition[0] = currentPosition[0] - 1;
- }
- break;
- case "down":
- if (currentPosition[0] + 1 > town.length - 1) {
- System.out.println(CANNOT_LEAVE_TOWN);
- continue;
- }
- if (town[currentPosition[0] + 1][currentPosition[1]].equals(HOUSE)) {
- int calcMoney = (currentPosition[0] + 1) * currentPosition[1];
- stolenMoney += calcMoney;
- System.out.printf(STOLE_MONEY + "\n", calcMoney);
- town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
- town[currentPosition[0] + 1][currentPosition[1]] = DILLINGER;
- currentPosition[0] = currentPosition[0] + 1;
- } else if (town[currentPosition[0] + 1][currentPosition[1]].equals(POLICE)) {
- town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
- town[currentPosition[0] + 1][currentPosition[1]] = JAIL;
- arested = true;
- } else {
- town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
- town[currentPosition[0] + 1][currentPosition[1]] = DILLINGER;
- currentPosition[0] = currentPosition[0] + 1;
- }
- break;
- case "right":
- if (currentPosition[1] + 1 > town.length - 1) {
- System.out.println(CANNOT_LEAVE_TOWN);
- continue;
- }
- if (town[currentPosition[0]][currentPosition[1] + 1].equals(HOUSE)) {
- int calcMoney = currentPosition[0] * (currentPosition[1] + 1);
- stolenMoney += calcMoney;
- System.out.printf(STOLE_MONEY + "\n", calcMoney);
- town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
- town[currentPosition[0]][currentPosition[1] + 1] = DILLINGER;
- currentPosition[1] = currentPosition[1] + 1;
- } else if (town[currentPosition[0]][currentPosition[1] + 1].equals(POLICE)) {
- town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
- town[currentPosition[0]][currentPosition[1] + 1] = JAIL;
- arested = true;
- } else {
- town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
- town[currentPosition[0]][currentPosition[1] + 1] = DILLINGER;
- currentPosition[1] = currentPosition[1] + 1;
- }
- break;
- case "left":
- if (currentPosition[1] - 1 < 0) {
- System.out.println(CANNOT_LEAVE_TOWN);
- continue;
- }
- if (town[currentPosition[0]][currentPosition[1] - 1].equals(HOUSE)) {
- int calcMoney = currentPosition[0] * (currentPosition[1] - 1);
- stolenMoney += calcMoney;
- System.out.printf(STOLE_MONEY + "\n", calcMoney);
- town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
- town[currentPosition[0]][currentPosition[1] - 1] = DILLINGER;
- currentPosition[1] = currentPosition[1] - 1;
- } else if (town[currentPosition[0]][currentPosition[1] - 1].equals(POLICE)) {
- town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
- town[currentPosition[0]][currentPosition[1] - 1] = JAIL;
- arested = true;
- } else {
- town[currentPosition[0]][currentPosition[1]] = REGULAR_POSITION;
- town[currentPosition[0]][currentPosition[1] - 1] = DILLINGER;
- currentPosition[1] = currentPosition[1] - 1;
- }
- break;
- }
- }
- if (!arested) {
- System.out.printf(GOT_ARESTED + "\n", stolenMoney);
- } else {
- System.out.printf(ARRESTED + "\n", stolenMoney);
- }
- for (int row = 0; row < town.length; row++) {
- System.out.println(String.join(" ", town[row]));
- }
- }
- private static int[] getCurrentPosition(String[][] town) {
- int[] temporary = new int[2];
- for (int row = 0; row < town.length; row++) {
- for (int col = 0; col < town[row].length; col++) {
- if (town[row][col].equals(DILLINGER)) {
- temporary[0] = row;
- temporary[1] = col;
- }
- }
- }
- return temporary;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment