|
| 1 | +import java.util.Scanner; |
| 2 | +import java.util.Random; |
| 3 | + |
| 4 | +public class methods { |
| 5 | + static int inputNum(String msg) { |
| 6 | + Scanner iScanner = new Scanner(System.in); |
| 7 | + System.out.print(msg); |
| 8 | + int num = iScanner.nextInt(); |
| 9 | + iScanner.close(); |
| 10 | + return num; |
| 11 | + } |
| 12 | + |
| 13 | + static int[][] createField(int rows, int columns) { |
| 14 | + int[][] field = new int[rows + 2][columns + 2]; |
| 15 | + |
| 16 | + for (int i = 0; i < field.length; i++) { |
| 17 | + for (int j = 0; j < field[i].length; j++) { |
| 18 | + if (i == 0 || i == rows + 1 || j == 0 || j == columns + 1) |
| 19 | + field[i][j] = -1; |
| 20 | + } |
| 21 | + } |
| 22 | + return field; |
| 23 | + } |
| 24 | + |
| 25 | + static void show2dArray(int[][] array) { |
| 26 | + for (int i = 0; i < array.length * 8; i++) { |
| 27 | + System.out.print("_"); |
| 28 | + } |
| 29 | + System.out.println(); |
| 30 | + for (int i = 0; i < array.length; i++) { |
| 31 | + for (int j = 0; j < array[i].length; j++) { |
| 32 | + if (j != array[i].length - 1) |
| 33 | + System.out.printf("| %d\t", array[i][j]); |
| 34 | + else |
| 35 | + System.out.printf("| %d\t|", array[i][j]); |
| 36 | + } |
| 37 | + System.out.println(); |
| 38 | + for (int k = 0; k < array.length * 8 + 1; k++) { |
| 39 | + if (k % 8 == 0) |
| 40 | + System.out.print("|"); |
| 41 | + else |
| 42 | + System.out.print("_"); |
| 43 | + } |
| 44 | + System.out.println(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + static void buildingWalls(int[][] array, int walls) { |
| 49 | + Random rnd = new Random(); |
| 50 | + for (int i = 0; i < walls; i++) { |
| 51 | + int row = rnd.nextInt(1, array.length - 1); |
| 52 | + int column = rnd.nextInt(1, array[0].length - 1); |
| 53 | + array[row][column] = -1; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + static int drawRoutes(int[][] array) { |
| 58 | + int step = 1; |
| 59 | + for (; step < array.length * array[0].length - 2 * (array.length + array[0].length - 2); step++) { |
| 60 | + for (int i = 1; i < array.length - 1; i++) { |
| 61 | + for (int j = 1; j < array[i].length - 1; j++) { |
| 62 | + if (array[i][j] == step) { |
| 63 | + if (array[i + 1][j] == -2 || array[i - 1][j] == -2 || array[i][j + 1] == -2 |
| 64 | + || array[i][j - 1] == -2) |
| 65 | + return step; |
| 66 | + else { |
| 67 | + if (array[i - 1][j] == 0) |
| 68 | + array[i - 1][j] = step + 1; |
| 69 | + if (array[i + 1][j] == 0) |
| 70 | + array[i + 1][j] = step + 1; |
| 71 | + if (array[i][j + 1] == 0) |
| 72 | + array[i][j + 1] = step + 1; |
| 73 | + if (array[i][j - 1] == 0) |
| 74 | + array[i][j - 1] = step + 1; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + return step; |
| 81 | + } |
| 82 | + |
| 83 | + static String writeRoute(int[][] array, int[] finish, int step) { |
| 84 | + String route = String.format("(%d;%d) ", finish[0], finish[1]); |
| 85 | + for (int i = 0, value =
A3DB
step; i < step; i++, value--) { |
| 86 | + if (array[finish[0]][finish[1] - 1] == value) |
| 87 | + finish[1]--; |
| 88 | + else if (array[finish[0]][finish[1] + 1] == value) |
| 89 | + finish[1]++; |
| 90 | + else if (array[finish[0] + 1][finish[1]] == value) |
| 91 | + finish[0]++; |
| 92 | + else |
| 93 | + finish[0]--; |
| 94 | + route += String.format("(%d;%d) ", finish[0], finish[1]); |
| 95 | + } |
| 96 | + return route; |
| 97 | + } |
| 98 | + |
| 99 | + static void ReverseArray(String[] array){ |
| 100 | + for (int i = 0; i < array.length / 2; i++) { |
| 101 | + String helper = array[i]; |
| 102 | + array[i] = array[array.length - 1 - i]; |
| 103 | + array[array.length - 1 - i] = helper; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + static void showArray(String[] array){ |
| 108 | + for (int i = 0; i < array.length; i++) { |
| 109 | + System.out.print(array[i] + " "); |
| 110 | + } |
| 111 | + System.out.println(); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments