Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Solves a maze
- *
- * @Saman Verma
- * @3/17/18
- */
- import java.util.*;
- import java.io.*;
- public class MazeSolver
- { public Stack<String> MazePath = new Stack <String>();
- public Stack<String> oldPathStack = new Stack <String>();
- public String Location;
- public int i = 0;
- public ArrayList <String> oldPath = new ArrayList<String>();
- public ArrayList <String> maze = new ArrayList<String>();
- public void MazeReader(){
- try{Scanner MazeScanner = new Scanner(new BufferedReader(new FileReader("maze.txt")));
- while(MazeScanner.hasNextLine()){
- maze.add(MazeScanner.nextLine());
- }}catch(Exception e){
- System.out.print(e.getMessage());
- e.printStackTrace();}
- for(int y = 0; y<maze.size(); y++){
- int j = 0;
- if (j != 0) {i++;}
- j++;
- for(int x = 0; x<maze.get(y).length(); x++){
- String check = maze.get(y).substring(x,x+1);
- if(check.equals("@")){
- String yCoordinate = String.valueOf(y);
- String xCoordinate = String.valueOf(x);
- Location = xCoordinate + "," + yCoordinate;
- MazePath.push(Location);
- }
- }
- }
- }
- public static void main(String [] args){
- MazeSolver MazeSolver = new MazeSolver();
- MazeSolver.MazeReader();
- MazeSolver.solveTheMaze();
- MazeSolver.printer();
- }
- public void Left(int x, int y){
- x = Integer.parseInt(Location.substring(0,1));
- y = Integer.parseInt(Location.substring(2,3));
- x = x-1;
- if(x <0) {
- return;
- }
- boolean hasBeen = oldPlace(x, y);
- if(hasBeen == true){return;}
- String xCd = String.valueOf(x);
- String yCd = String.valueOf(y);
- if(maze.get(y).substring(x,x+1).equals(".") || maze.get(y).substring(x,x+1).equals("$")){
- Location = xCd + "," + yCd;
- MazePath.push(Location);
- }
- }
- public void Right(int x, int y){
- String Position = MazePath.peek();
- x = Integer.parseInt(Location.substring(0,1));
- y = Integer.parseInt(Location.substring(2,3));
- x = x+1;
- boolean hasBeen = oldPlace(x, y);
- if(hasBeen == true){return;}
- String xCd = String.valueOf(x);
- String yCd = String.valueOf(y);
- if(maze.get(y).substring(x,x+1) == null) {return;}
- else if(maze.get(y).substring(x,x+1) == "." || maze.get(y).substring(x,x+1) == "$"){
- Location = xCd + "," + yCd;
- MazePath.push(Location);
- }
- }
- public void Down(int x, int y){
- x = Integer.parseInt(Location.substring(0,1));
- y = Integer.parseInt(Location.substring(2,3));
- y = y-1;
- String xCd = String.valueOf(x);
- String yCd = String.valueOf(y);
- if(y <0) {
- return;
- }
- boolean hasBeen = oldPlace(x, y);
- if(hasBeen == true){return;}
- if(maze.get(y).substring(x,x+1).equals(".") || maze.get(y).substring(x,x+1).equals("$")){
- Location = xCd + "," + yCd;
- MazePath.push(Location);
- }
- }
- public void Up(int x, int y){
- x = Integer.parseInt(Location.substring(0,1));
- y = Integer.parseInt(Location.substring(2,3));
- y = y+1;
- String xCd = String.valueOf(x);
- String yCd = String.valueOf(y);
- boolean hasBeen = oldPlace(x, y);
- if(hasBeen == true){return;}
- else if(maze.get(y).substring(x,x+1).equals(".") || maze.get(y).substring(x,x+1).equals("$")){
- Location = xCd + "," + yCd;
- MazePath.push(Location);
- }
- }
- public String getCurrentPosition(){
- String currentPosition = MazePath.peek();
- return currentPosition;
- }
- public Stack solveTheMaze(){
- int x = Integer.parseInt(Location.substring(0,1));
- int y = Integer.parseInt(Location.substring(2,3));
- oldPathStack.push(Location);
- while(!(maze.get(y).substring(x,x+1).equals("$"))){
- boolean hasBeen = oldPlace(x, y);
- Left(x, y);
- oldPathStack.push(Location);
- x = Integer.parseInt(Location.substring(0,1));
- y = Integer.parseInt(Location.substring(2,3));
- Right(x, y);
- oldPathStack.push(Location);
- x = Integer.parseInt(Location.substring(0,1));
- y = Integer.parseInt(Location.substring(2,3));
- Down(x, y);
- oldPathStack.push(Location);
- x = Integer.parseInt(Location.substring(0,1));
- y = Integer.parseInt(Location.substring(2,3));
- Up(x, y);
- oldPathStack.push(Location);
- x = Integer.parseInt(Location.substring(0,1));
- y = Integer.parseInt(Location.substring(2,3));
- boolean r = RightChecker(x, y);
- boolean l = LeftChecker(x, y);
- boolean u = UpChecker(x, y);
- boolean d = DownChecker(x, y);
- while(r== false && l== false && u== false && d == false){
- String check = maze.get(y).substring(x,x+1);
- if(check.equals("@")) {
- break;
- }
- while (hasBeen == true)
- {MazePath.pop();
- Location = MazePath.peek();
- x = Integer.parseInt(Location.substring(0,1));
- y = Integer.parseInt(Location.substring(2,3));
- r = RightChecker(x, y);
- l = LeftChecker(x, y);
- u = UpChecker(x, y);
- d = DownChecker(x, y);
- hasBeen = oldPlace(x, y);
- }
- }
- }
- return MazePath;
- }
- public boolean RightChecker(int x, int y){
- x = Integer.parseInt(Location.substring(0,1));
- y = Integer.parseInt(Location.substring(2,3));
- int xCheck = x +1;
- int yCheck = y;
- String xCd = String.valueOf(xCheck);
- String yCd = String.valueOf(yCheck);
- if(maze.get(yCheck).substring(xCheck).equals(".") || maze.get(y).substring(x,x+1).equals("$")){
- return true;
- }else if(maze.get(yCheck).substring(xCheck, xCheck + 1) == null){
- return false;
- }
- else{return false;}
- }
- public boolean LeftChecker(int x, int y){
- x = Integer.parseInt(Location.substring(0,1));
- y = Integer.parseInt(Location.substring(2,3));
- x = x -1;
- y = y;
- String xCd = String.valueOf(x);
- String yCd = String.valueOf(y);
- if(maze.get(y).substring(x, x +1).equals(".") || maze.get(y).substring(x, x+1).equals("$")){
- return true;
- }else if(maze.get(y).substring(x, x+1) == null){
- return false;
- }
- else{return false;}
- }
- public boolean UpChecker(int x, int y){
- x = Integer.parseInt(Location.substring(0,1));
- y = Integer.parseInt(Location.substring(2,3));
- int xCheck = x;
- int yCheck = y + 1;
- String xCd = String.valueOf(xCheck);
- String yCd = String.valueOf(yCheck);
- if(yCheck > i) {return false;}
- else if(maze.get(yCheck).substring(xCheck, xCheck +1).equals(".") || maze.get(yCheck).substring(xCheck,xCheck+1).equals("$")){
- return true;
- } else if(maze.get(yCheck).substring(xCheck, xCheck +1) == null){
- return false;
- }
- else{return false;}
- }
- public boolean DownChecker(int x, int y){
- x = Integer.parseInt(Location.substring(0,1));
- y = Integer.parseInt(Location.substring(2,3));
- int xCheck = x;
- int yCheck = y - 1;
- String xCd = String.valueOf(xCheck);
- String yCd = String.valueOf(yCheck);
- if(yCheck < 0) {return false;}
- else if(maze.get(yCheck).substring(xCheck, xCheck +1).equals(".") || maze.get(yCheck).substring(xCheck,xCheck+1).equals("$")){
- return true;
- } else if(maze.get(yCheck).substring(xCheck, xCheck +1) == null){
- return false;
- }
- else{return false;}
- }
- public void printer(){
- while(MazePath.peek() != null){
- System.out.println(MazePath.peek());
- }
- MazePath.pop();
- }
- public boolean oldPlace(int x, int y){
- boolean trueornot = false;
- for (y = 0; y< oldPath.size(); y++){
- if(oldPath.get(y).equals(MazePath.peek())){
- trueornot = true;
- }
- }
- if(trueornot == true){
- return true;
- }
- else{
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement