Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- 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);
- }
- }
- }
- }
- /**
- * Uses a constructor to start the process of creating a maze
- */
- public static void main ( String [] args ) {
- new Mazebuilder ();
- }
- public void Algo(int x, int y) {
- SValue.push(new Position(x,y));
- System.out.println("Cordinates" + 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
- SValue.push(new Position(up1,up2));
- }
- if (SValue.substring(up1,up2+1).equals(".")
- || SValue.substring(up1,up2+1).equals("$")) {
- Algo(up1,up2+1);
- }
- else {
- SValue.pop(Position(up1,up2));
- Down(up1, up2);
- }
- }
- /*
- * Method used to check whether you can go UP
- */
- public void Down(int down1, int down2) {
- if(down2 < XVal){ //Checks for bottom of Maze
- SValue.push(new Position(down1,down2));
- }
- if (SValue.substring(down1,down2-1).equals(".")
- || SValue.substring(down1,down2-1).equals("$")) {
- Algo(down1,down2-1);
- }
- else {
- SValue.pop(Position(down1,down2));
- Left(down1, down2);
- }
- }
- /*
- * Method used to check whether you can go UP
- */
- public void Left(int left1, int left2) {
- SValue.push(new Position(left1,left2));
- if (SValue.substring(left1-1,left2).equals(".")
- || SValue.substring(left1-1,left2).equals("$")) {
- Algo(left1-1,left2);
- }
- else {
- SValue.pop(Position(left1,left2));
- Right(left1, left2);
- }
- }
- /*
- * Method used to check whether you can go Right
- */
- public void Right(int right1, int right2) {
- SValue.push(new Position(right1,right2));
- if (SValue.substring(right1+1,right2).equals(".")
- || SValue.substring(right1+1,right2).equals("$")) {
- Algo(right1+1,right2);
- }
- else {
- SValue.pop(Position(right1,right2));
- error(right1, right2);
- }
- }
- public void error(int e1, int e2) {
- System.out.println("HOUSTON WE HAVE AN ERROR!");
- }
- public boolean check(int done1, int done2) {
- boolean hasbeen = false;
- for( int a = 0; a < olmaize.size(); a++) {
- if(olmaize.get(a).equals(new Position (done1,done2))){
- hasbeen = true;
- break;
- }
- if(!hasbeen) {
- olmaize.add(new Position(done1, done2));
- return true;
- }
- }
- }
- public class Position{
- public int downL;
- public int upL;
- public Position (int downL, int upL) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement