|
| 1 | +package com.baeldung.algorithms.maze.solver; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileNotFoundException; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Scanner; |
| 8 | + |
| 9 | +public class Maze { |
| 10 | + private static final int ROAD = 0; |
| 11 | + private static final int WALL = 1; |
| 12 | + private static final int START = 2; |
| 13 | + private static final int EXIT = 3; |
| 14 | + private static final int PATH = 4; |
| 15 | + |
| 16 | + private int[][] maze; |
| 17 | + private boolean[][] visited; |
| 18 | + private Coordinate start; |
| 19 | + private Coordinate end; |
| 20 | + |
| 21 | + public Maze(File maze) throws FileNotFoundException { |
| 22 | + String fileText = ""; |
| 23 | + try (Scanner input = new Scanner(maze)) { |
| 24 | + while (input.hasNextLine()) { |
| 25 | + fileText += input.nextLine() + "\n"; |
| 26 | + } |
| 27 | + } |
| 28 | + initializeMaze(fileText); |
| 29 | + } |
| 30 | + |
| 31 | + private void initializeMaze(String text) { |
| 32 | + if (text == null || (text = text.trim()).length() == 0) { |
| 33 | + throw new IllegalArgumentException("empty lines data"); |
| 34 | + } |
| 35 | + |
| 36 | + String[] lines = text.split("[\r]?\n"); |
| 37 | + maze = new int[lines.length][lines[0].length()]; |
| 38 | + visited = new boolean[lines.length][lines[0].length()]; |
| 39 | + |
| 40 | + for (int row = 0; row < getHeight(); row++) { |
| 41 | + if (lines[row].length() != getWidth()) { |
| 42 | + throw new IllegalArgumentException("line " + (row + 1) + " wrong length (was " + lines[row].length() + " but should be " + getWidth() + ")"); |
| 43 | + } |
| 44 | + |
| 45 | + for (int col = 0; col < getWidth(); col++) { |
| 46 | + if (lines[row].charAt(col) == '#') |
| 47 | + maze[row][col] = WALL; |
| 48 | + else if (lines[row].charAt(col) == 'S') { |
| 49 | + maze[row][col] = START; |
| 50 | + start = new Coordinate(row, col); |
| 51 | + } else if (lines[row].charAt(col) == 'E') { |
| 52 | + maze[row][col] = EXIT; |
| 53 | + end = new Coordinate(row, col); |
| 54 | + } else |
| 55 | + maze[row][col] = ROAD; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public int getHeight() { |
| 61 | + return maze.length; |
| 62 | + } |
| 63 | + |
| 64 | + public int getWidth() { |
| 65 | + return maze[0].length; |
| 66 | + } |
| 67 | + |
| 68 | + public Coordinate getEntry() { |
| 69 | + return start; |
| 70 | + } |
| 71 | + |
| 72 | + public Coordinate getExit() { |
| 73 | + return end; |
| 74 | + } |
| 75 | + |
| 76 | + public boolean isExit(int x, int y) { |
| 77 | + return x == end.getX() && y == end.getY(); |
| 78 | + } |
| 79 | + |
| 80 | + public boolean isStart(int x, int y) { |
| 81 | + return x == start.getX() && y == start.getY(); |
| 82 | + } |
| 83 | + |
| 84 | + public boolean isExplored(int row, int col) { |
| 85 | + return visited[row][col]; |
| 86 | + } |
| 87 | + |
| 88 | + public boolean isWall(int row, int col) { |
| 89 | + return maze[row][col] == WALL; |
| 90 | + } |
| 91 | + |
| 92 | + public void setVisited(int row, int col, boolean value) { |
| 93 | + visited[row][col] = value; |
| 94 | + } |
| 95 | + |
| 96 | + public boolean isValidLocation(int row, int col) { |
| 97 | + if (row < 0 || row >= getHeight() || col < 0 || col >= getWidth()) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + public void printPath(List<Coordinate> path) { |
| 104 | + int[][] tempMaze = Arrays.stream(maze) |
| 105 | + .map(int[]::clone) |
| 106 | + .toArray(int[][]::new); |
| 107 | + for (Coordinate coordinate : path) { |
| 108 | + if (isStart(coordinate.getX(), coordinate.getY()) || isExit(coordinate.getX(), coordinate.getY())) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + tempMaze[coordinate.getX()][coordinate.getY()] = PATH; |
| 112 | + } |
| 113 | + System.out.println(toString(tempMaze)); |
| 114 | + } |
| 115 | + |
| 116 | + public String toString(int[][] maze) { |
| 117 | + StringBuilder result = new StringBuilder(getWidth() * (getHeight() + 1)); |
| 118 | + for (int row = 0; row < getHeight(); row++) { |
| 119 | + for (int col = 0; col < getWidth(); col++) { |
| 120 | + if (maze[row][col] == ROAD) { |
| 121 | + result.append(' '); |
| 122 | + } else if (maze[row][col] == WALL) { |
| 123 | + result.append('#'); |
| 124 | + } else if (maze[row][col] == START) { |
| 125 | + result.append('S'); |
| 126 | + } else if (maze[row][col] == EXIT) { |
| 127 | + result.append('E'); |
| 128 | + } else { |
| 129 | + result.append('.'); |
| 130 | + } |
| 131 | + } |
| 132 | + result.append('\n'); |
| 133 | + } |
| 134 | + return result.toString(); |
| 135 | + } |
| 136 | + |
| 137 | + public void reset() { |
| 138 | + for (int i = 0; i < visited.length; i++) |
| 139 | + Arrays.fill(visited[i], false); |
| 140 | + } |
| 141 | +} |
0 commit comments