Advertisement
Guest User

Maze.java

a guest
Apr 7th, 2020
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.90 KB | None | 0 0
  1. import java.awt.*;
  2. import java.util.ArrayList;
  3. import java.util.Stack;
  4.  
  5. public class Maze {
  6.  
  7.     private Point startingPoint;
  8.     private Point destinationPoint;
  9.     private ArrayList<Point> walls;
  10.     private ArrayList<Point> path;
  11.  
  12.     private int maxX;
  13.     private int maxY;
  14.     private Node solution;
  15.  
  16.     public Maze(String maze) {
  17.         ArrayList<Point> walls = new ArrayList<>();
  18.         ArrayList<Point> path = new ArrayList<>();
  19.         int x = 0;
  20.         int y = 0;
  21.         for (int i = 0; i < maze.length(); i++)  {
  22.             if (maze.charAt(i) == '\n') {
  23.                 y++;
  24.                 x = 0;
  25.                 continue;
  26.             }
  27.             if (x > maxX) maxX = x;
  28.             if (y > maxY) maxY = y;
  29.             if (maze.charAt(i) == '#') {
  30.                 walls.add(new Point(x, y));
  31.             } else if (maze.charAt(i) == ' ') {
  32.                 path.add(new Point(x, y));
  33.             } else if (maze.charAt(i) == 'A') {
  34.                 startingPoint = new Point(x, y);
  35.             } else if (maze.charAt(i) == 'B') {
  36.                 destinationPoint = new Point(x, y);
  37.                 path.add(destinationPoint);
  38.             }
  39.             x++;
  40.         }
  41.         this.walls = walls;
  42.         this.path = path;
  43.     }
  44.  
  45.     private ArrayList<Node> getNeighbors(Node node) {
  46.         ArrayList<Node> result = new ArrayList<>();
  47.         Point top = new Point(node.getState().x, node.getState().y - 1);
  48.         Point bottom = new Point(node.getState().x, node.getState().y + 1);
  49.         Point left = new Point(node.getState().x - 1, node.getState().y);
  50.         Point right = new Point(node.getState().x + 1, node.getState().y);
  51.         if (path.contains(top)) result.add(new Node(top, node));
  52.         if (path.contains(bottom)) result.add(new Node(bottom, node));
  53.         if (path.contains(right)) result.add(new Node(right, node));
  54.         if (path.contains(left)) result.add(new Node(left, node));
  55.         return result;
  56.     }
  57.  
  58.     public Node solve() {
  59.         ArrayList<Point> visited = new ArrayList<>();
  60.  
  61.         // Create frontier that has only the initial state.
  62.         Stack<Node> frontier = new Stack<>(); // OR QUEUE.
  63.         frontier.add(new Node(startingPoint, null));
  64.  
  65.         // Repeat the following:
  66.         //      1. If frontier is empty, return no solution.
  67.         //      2. Remove an element, if it's out destination, we're done.
  68.         //      3. Add neighbours to the frontier
  69.         while (true) {
  70.             if (frontier.isEmpty()) return null;
  71.             Node temp = frontier.pop();
  72.             if (temp.getState().equals(destinationPoint)) return solution = temp;
  73.  
  74.             for (Node node : getNeighbors(temp)) {
  75.                if (!visited.contains(node.getState())) {
  76.                     visited.add(node.getState());
  77.                     frontier.add(node);
  78.                 }
  79.             }
  80.  
  81.         }
  82.  
  83.     }
  84.  
  85.     public void print() {
  86.         if (solution == null) return;
  87.         Node tempSolution = solution;
  88.         ArrayList<Point> correctPath = new ArrayList<>();
  89.         while (tempSolution.getParent() != null) {
  90.             correctPath.add(tempSolution.getState());
  91.             tempSolution = tempSolution.getParent();
  92.         }
  93.  
  94.         for (int i = 0; i <= maxY; i++) {
  95.             for (int j = 0; j <= maxX; j++) {
  96.                 Point temp = new Point(j, i);
  97.                 if (startingPoint.equals(temp)) {
  98.                     System.out.printf("A");
  99.                 } else if (destinationPoint.equals(temp)) {
  100.                     System.out.printf("B");
  101.                 } else if (walls.contains(temp)) {
  102.                     System.out.printf("%c", '#');
  103.                 } else if (correctPath.contains(temp)) {
  104.                     System.out.printf("+");
  105.                 } else {
  106.                     System.out.printf(" ");
  107.                 }
  108.             }
  109.             System.out.println();
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement