Results 1 to 7 of 7

Thread: Java maze help

  1. #1
    donulld is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Java maze help

    New to java. I've seen threads about the java maze solver already made, but mine is different. I think I've figured almost everything I need out, I just need to be able to recursively solve my maze. I've also found that my maze stops at a random place.

    Java Code:
    import java.io.*;
    import java.util.*;
    
    /*********
     * CLASS *
     *********/
    
    public class Project11 {
    
    	/********
    	 * MAIN *
    	 ********/
    
    	public static void main(String[] args) {
    
    		new Project11();
    
    	}//end Main
    
    	/**************
    	 * PROJECT 11 *
    	 **************/
    
    	public Project11() {
    
    		File mazeFile = null;
    		//check if file exits
    		String dir = ".";
    		File folder = new File(dir); // import -> import java.io.File;
    		File[] fileList = folder.listFiles();
    		for (int i = 0; i < fileList.length; i++) {
    			if (fileList[i].isFile()) {
    				String file = fileList[i].getName();
    				if (file.startsWith("maze") && file.endsWith(".txt")) {
    					System.out.println("Found " + file + "\n");
    					mazeFile = new File(file);
    				}//inner if
    			}//outer if
    		}//end for
    
    		//this creates the maze
    		char[][] maze = null;
    		try {
    			maze = createMaze(mazeFile);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		//this print the maze
    		printMaze(maze);
    		//this finds starting point
    		int[] startLocation = findStart(maze);
    		int startX = startLocation[0];
    		int startY = startLocation[1];
    
    		//find maze path
    		findPath(maze, startX, startY);  
    
    		printMaze(maze);
    
    
    	}//end Project11()
    
    	/***********
    	 * METHODS *
    	 ***********/
    
    	//create Maze()
    	public char[][] createMaze(File file) throws IOException {
    		Scanner myScanner = new Scanner(file);
    		char[][] maze = new char[7][14];
    		for (int i = 0; i < 7; i++) {
    			String nextLine = myScanner.nextLine();
    			for (int j = 0; j < 14; j++) {
    				char nextChar = nextLine.charAt(j);
    				maze[i][j] = nextChar;
    			}
    		}
    		return maze;
    
    	}//end createMaze()
    
    	//print Maze()
    	public void printMaze(char[][] maze) {
    		for (int i = 0; i < maze.length; i++) {  
    			if (maze[i] != null) {  
    				for (int j = 0; j < maze[i].length; j++) {  
    					System.out.print(maze[i][j]);  
    				}  
    				System.out.println();  
    			}  
    		}
    	}//end printMaze()
    
    	// Find the starting point
    	// returns int[] of start and end point [i][j] locations of 'S' & 'E' (S=0,1 & E=2,3)
    	public int[] findStart(char[][] maze) {
    		int[] startEndpts = new int[2];
    		for (int i = 0; i < maze.length; i++) {
    			if (maze[i] != null) {
    				for (int j = 0; j < maze[i].length; j++) {
    					if(maze[i][j] == 'S') {
    						startEndpts[0] = i;
    						startEndpts[1] = j;
    					}
    					/* --> For Finding End point (change maze size from 2:4 if using this)
         if(maze[i][j] == 'E') {
          startEndpts[2] = i;
          startEndpts[3] = j;
         }
    					 */
    				}
    			}
    		}
    		return startEndpts;
    	}//end findStart()
    
    	public void findPath(char[][] theMaze, int row, int col) {
    		boolean solved = false;
    		boolean wall = false;
    		char[][] maze = theMaze;
    
    		if (maze[row][col] == 'E') {
    			solved = true;
    			return;
    		}
    		maze[row][col] = 'H';
    		if (maze[row + 1][col] == ' ' || maze[row + 1][col] == 'E') {
    			findPath(maze, row + 1, col);
    		}
    		else if (maze[row][col + 1] == ' ' || maze[row][col + 1] == 'E') {
    			findPath(maze, row, col + 1);
    		}
    		else if (maze[row - 1][col] == ' ' || maze[row - 1][col] == 'E') {
    			findPath(maze, row -1, col);
    		}
    		else if (maze[row][col - 1] == ' ' || maze[row][col - 1] == 'E') {
    			findPath(maze, row, col - 1);
    		}
    		else {
    			wall = true;
    			return;
    		}
    		if (wall) {
    			wall = false;
    			findPath(maze, row, col);
    		}
    		if (solved) {
    			maze[row][col] = '+';
    		}
    		//marks change
    
    	}//end findPath()
    
    
    }//end Class
    This is the output:

    Found maze1.txt

    It stops like this:
    Java Code:
    ****H**E******
    * HHH*  ***  *
    **H***  ***  *
    **H ***HH**  *
    * H**  H***  *
    * HHHHHH***  *
    **************
    But should end like this:

    Java Code:
    ****H**E******
    * HHH* H***  *
    **H*** H***  *
    **H ***H **  *
    * H**  H***  *
    * HHHHHH***  *
    **************
    FYI: I don't fully understand recursion. I can picture it when viewing code, but cannot seem to grasp coding it myself.
    Thanks in advance. I'm new here (:

    Edit: I'm not understanding why my code does not completely finish and end at the end point "E" instead of stopping near it like it does in the output.
    Last edited by donulld; 08-19-2013 at 01:10 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,016
    Rep Power
    6

    Default Re: Java maze help

    You're not asking much of a question here, you're just dumping your code and stating "I don't understand". Okay... but a forum is not a substitute teacher or a school. If you want help, explain what you've been trying to do and where you are stuck.

    I see that you have managed to recursively call findPath() and I also see that you've managed to put code into place which will cause the recursion to stop under a specific condition, so you must understand something about recursiveness. So what about it DON'T you understand?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    donulld is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: Java maze help

    Sorry, left that out after copying and pasting my output, but I'm not understanding why my code does not completely finish and end at the end point "E" instead of stopping near it like it does in the output.

    It stops like this:
    Java Code:
    ****H**E******
    * HHH*  ***  *
    **H***  ***  *
    **H ***HH**  *
    * H**  H***  *
    * HHHHHH***  *
    **************
    But should end like this:

    Java Code:
    ****H**E******
    * HHH* H***  *
    **H*** H***  *
    **H ***H **  *
    * H**  H***  *
    * HHHHHH***  *
    **************

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: Java maze help

    Quote Originally Posted by donulld View Post
    New to java.
    Moved from Advanced Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,016
    Rep Power
    6

    Default Re: Java maze help

    Yeah its hard to see "the matrix" when you have recursive code in place. There is but one real solution: add informative System.out.println() statements to see the iterations that the code goes through and all the variables, then figure out where it is going wrong. Basic debugging 101 without actually using a debugger.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    donulld is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: Java maze help

    I've tried that, but I'm not understanding how inserting lines helps me. Are they line gaps suppose to show me how many times the how many times it travels each direction? And if so, how do I use that to my advantage?

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,016
    Rep Power
    6

    Default Re: Java maze help

    The idea is to print out useful information that might help you to see what you are not seeing right now, not to add "additional lines". But I understand that it is easy to misunderstand since you are directly outputting the maze to the System.out, so adding debug information in your application's current state is going to screw up the maze being printed.

    In stead what you might want to try is to put the maze to a StringBuilder object and print it out in one go at the end of your program, so while the maze is building you can add debug System.out.println() statements to your code to see the values in the variables you are using.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Solving a maze in java
    By kuro in forum New To Java
    Replies: 4
    Last Post: 04-11-2011, 11:41 PM
  2. Maze help
    By bap2 in forum Java Software
    Replies: 0
    Last Post: 01-14-2011, 12:31 AM
  3. Maze
    By soriano in forum New To Java
    Replies: 1
    Last Post: 12-16-2008, 05:40 PM
  4. Help me with create a 2D Maze in Java
    By hnam31190 in forum New To Java
    Replies: 4
    Last Post: 11-02-2008, 01:37 AM
  5. Java Maze Problem
    By mousey182 in forum New To Java
    Replies: 2
    Last Post: 03-28-2008, 06:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •