Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class FishingCompetition {
- private static final char FISHER = 'S';
- private static final int DISCARD = 5;
- private static int CATCH = 0;
- private static final char WHIRLPOOL = 'W';
- private static final char SEA_STORM = '@';
- private static final char EMPTY_POSITION = '-';
- private static boolean FALL_INTO_STORM = false;
- private static boolean FALL_INTO_WHIRLPOOL = false;
- private static boolean SUCCESS = false;
- private static final String FINAL = "collect the nets";
- private static final String UP = "up";
- private static final String DOWN = "down";
- private static final String LEFT = "left";
- private static final String RIGHT = "right";
- private static final String FALL_WHIRLPOOL = "You fell into a whirlpool! The ship sank and you lost the fish you caught. Last coordinates of the ship: [%s,%s]\n";
- private static final String FALL_STORM = "You fell into a sea storm! The ship will be moved to the beginning of the fishing area.\n";
- private static final String SUCCEEDED = "Success! You managed to reach the quota!";
- private static final String NOT_SUCCEEDED = "You didn't catch enough fish and didn't reach the quota! You need %d tons of fish more.\n";
- private static final String AMOUNT_OUTPUT = "Amount of fish caught: %d tons.\n";
- public FishingCompetition() {
- }
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int rows = Integer.parseInt(reader.readLine());
- char[][] fishingArea = getSea(rows, reader);
- int[] currentPosition = getCurrentPosition(fishingArea);
- boolean storm = true;
- String command;
- while(!"collect the nets".equals(command = reader.readLine()) && storm) {
- fishingArea[currentPosition[0]][currentPosition[1]] = '-';
- int var10002;
- switch (command) {
- case "up":
- if (currentPosition[0] - 1 < 0) {
- currentPosition[0] = fishingArea.length - 1;
- } else {
- var10002 = currentPosition[0]--;
- }
- storm = moveFisher(currentPosition, fishingArea);
- break;
- case "down":
- if (currentPosition[0] + 1 >= fishingArea.length) {
- currentPosition[0] = 0;
- } else {
- var10002 = currentPosition[0]++;
- }
- storm = moveFisher(currentPosition, fishingArea);
- break;
- case "left":
- if (currentPosition[1] - 1 < 0) {
- currentPosition[1] = fishingArea[currentPosition[0]].length - 1;
- } else {
- var10002 = currentPosition[1]--;
- }
- storm = moveFisher(currentPosition, fishingArea);
- break;
- case "right":
- if (currentPosition[1] + 1 >= fishingArea[currentPosition[0]].length) {
- currentPosition[1] = 0;
- } else {
- var10002 = currentPosition[1]++;
- }
- storm = moveFisher(currentPosition, fishingArea);
- }
- }
- if (!FALL_INTO_WHIRLPOOL) {
- if (SUCCESS) {
- System.out.println("Success! You managed to reach the quota!");
- } else {
- System.out.printf("You didn't catch enough fish and didn't reach the quota! You need %d tons of fish more.\n", 20 - CATCH);
- }
- if (CATCH > 0) {
- System.out.printf("Amount of fish caught: %d tons.\n", CATCH);
- }
- if (!FALL_INTO_WHIRLPOOL) {
- printFishArea(fishingArea);
- }
- }
- }
- private static void printFishArea(char[][] fishingArea) {
- char[][] var1 = fishingArea;
- int var2 = fishingArea.length;
- for(int var3 = 0; var3 < var2; ++var3) {
- char[] row = var1[var3];
- char[] var5 = row;
- int var6 = row.length;
- for(int var7 = 0; var7 < var6; ++var7) {
- char col = var5[var7];
- System.out.print(col);
- }
- System.out.println();
- }
- }
- private static boolean moveFisher(int[] currentPosition, char[][] fishingArea) {
- if (Character.isDigit(fishingArea[currentPosition[0]][currentPosition[1]])) {
- CATCH += Integer.parseInt(String.valueOf(fishingArea[currentPosition[0]][currentPosition[1]]));
- SUCCESS = CATCH >= 20;
- } else {
- if (fishingArea[currentPosition[0]][currentPosition[1]] == 'W') {
- FALL_INTO_WHIRLPOOL = true;
- System.out.printf("You fell into a whirlpool! The ship sank and you lost the fish you caught. Last coordinates of the ship: [%s,%s]\n", currentPosition[0], currentPosition[1]);
- return false;
- }
- if (fishingArea[currentPosition[0]][currentPosition[1]] == '@') {
- System.out.print("You fell into a sea storm! The ship will be moved to the beginning of the fishing area.\n");
- fishingArea[currentPosition[0]][currentPosition[1]] = '-';
- currentPosition[0] = 0;
- currentPosition[1] = 0;
- CATCH = Math.max(0, CATCH - 5);
- SUCCESS = CATCH >= 20;
- }
- }
- fishingArea[currentPosition[0]][currentPosition[1]] = 'S';
- return true;
- }
- private static int[] getCurrentPosition(char[][] sea) {
- int[] coordinates = new int[2];
- for(int r = 0; r < sea.length; ++r) {
- for(int col = 0; col < sea[r].length; ++col) {
- if (sea[r][col] == 'S') {
- return new int[]{r, col};
- }
- }
- }
- return coordinates;
- }
- private static char[][] getSea(int rows, BufferedReader reader) throws IOException {
- char[][] toReturn = new char[rows][];
- for(int i = 0; i < toReturn.length; ++i) {
- toReturn[i] = reader.readLine().toCharArray();
- }
- return toReturn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment