Advertisement
Ed94

Prog 2 Assignment 2 Maze

Jul 11th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.98 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Iterator ;
  3. import java.util.ListIterator;
  4. import java.util.Stack    ;
  5.  
  6.  
  7. public class Maze
  8. {
  9.     //Public
  10.     public Maze(char[][] mazeMap)
  11.     { this.mazeMap = mazeMap; }
  12.      
  13.     public boolean escape(int x, int y)
  14.     {  
  15.         reset();
  16.        
  17.         ducktape = new Tuple<Integer, Integer>(x, y);
  18.        
  19.         if (recursion(x, y))
  20.         {
  21.             path.push(pos);
  22.            
  23.             return true;
  24.         }
  25.         else
  26.             return false;
  27.     }
  28.    
  29.    
  30.     //Private
  31.     boolean recursion(int x, int y)
  32.     {
  33.         try
  34.         {
  35.             if (!reachedBounds)
  36.             {
  37.                 getNextPos(x, y);
  38.                
  39.                 if  (checkIfBoundary()) { return true   ; }
  40.                 else                    { checkOptions(); }
  41.                
  42.                 path.push(pos);
  43.                
  44.                 if (history.size() < ((mazeMap.length-1)*(mazeMap[0].length-1)))
  45.                     history.push(path.peek());
  46.                
  47.                 if (optSize > 0 && !repeat) { recursion(pos.x, pos.y); }
  48.                 else                        { return false           ; }
  49.             }
  50.            
  51.             return reachedBounds;
  52.         }
  53.         catch (StackOverflowError t)
  54.         {
  55.             return false;
  56.         }
  57.     }
  58.    
  59.     boolean checkIfBoundary()
  60.     {
  61.         if  (pos.x == mazeMap.length || pos.y == mazeMap.length || pos.x == 0 || pos.y == 0)
  62.             return reachedBounds = true;
  63.         else
  64.             return false;
  65.     }
  66.    
  67.     boolean checkOptionLevel()
  68.     {
  69.         try
  70.         {
  71.             if (options.get(stackLvl).size() <= 0)
  72.                 return false;
  73.             else
  74.                 return true;
  75.         }
  76.         catch (StackOverflowError t)
  77.         {
  78.             System.out.println("StackOverflow for: "+ t);
  79.            
  80.             System.out.println("Recursion has most likely overflowed due to checks to prevent repeat coords not working.");
  81.            
  82.             return false;
  83.         }
  84.     }
  85.    
  86.     boolean checkIfTravelled(int x, int y)
  87.     {
  88.         for (ListIterator<Tuple<Integer, Integer>> pathList = history.listIterator();  pathList.hasNext();)
  89.         {
  90.             Tuple<Integer, Integer> crnt = pathList.next();
  91.            
  92.             if (x == crnt.x && y == crnt.y)
  93.                 return true;
  94.         }
  95.         return false;
  96.     }
  97.    
  98.     class Tuple<X, Y>   //Stack Overflow
  99.     {
  100.         public final X x;
  101.         public final Y y;
  102.        
  103.         public Tuple(X x, Y y)
  104.         {
  105.             this.x = x;
  106.             this.y = y;
  107.         }
  108.        
  109.         public Tuple(Tuple<X, Y> passedTuple)
  110.         {
  111.             this.x = passedTuple.x;
  112.             this.y = passedTuple.y;
  113.         }
  114.        
  115.         @Override
  116.         public String toString()
  117.         { return new String(x+ " "+ y); }
  118.     }
  119.    
  120.     void checkOptions()
  121.     {
  122.         expandLevels();
  123.        
  124.         for (int y = -1; y <= 1; y++)
  125.         {  
  126.             for (int x = -1; x <= 1; x++)
  127.             {
  128.                 int index_X = x + pos.x, index_Y = y + pos.y;
  129.                
  130.                 if (index_X < mazeMap.length && index_Y < mazeMap[0].length)
  131.                 {
  132.                     if      (x ==  0 && y ==  0) {}
  133.                     else if (x == -1 && y == -1) {}
  134.                     else if (x ==  1 && y ==  1) {}
  135.                     else if (x == -1 && y ==  1) {}
  136.                     else if (x ==  1 && y == -1) {}
  137.                     else if (mazeMap[index_X][index_Y] == ' ')
  138.                     {
  139.                         Tuple<Integer, Integer> possible = new Tuple<Integer, Integer>((x + pos.x), (y + pos.y));
  140.                        
  141.                         boolean travelled = false;
  142.                         boolean inOptions = false;
  143.                        
  144.                         for (int tempLvlIndex = 0; tempLvlIndex < optSize; tempLvlIndex++)
  145.                         {
  146.                             for (int stackIndex = 0; stackIndex < options.get(tempLvlIndex).size(); stackIndex++)
  147.                             {
  148.                                 if (possible.x == options.get(tempLvlIndex).get(stackIndex).x && possible.y == options.get(tempLvlIndex).get(stackIndex).y)
  149.                                     inOptions = true;
  150.                                
  151.                                 try
  152.                                 {
  153.                                     for (ListIterator<Tuple<Integer, Integer>> pathList = history.listIterator();  pathList.hasNext();)
  154.                                     {
  155.                                         Tuple<Integer, Integer> crnt = pathList.next();
  156.                                        
  157.                                         if (possible.x == crnt.x && possible.y == crnt.y)
  158.                                             travelled = true;
  159.                                     }
  160.                                 }
  161.                                 catch (StackOverflowError t)
  162.                                 {
  163.                                     System.out.println("StackOverflow for: "+ t);
  164.                                    
  165.                                     System.out.println("History stack was mostly likely at max size and somehow got passed all these checks...");
  166.                                 }
  167.                                
  168.                             }
  169.                         }
  170.                         if (travelled == false && inOptions == false)
  171.                         {
  172.                             options.get(stackLvl).push(possible);
  173.                         }
  174.                     }
  175.                 }
  176.             }
  177.         }
  178.     }
  179.  
  180.     void getNextPos(int x, int y)
  181.     {
  182.         try
  183.         {
  184.             if (options.isEmpty() && !checkIfTravelled(x, y))
  185.                 pos = new Tuple<Integer, Integer>(x, y);
  186.             else
  187.             {
  188.                 if (checkOptionLevel())
  189.                 {
  190.                     pos = new Tuple<Integer, Integer>(options.get(stackLvl).pop());
  191.                    
  192.                     stackLvl++;
  193.                    
  194.                     if (pos.x == ducktape.x && pos.y == ducktape.y)
  195.                     {
  196.                         repeat = true; //My check options somehow leaks the original position if it does not find an exit =/.
  197.                     }
  198.                 }
  199.                 else
  200.                 {
  201.                     if (stackLvl > 0)
  202.                     {
  203.                         options.remove(stackLvl);
  204.                        
  205.                         optSize--; stackLvl--;
  206.                        
  207.                         path.pop();
  208.                        
  209.                         getNextPos(x, y);
  210.                     }
  211.                 }
  212.             }
  213.         }
  214.         catch (StackOverflowError t)
  215.         {
  216.             System.out.println("StackOverflow for: "+ t);
  217.            
  218.             System.out.println("Options stack was most likely empty and somehow got passed all these checks...");
  219.         }
  220.     }
  221.    
  222.     void expandLevels()
  223.     {
  224.         if (optSize <= stackLvl)
  225.         {
  226.             options.add(new Stack<Tuple<Integer, Integer>>());
  227.            
  228.             optSize++;
  229.         }
  230.     }
  231.    
  232.     public void printPath()
  233.     {
  234.         System.out.println("Path:");
  235.        
  236.         for (ListIterator<Tuple<Integer, Integer>> pathList = path.listIterator();  pathList.hasNext();)
  237.         {
  238.             Tuple<Integer, Integer> crnt = pathList.next();
  239.            
  240.             System.out.print("("+ crnt.x+ ", "+ crnt.y+ ") ");
  241.         }
  242.        
  243.         System.out.println();
  244.     }
  245.    
  246.     void reset()
  247.     {
  248.         stackLvl = 0; optSize = 0; reachedBounds = false;
  249.        
  250.         options = new ArrayList<Stack<Tuple<Integer, Integer>>>();
  251.        
  252.         path    = new Stack<Tuple<Integer, Integer>>();
  253.         history = new Stack<Tuple<Integer, Integer>>();
  254.     }
  255.    
  256.     //Instance
  257.     boolean reachedBounds = false; boolean repeat = false;
  258.    
  259.     char[][] mazeMap;
  260.    
  261.     int stackLvl = 0, optSize = 0;
  262.    
  263.     ArrayList<Stack<Tuple<Integer, Integer>>> options = new ArrayList<Stack<Tuple<Integer, Integer>>>();
  264.    
  265.     Stack<Tuple<Integer, Integer>> history = new Stack<Tuple<Integer, Integer>>();
  266.     Stack<Tuple<Integer, Integer>> path    = new Stack<Tuple<Integer, Integer>>();
  267.    
  268.     Tuple<Integer, Integer> pos     ;
  269.     Tuple<Integer, Integer> ducktape;
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement