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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 1]
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
- public class Main {
- public static void main(String[] args)
- {
- Scanner reader = new Scanner(System.in);
- System.out.print("Enter maze file location: ");
- //String fileLocation = reader.nextLine();
- reader.close();
- ArrayList<String> mazeRaw = new ArrayList<String>();
- File file = new File("C:\Users\Towarzysz\Desktop\Gentrack Maze Technical Test\Samples\sparse_medium.txt");
- try
- {
- String st;
- BufferedReader br = new BufferedReader(new FileReader(file));
- while ((st = br.readLine()) != null)
- mazeRaw.add(st);
- br.close();
- }
- catch (IOException e)
- {
- System.out.println("Error: " + e);
- }
- Maze testMaze = createMaze(mazeRaw);
- int startY = testMaze.returnStartingPosition()[0];
- int startX = testMaze.returnStartingPosition()[1];
- System.out.println(testMaze.returnStartingPosition());
- Node p = solveMaze(testMaze, startY, startX);
- System.out.print(testMaze.maze2D[0].length);
- String[][] results = new String[testMaze.maze2D.length][testMaze.maze2D[0].length];
- for(int i = 0; i < testMaze.maze2D.length; i++){
- for(int j = 0; j < testMaze.maze2D[i].length; j++){
- results[i][j] = "";
- }
- }
- for (int i = 0; i < testMaze.getMaze2D().length; i++) {
- for (int j = 0; j < testMaze.getMaze2D()[i].length; j++) {
- switch(testMaze.maze2D[i][j])
- {
- case 0 :
- results[i][j] = " ";
- break;
- case 1 :
- results[i][j] = "#";
- break;
- case -1 :
- results[i][j] = " ";
- break;
- case 9 :
- results[i][j] = "E";
- break;
- }
- }
- }
- try
- {
- while(p.getParent() != null) {
- p = p.getParent();
- if(testMaze.maze2D[p.x][p.y] == 9)
- continue;
- results[p.x][p.y] = "X";
- System.out.println("x" + p.x + " " + "y" + p.y);
- }
- results[startY][startX] = "S";
- }
- catch(java.lang.NullPointerException e)
- {
- System.out.println("Maze not solvable");
- }
- for (int i = 0; i < results.length; i++) {
- for (int j = 0; j < results[i].length; j++) {
- System.out.print(results[i][j]);
- }
- System.out.println();
- }
- }
- static Maze createMaze(ArrayList<String> n)
- {
- Maze testMaze = new Maze(n);
- System.out.print(testMaze.toString());
- testMaze.drawMaze();
- return testMaze;
- }
- //public static Queue<Node> q = new LinkedList<Node>();
- static Stack q = new Stack();
- public static Node solveMaze(Maze maze, int x, int y)
- {
- q.push(new Node(x,y, null));
- while(!q.isEmpty()) {
- Node p = (Node) q.pop();
- if (maze.getMaze2D()[p.x][p.y] == 9) {
- System.out.print(p.x + " " + p.y);
- System.out.println("Exit is reached!");
- return p;
- }
- if(isBorder(maze, p.x, p.y, p) != null)
- {
- q.push(isBorder(maze, p.x, p.y, p));
- System.out.println("WALL " + p.toString());
- }
- if(isFree(maze, p.x+1,p.y)) {
- maze.getMaze2D()[p.x][p.y] = -1;
- Node nextP = new Node(p.x+1,p.y, p);
- q.push(nextP);
- }
- if(isFree(maze, p.x-1,p.y)) {
- maze.getMaze2D()[p.x][p.y] = -1;
- Node nextP = new Node(p.x-1,p.y, p);
- q.push(nextP);
- }
- if(isFree(maze, p.x,p.y+1)) {
- maze.getMaze2D()[p.x][p.y] = -1;
- Node nextP = new Node(p.x,p.y+1, p);
- q.push(nextP);
- }
- if(isFree(maze, p.x,p.y-1)) {
- maze.getMaze2D()[p.x][p.y] = -1;
- Node nextP = new Node(p.x,p.y-1, p);
- q.push(nextP);
- }
- }
- return null;
- }
- public static Node isBorder(Maze maze, int x, int y, Node parent)
- {
- Node nextNode;
- if(x == 0 && maze.getMaze2D()[x][y] != 1)
- {
- if(maze.getMaze2D()[maze.getMaze2D().length - 1][y] != 1)
- return nextNode = new Node(maze.getMaze2D().length - 1, y, parent);
- }
- if(x == maze.getMaze2D().length - 1 && maze.getMaze2D()[x][y] != 1)
- {
- if(maze.getMaze2D()[0][y] != 1)
- return nextNode = new Node(0, y, parent);
- }
- if(y == 0 && maze.getMaze2D()[x][y] != 1)
- {
- if(maze.getMaze2D()[x][maze.getMaze2D().length - 1] != 1)
- return nextNode = new Node(x, maze.getMaze2D().length - 1, parent);
- }
- if(y == maze.getMaze2D()[x].length - 1 && maze.getMaze2D()[x][y] != 1)
- {
- if(maze.getMaze2D()[x][0] != 1)
- return nextNode = new Node(x, 0, parent);
- }
- return null;
- }
- public static boolean isFree(Maze maze, int x, int y) {
- if((x >= 0 && x < maze.getMaze2D().length) && (y >= 0 && y < maze.getMaze2D()[x].length) && (maze.getMaze2D()[x][y] == 0 || maze.getMaze2D()[x][y] == 9)) {
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement