Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Recursion Maze

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Recursion Maze

    Heres what I have:
    import java.io.*;
     
    public class Driver {
    	private File mapFile = new File("map.txt");
    	private char[][]maze;
    	private char pathMarker = 'a';
    	private int row,col,ndx=0;
    	public static void main(String[] args) {
    		Driver d = new Driver();
    	}
    	public Driver(){
    		loadMap(mapFile);
    		row=startRow(maze);
    		col=startColumn(maze);
    		maze[row][col]=' ';
    		findPath(maze,row,col);
    		for (int i = 0; i < maze.length; i++) {
    	        for (int j = 0; j < maze[i].length; j++) {
    	            System.out.print(maze[i][j]);
    	        }
    	        System.out.println();
    	    }
    	}
     
    	private void loadMap(File f){
    		try{
    			String line,line2;
    			int ndx=0,ndx2=0;
    			FileReader fReader,fReader2;
    			BufferedReader reader,reader2;
    			fReader = new FileReader(f);
    			reader = new BufferedReader(fReader);
    			while ((line = reader.readLine()) != null ) {
    				ndx++;
    			}
    			reader.close();
    			fReader2 = new FileReader(f);
    			reader2 = new BufferedReader(fReader2);
    			maze = new char [ndx][];
    			while ((line2 = reader2.readLine()) != null) {
    			    maze[ndx2] = line2.toCharArray();
    			    ndx2++;
    			}
    			reader2.close();
    		}catch(IOException e){
    			System.out.println("INVALID FILE");
    		}
    	}
    	  public int startRow(char [][]arr){
    		  int z=0;
    		  for(int i=0;i!=arr.length;i++){
    			  for(int j=0; j!=arr[i].length;j++){
    				  if(arr[i][j]=='a'){
    					  z=i;
    				  }
    			  }
    		  }return z;
    	  }
    	  public int startColumn(char[][]arr){
    		  int z=0;
    		  for(int i=0;i!=arr.length;i++){
    			  for(int j=0; j!=arr[i].length;j++){
    				  if(arr[i][j]=='a'){
    					  z=j;
    				  }
    			  }
    		  }return z;  
    	  }
    	  public boolean endFound(char[][] arr,int r, int c){
    		  if ((!(r < arr.length)) || r < 0) {
    		        return false;
    		    }
     
    		    if ((!(c < arr[r].length)) || c < 0) {
    		        return false;
    		    }
     
    		    if ((r - 1) > -1) {
    		        if (arr[r - 1][c] == '*') {
    		            return true;
    		        }
    		    }
    		    else if((r+1) <= arr.length){
    		    	if(arr[r+1][c] =='*'){
    		    		return true;
    		    	}
    		    }
    		    else if((c-1)>-1){
    		    	if(arr[r][c-1]=='*'){
    		    		return true;
    		    	}
    		    }
    		    else if((c+1)<=arr[r].length){
    		    	if(arr[r][c+1]=='*'){
    		    		return true;
    		    	}
    		    }
    			return false;
    	  }
    	  public boolean findPath(char[][] arr, int r, int c){
     
    			  if(r<0 || r>arr.length || c<0 || c>arr.length){
    				  return false;
    			  }
    			  else if(endFound(arr,r,c)==true){
    				  //else if(maze[r][c]=='*'){
    					  System.out.println("FINISHED!");
    					  for(int i=0;i!=arr.length;i++){
    						  System.out.println(arr[i]);
    					  }
    					  return true;
    				  }
    			  else if(maze[r][c]=='#'){
    				  return false;
    			  }
    			  else if(maze[r][c]!=' '){
    				  return false;
    			  }
    			  else{
    				  maze[r][c] = pathMarker;
    				  pathMarker++;
    				  ndx++;
    				  if(ndx==26){
    					  pathMarker='a';
    					  ndx=0;
    				  }
    				  if(findPath(arr,r+1,c)){
    					  return true;
    				  }else if(findPath(arr,r,c+1)){
    					  return true;
    				  }else if(findPath(arr,r,c-1)){
    					  return true;
    				  }else if(findPath(arr,r-1,c)){
    					  return true;
    				  }else{
    					  //maze[r][c]=' ';
    					  return false;  
    				  }
    	  }
    	  }
    }
    I don't know why, but when It is run it prints out the maze with every blank filled with letters. It does not stop at the '*' like it is suppose to. Am I just missing something or do I have a coding error?
    Last edited by sman1234; October 28th, 2010 at 01:00 PM.


Similar Threads

  1. Recursion
    By javapenguin in forum Algorithms & Recursion
    Replies: 12
    Last Post: October 18th, 2010, 04:42 PM
  2. Recursion Help
    By vmr in forum Algorithms & Recursion
    Replies: 3
    Last Post: April 2nd, 2010, 12:27 AM
  3. Recursion help
    By rhoruns in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 8th, 2010, 11:50 PM
  4. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 05:30 AM
  5. Problem in recursion for Solving Maze Recursively program
    By _Coder1985 in forum Algorithms & Recursion
    Replies: 1
    Last Post: April 29th, 2009, 05:37 AM