Advertisement
The_Realist

MazeSolverTooStronkForYou

Mar 24th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.59 KB | None | 0 0
  1. //Convert me! (base64)
  2. //aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1tTWZ4STNyX0x5QQ==
  3. import java.io.*;
  4. import java.util.*;
  5. public class BootlegAmalgamation {
  6.     public Stack <moveController> stacksOnStacks = new Stack<>();
  7.     public ArrayList <String> newMaze = new ArrayList<>();
  8.     public ArrayList <moveController> ogMaze = new ArrayList();
  9.     int wingle1;
  10.     int wingle2;
  11.     public BootlegAmalgamation() {
  12.         try {
  13.             Scanner scanSki = new Scanner ( new BufferedReader( new FileReader("maze.txt")));
  14.             while (scanSki.hasNextLine()) {
  15.                 newMaze.add(scanSki.nextLine());
  16.             }
  17.             scanSki.close();
  18.         }
  19.         catch(Exception e){
  20.             System.out.print(e.getMessage());
  21.             e.printStackTrace();
  22.         }
  23.         for( int x = 0; x < newMaze.size(); x++) {
  24.             for (int y = 0; y < newMaze.get(x).length(); y++) {
  25.                 if( newMaze.get(x).substring(y, y+1).equals("@")) {
  26.                     stacksOnStacks.push(new moveController(x,y)); //Finds the Start of the Maze and pushes the location onto the stack
  27.                     System.out.println("Sup Bih, heres where you start" + " " + x + " " + "," + " " + y);
  28.  
  29.                 }
  30.             }
  31.         }
  32.         if(stacksOnStacks.peek() == null){
  33.             System.out.println("Kramer wtf, why does this maze not have a start point");
  34.         }
  35.         else {
  36.             try {
  37.                 while(newMaze.get(stacksOnStacks.peek().getYCoordinate()).substring(stacksOnStacks.peek().getXCoordinate(), stacksOnStacks.peek().getXCoordinate() + 1) != "$") {
  38.                     goGoGadgetSolver();
  39.                 }
  40.             } catch (Exception e) {
  41.                 System.out.print(e.getMessage());
  42.                 e.printStackTrace();
  43.             }
  44.         }
  45.  
  46.     }  
  47.  
  48.     public static void main (String [] args) {
  49.         new BootlegAmalgamation();
  50.     }
  51.  
  52.     private boolean uberSexyBacktrack(int newX, int newY){ //not sure how to use this
  53.         boolean dejaVu = false;
  54.         for(int wingle = 0; wingle < ogMaze.size(); wingle++){
  55.             if(ogMaze.get(wingle).equals(new moveController(newX, newY))) {
  56.                 dejaVu = true;
  57.             }
  58.         }    
  59.         if(!dejaVu){
  60.             ogMaze.add(new moveController(wingle1, wingle2)); //artifact leftover from your stuff, check and modify pls
  61.             stacksOnStacks.push(new moveController(newX,newY));
  62.             return true;
  63.         }
  64.        
  65.             return false;
  66.     }    
  67.  
  68.     /**
  69.      * this ting goes through the 4 checks and also functions as the printer too.
  70.      */
  71.     public void goGoGadgetSolver(){
  72.         int a;
  73.         int b;
  74.         a = stacksOnStacks.peek().getXCoordinate();
  75.         b = stacksOnStacks.peek().getYCoordinate();
  76.         stacksOnStacks.push(new moveController(a,b));
  77.         System.out.print(a + " , " + b);
  78.         up(a,b);
  79.         down(a,b);
  80.         left(a,b);
  81.         right(a,b);
  82.  
  83.     }
  84.  
  85.     /**
  86.      * checks for a possible path that can go upwards
  87.      *
  88.      * @param  y   a sample parameter for a method
  89.      * @return     the sum of x and y
  90.      */
  91.     public boolean up(int x1, int y1)
  92.     {
  93.         boolean ye1 = false;
  94.         if(y1 != 0){ //Checks if we can actually go up or not
  95.  
  96.             if (newMaze.get(stacksOnStacks.peek().getXCoordinate()).substring(y1+1,y1+2).equals(".") || newMaze.get(stacksOnStacks.peek().getXCoordinate()).substring(y1+1,y1+2).equals("$")) { //if it finds either a valid solution or the end
  97.  
  98.                 if(uberSexyBacktrack(x1,y1+1)) {
  99.                     return true;
  100.                 } else {
  101.                     return false;
  102.                 }
  103.             }
  104.         }
  105.         return ye1;
  106.     }
  107.  
  108.     /**
  109.      * Checks for a possible path that can go upwards
  110.      *      
  111.      * @param  y   a sample parameter for a method
  112.      * @return     the sum of x and y
  113.      */
  114.     public boolean down(int x2, int y2)
  115.     {  
  116.         boolean ye2 = false;
  117.         if(y2 < newMaze.size()){ //Checks if we can actually go down or not, idk what to put here, it used to be the xaxis but that shit aint here
  118.  
  119.             if (newMaze.get(stacksOnStacks.peek().getXCoordinate()).substring(y2-1,y2).equals(".") || newMaze.get(stacksOnStacks.peek().getXCoordinate()).substring(y2-1,y2).equals("$")){
  120.  
  121.                 if(uberSexyBacktrack(x2, y2-1)) {
  122.                     ye2 = true;
  123.                 } else {
  124.                     ye2 = false;
  125.                 }
  126.             }
  127.         }
  128.         return ye2;
  129.     }
  130.  
  131.     /**
  132.      * checks for a possible path that can go leftwards  
  133.      *
  134.      * @param  y   a sample parameter for a method
  135.      * @return     the sum of x and y
  136.      */
  137.     public boolean left(int x3, int y3)
  138.     {
  139.         boolean ye3 = false;
  140.         if (newMaze.get(stacksOnStacks.peek().getXCoordinate()).substring(x3-1,x3).equals(".") || newMaze.get(stacksOnStacks.peek().getXCoordinate()).substring(x3-1,x3).equals("$")) {
  141.  
  142.             if(uberSexyBacktrack(x3-1, y3)) {
  143.                 ye3 = true;
  144.  
  145.             } else {
  146.                 ye3 = false;
  147.             }
  148.         }
  149.         return ye3;
  150.     }
  151.  
  152.     /**
  153.      * checks for a possible path that can go rightwards
  154.      *
  155.      * @param  y   a sample parameter for a method
  156.      * @return     the sum of x and y
  157.      */
  158.     public boolean right(int x4, int y4)
  159.     {
  160.         boolean ye4 = false;
  161.         if (newMaze.get(stacksOnStacks.peek().getXCoordinate()).substring(x4+1, x4+2).equals(".") || newMaze.get(stacksOnStacks.peek().getXCoordinate()).substring(x4+1, x4+2).equals("$")) {
  162.  
  163.             if(uberSexyBacktrack(x4+1, y4)) {
  164.                 ye4 = true;
  165.             } else {
  166.                 ye4 = false;
  167.             }
  168.         }
  169.         return ye4;
  170.     }
  171.  
  172.     public class moveController{
  173.         public int xLocation;
  174.         public int yLocation;
  175.         public  moveController(int xPosition, int yPosition) {
  176.             xLocation = xPosition;
  177.             yLocation = yPosition;
  178.         }
  179.  
  180.         public int getYCoordinate(){
  181.             return yLocation;
  182.         }
  183.  
  184.         public int getXCoordinate(){
  185.             return xLocation;
  186.         }
  187.         @Override
  188.         public boolean equals(Object obj){
  189.             if(obj == this){
  190.                 return true;
  191.             }
  192.             if(obj instanceof moveController){
  193.                 moveController location = (moveController) obj;
  194.                 return (xLocation == moveController.getXCoordinate()) && (yLocation == moveController.getYCoordinate());
  195.             }    
  196.             return false;
  197.         }    
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement