Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.96 KB | None | 0 0
  1. package assignment9;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.util.ArrayList;
  6. import java.util.NoSuchElementException;
  7. import java.util.Scanner;
  8.  
  9. import assignment6.MyLinkedList;
  10.  
  11. public class PathFinder {
  12.  
  13.     public static int row;
  14.     public static int column;
  15.     public static int sRow;
  16.     public static int sColumn;
  17.     public static int gRow;
  18.     public static int gColumn;
  19.     public static ArrayList<node> path = new ArrayList<node>();
  20.     public static MyQueue<node> queue = new MyQueue<node>();
  21.  
  22.     // subclass called node
  23.     public static class node {
  24.         boolean visited;
  25.         char data;
  26.         node left;
  27.         node right;
  28.         node up;
  29.         node down;
  30.         node from;
  31.         int nRow;
  32.         int nColumn;
  33.  
  34.         // node constructor
  35.         public node(char data, node left, node right, node up, node down,
  36.                 int nRow, int nColumn) {
  37.             this.data = data;
  38.             this.left = left;
  39.             this.right = right;
  40.             this.up = up;
  41.             this.down = down;
  42.             // marking the node as false when created
  43.             this.visited = false;
  44.             // takes down the node it came from in the bfs
  45.             this.from = null;
  46.             // node position
  47.             this.nRow = nRow;
  48.             this.nColumn = nColumn;
  49.         }
  50.     }
  51.  
  52.     // queue class based off linked list
  53.     public static class MyQueue<E> {
  54.  
  55.         private MyLinkedList<E> queue;
  56.  
  57.         public MyQueue() {
  58.             queue = new MyLinkedList<E>();
  59.         }
  60.  
  61.         /**
  62.          * Removes all of the elements from the stack.
  63.          */
  64.         public void clear() {
  65.             // FILL IN
  66.             queue.clear();
  67.         }
  68.  
  69.         /**
  70.          * Returns true if the stack contains no elements.
  71.          */
  72.         public boolean isEmpty() {
  73.             // FILL IN
  74.             return queue.isEmpty();
  75.         }
  76.  
  77.         /**
  78.          * Returns the item at the top of the stack without removing it from the
  79.          * stack. Throws NoSuchElementException if the stack is empty.
  80.          */
  81.         public E peek() throws NoSuchElementException {
  82.             // FILL IN
  83.             return (E) queue.getLast();
  84.         }
  85.  
  86.         /**
  87.          * Returns and removes the item at the bottom of the queue. Throws
  88.          * NoSuchElementException if the stack is empty.
  89.          */
  90.         public E pop() throws NoSuchElementException {
  91.             // FILL IN
  92.             return (E) queue.removeFirst();
  93.         }
  94.  
  95.         /**
  96.          * Pushes the input item onto the top of the stack.
  97.          */
  98.         public void push(E item) {
  99.             // FILL IN
  100.             queue.addLast(item);
  101.         }
  102.  
  103.         /**
  104.          * Returns the number of items in the stack.
  105.          */
  106.         public int size() {
  107.             return queue.size();
  108.         }
  109.     }
  110.  
  111.     public static void solveMaze(String inputFile) {
  112.         node[][] map;
  113.         ArrayList<node> path;
  114.         try {
  115.             map = translate(inputFile);
  116.             // connects adjacent nodes
  117.             pointNodes(map);
  118.             // makes a path from g to s and storing the path in an array list
  119.             path = reconstructPath(map, map[gRow][gColumn]);
  120.             // go back from the array list path
  121.             map = drawPath(map, path);
  122.             // print out path
  123.             for(int r = 0; r < row; r++) {
  124.                 for(int c = 0; c < column; c++) {
  125.                     System.out.print(map[r][c].data);
  126.                 }
  127.                 System.out.print('\n');
  128.             }
  129.         } catch (FileNotFoundException e) {
  130.             // TODO Auto-generated catch block
  131.             System.out.println("solveMaze error");
  132.             e.printStackTrace();
  133.         }
  134.     }
  135.  
  136.     /**
  137.      * translates the maze into an array of arrays of nodes called map
  138.      *
  139.      * @param inputFile
  140.      * @throws FileNotFoundException
  141.      */
  142.     public static node[][] translate(String inputFile)
  143.             throws FileNotFoundException {
  144.         String rowString = null;
  145.         String dimensions = null;
  146.         char ch;
  147.         Scanner s = new Scanner(new FileReader(inputFile));
  148.         dimensions = s.nextLine();
  149.         Scanner d = new Scanner(dimensions);
  150.         row = Integer.parseInt(d.next());
  151.         column = Integer.parseInt(d.next());
  152.         System.out.println(row + " " + column);
  153.         node[][] map = new node[row][column];
  154.         for (int r = 0; r < row; r++) {
  155.             if (s.hasNextLine()) {
  156.                 rowString = s.nextLine();
  157.                 System.out.println(rowString);
  158.             }
  159.             for (int c = 0; c < column; c++) {
  160.                 ch = rowString.charAt(c);
  161.                 if (ch == 'X') {
  162.                     // create node
  163.                     map[r][c] = new node(ch, null, null, null, null, r, c);
  164.                 }
  165.                 // check for 'S'
  166.                 else if (ch == 'S') {
  167.                     sRow = r;
  168.                     sColumn = c;
  169.                     // create node
  170.                     map[r][c] = new node(ch, null, null, null, null, r, c);
  171.                 } else if (ch == 'G') {
  172.                     gRow = r;
  173.                     gColumn = c;
  174.                     // create node
  175.                     map[r][c] = new node(ch, null, null, null, null, r, c);
  176.                 } else {
  177.                     // create node
  178.                     map[r][c] = new node(ch, null, null, null, null, r, c);
  179.                 }
  180.             }
  181.         }
  182.         s.close();
  183.         return map;
  184.     }
  185.  
  186.     /**
  187.      * points all the nodes in the array of arrays to their correct neighbors
  188.      *
  189.      * @param map
  190.      */
  191.     public static void pointNodes(node[][] map) {
  192.         for (int r = 0; r < row; r++) {
  193.             for (int c = 0; c < column; c++) {
  194.  
  195.                 // top row
  196.                 // first row first column
  197.                 if (r == 0 && c == 0) {
  198.                     map[r][c].right = map[r][c + 1];
  199.                     map[r][c].down = map[r + 1][c];
  200.                 }
  201.                 // first row last column
  202.                 else if (r == 0 && c == column - 1) {
  203.                     map[r][c].left = map[r][c - 1];
  204.                     map[r][c].down = map[r + 1][c];
  205.                 }
  206.                 // first row in between
  207.                 else if (r == 0 && c > 0 && c < column - 1) {
  208.                     map[r][c].left = map[r][c - 1];
  209.                     map[r][c].right = map[r][c + 1];
  210.                     map[r][c].down = map[r + 1][c];
  211.                 }
  212.  
  213.                 // bottom row
  214.                 // last row first column
  215.                 else if (r == row - 1 && c == 0) {
  216.                     map[r][c].right = map[r][c + 1];
  217.                     map[r][c].up = map[r - 1][c];
  218.                 }
  219.                 // last row last column
  220.                 else if (r == row - 1 && c == column - 1) {
  221.                     map[r][c].left = map[r][c - 1];
  222.                     map[r][c].up = map[r - 1][c];
  223.                 }
  224.                 // last row in between
  225.                 else if (r == row - 1 && c > 0 && c < column - 1) {
  226.                     map[r][c].left = map[r][c - 1];
  227.                     map[r][c].right = map[r][c + 1];
  228.                     map[r][c].up = map[r - 1][c];
  229.                 }
  230.  
  231.                 // first column in between
  232.                 else if (r > 0 && r < row - 1 && c == 0) {
  233.                     map[r][c].right = map[r][c + 1];
  234.                     map[r][c].up = map[r - 1][c];
  235.                     map[r][c].down = map[r + 1][c];
  236.                 }
  237.  
  238.                 // last column in between
  239.                 else if (r > 0 && r < row - 1 && c == column - 1) {
  240.                     map[r][c].left = map[r][c - 1];
  241.                     map[r][c].up = map[r - 1][c];
  242.                     map[r][c].down = map[r + 1][c];
  243.                 }
  244.  
  245.                 // everything else
  246.                 else {
  247.                     map[r][c].left = map[r][c - 1];
  248.                     map[r][c].right = map[r][c + 1];
  249.                     map[r][c].up = map[r - 1][c];
  250.                     map[r][c].down = map[r + 1][c];
  251.                 }
  252.             }
  253.         }
  254.     }
  255.  
  256.     /**
  257.      * returns true if you can get from 'S' to 'G', false otherwise
  258.      *
  259.      * @param map
  260.      * @return
  261.      */
  262.     public boolean bfs(node[][] map, node n) {
  263.         if (n.data == 'G') {
  264.             return true;
  265.         } else {
  266.             if (n.left.data != 'X' && n.left.visited == false) {
  267.                 queue.push(map[sRow][sColumn].left);
  268.                 n.left.visited = true;
  269.                 n.left.from = n;
  270.             }
  271.             if (n.right.data != 'X' && n.right.visited == false) {
  272.                 queue.push(map[sRow][sColumn].right);
  273.                 n.right.visited = true;
  274.                 n.right.from = n;
  275.             }
  276.             if (n.up.data != 'X' && n.up.visited == false) {
  277.                 queue.push(map[sRow][sColumn].up);
  278.                 n.up.visited = true;
  279.                 n.up.from = n;
  280.             }
  281.             if (n.down.data != 'X' && n.down.visited == false) {
  282.                 queue.push(map[sRow][sColumn].down);
  283.                 n.down.visited = true;
  284.                 n.down.from = n;
  285.             }
  286.             if (queue.isEmpty()) {
  287.                 return false;
  288.             } else {
  289.                 return bfs(map, queue.pop());
  290.             }
  291.         }
  292.     }
  293.  
  294.     /**
  295.      * reconstructs the path back from 'G' to the node containing 'S'
  296.      *
  297.      * @param map
  298.      * @param n
  299.      * @return
  300.      */
  301.     public static ArrayList<node> reconstructPath(node[][] map, node n) {
  302.         if (n.from.data == 'S') {
  303.             path.add(n);
  304.             path.add(n.from);
  305.             return path;
  306.         } else {
  307.             path.add(n.from);
  308.             return reconstructPath(map, n.from);
  309.         }
  310.     }
  311.  
  312.     /**
  313.      * puts a '.' instead of ' ' through the path
  314.      *
  315.      * @param map
  316.      * @param path
  317.      * @return
  318.      */
  319.     public static node[][] drawPath(node[][] map, ArrayList<node> path) {
  320.         for (int i = 0; i < path.size(); i++) {
  321.             if (path.get(i).data == ' ') {
  322.                 map[path.get(i).nRow][path.get(i).nColumn].data = '.';
  323.             }
  324.         }
  325.         return map;
  326.     }
  327.  
  328.     public static void main(String[] args) {
  329.         solveMaze("tinyMaze.txt");
  330.     }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement