Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Rishabh Sud
- * Maze Solver
- */
- import java.io.*;
- import java.util.*;
- public class MazeSolver {
- public ArrayList <String> maize = new ArrayList<>();
- public Stack <Position> SValue = new Stack<>();
- public ArrayList <Position> olmaize = new ArrayList();
- /**
- * Uses a constructor to start the process of creating a maze
- */
- public MazeSolver (){
- Mazebuilder();
- }
- public void Mazebuilder() {
- try {
- Scanner file = new Scanner ( new BufferedReader( new FileReader("maze.txt")));
- while (file.hasNextLine()) {
- maize.add(file.nextLine());
- }
- file.close();
- }
- catch(Exception e){
- System.out.print(e.getMessage());
- e.printStackTrace();
- }
- for( int x = 0; x < maize.size(); x++) {
- for (int y; y < maize.get(x).length(); y++) {
- if( maize.get(x).substring(y, y+1).equals("@")) {
- SValue.push(new Position(x,y)); //Finds the Start of the Maze and pushes the location onto the stack
- System.out.println("Your Starting position is" + x + y);
- Algo(x,y);
- }
- }
- }
- }
- public static void main (String [] args) {
- new MazeSolver();
- }
- //Major Algorithim for the project
- public void Algo(int x, int y) {
- if ((maize.get(SValue.peek().getXCoordinate()).substring(x,y)).equals("$")) {
- done(x,y);
- }
- Up(x,y);
- Down(x,y);
- Left(x,y);
- Right(x,y);
- }
- /*
- * Method used to check whether you can go UP
- */
- public void Up(int up1, int up2) {
- if(up2 != 0){ //Checks for Wall
- if (maize.get(SValue.peek().getXCoordinate()).substring(up1,up2+1).equals(".")
- || ((SValue.peek().getXCoordinate()).substring(up1,up2+1).equals("$"))) {
- if (check(up1,up2+1) != false) {
- Algo(up1,up2+1);
- }
- }
- }
- else {
- Down(up1, up2);
- }
- }
- /*
- * Method used to check whether you can go UP
- */
- public void Down(int down1, int down2) {
- if(down2 < maize.size()){ //Checks for bottom of Maze
- if (maize.get(SValue.peek().getXCoordinate()).substring(down1,down2-1).equals(".")
- || (maize.get(SValue.peek().getXCoordinate()).substring(down1,down2-1).equals("$"))) {
- if ( check(down1,down2-1) != false) {
- Algo(down1,down2-1);
- }
- }
- }
- else {
- Left(down1, down2);
- }
- }
- /*
- * Method used to check whether you can go UP
- */
- public void Left(int left1, int left2) {
- if (maize.get(SValue.peek().getXCoordinate()).substring(left1-1,left2).equals(".")
- || ((maize.get(SValue.peek().getXCoordinate()).substring(left1-1,left2).equals("$")))) {
- if ( check(left1-1,left2) != false) {
- Algo(left1-1,left2);
- }
- }
- else if(check(left1-1,left2) != false) {
- Right(left1, left2);
- }
- }
- /*
- * Method used to check whether you can go Right
- */
- public void Right(int right1, int right2) {
- if ((maize.get(SValue.peek().getXCoordinate()).substring(right1+1,right2).equals(".")
- || (maize.get(SValue.peek().getXCoordinate()).substring(right1+1,right2).equals("$")))) {
- if(check(right1+1, right2) != false){
- Algo(right1+1,right2);
- }
- }
- else {
- SValue.pop(new Position(right1,right2));
- error(right1,right2);
- }
- }
- //Error if no points are available to move
- public void error(int e1, int e2) {
- if (SValue.peek() == null) {
- System.out.println("HOUSTON WE HAVE AN ERROR!, Maze Cannote be solved given the present Algorithims and without going backwards");
- }
- }
- //Finishes the program
- public void done(int done1, int done2) {
- System.out.println("We done boyz");
- return;
- }
- // Used to check whether program has been there, or "backtracking"
- //Puts the Coordinates into the stack
- public boolean check(int check1, int check2) {
- boolean hasbeen = false;
- for( int a = 0; a < olmaize.size(); a++) {
- if(olmaize.get(a).equals(new Position (check1,check2))){
- hasbeen = true;
- break;
- }
- if(!hasbeen) {
- SValue.push(new Position(check1,check2));
- System.out.println("Cordinates" + check1 + "," + check2);
- return true;
- }
- }
- return false;
- }
- public class Position{
- public int downL;
- public int upL;
- public Position (int x, int y) {
- downL = x;
- upL = y;
- }
- public int getXCoordinate(){
- return downL;
- }
- public boolean equals(Object obj){
- if(obj == this){
- return true;
- }
- if(obj instanceof Position){
- Position location = (Position) obj;
- return (downL == Position.getXCoordinate());
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement