Results 1 to 10 of 10

Thread: Recursion

  1. #1
    Deejay1992 is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Recursion

    Hi All,

    I am having a problem with this maze project that I was assigned in my programming and data structures class. I have everything done, except for a few minor steps of the Generator method here it is:

    Java Code:
     // The Gen n Method: Recursively Generates The Maze
    
    	private void genFile(int startX, int startY, int endX, int endY) {
    		Random gen = new Random();
    
    		// *** BASES CASES ***
    
    		// Check if we can divide the space
    
    		boolean wallPossible = false;
    
    		// Check all vertical wall placements
    
    		for (int i = startX + 1; i < endX; i++) {
    			if ( ((endY+1 < n) || maze[i][endY+1] != '_')    && 
    					((startY-1 >= 0) || maze[i][startY-1] != '_') ) {
    
    				// Do this when there is no wall opening mark on either side
    
    				wallPossible = true;
    				break;
    			}
    		}
    
    		if (!wallPossible) {
    
    			// Check all horizontal wall placements
    
    			for (int i=startY+1; i<endY; i++) {
    				if ( ((endX+1 < n) || maze[endX+1][i] != '_')    && 
    						((startX-1 >= 0) || maze[startX-1][i] != '_') ) {
    
    					// Do this when there is no wall opening mark on either side
    
    					wallPossible = true;
    					break;
    
    				}
    			}
    		}
    
    		if (!wallPossible) return;
    
    
    
    		// *** RECURSIVE CASE ***
    		// Once here, it means we can divide the space
    
    		// Loop that keeps asking for direction and location for a wall until valid
    
    		int dir = 0;
    		int k = 0;
    
    		do {
    
    			// Pick a random direction
    
    			dir = gen.nextInt(2);
    
    			// Pick a random wall location
    
    			if (dir == 0) {
    
    				// Generate a random number between the boundaries
    				// that will represent the (vertical) wall location
    
    				k = gen.nextInt (endy - starty - 1) + starty + 1;
    
    			} else {
    
    				// Generate a random number between the boundaries
    				// that will represent the (horizontal) wall location
    
    				k = gen.nextInt(endY - startY - 1) + startY + 1;
    
    			}
    
    			// Check if valid
    
    			//for (int i = startX + 1; i < endX; i++) {
    			if (dir==0) {
    				if ( ((endY+1 < n) || maze[k][endY+1] != '_')    && 
    						((startY-1 >= 0) || maze[k][startY-1] != '_') ) {
    
    					// Do this when there is no wall opening mark on either side
    
    					wallPossible = true;
    					break;
    				}
    			}
    
    			else {
    
    				// Check all horizontal wall placements
    
    				if ( ((endX+1 < n) || maze[endX+1][k] != '_')    && 
    						((startX-1 >= 0) || maze[startX-1][k] != '_') ) {
    
    					// Do this when there is no wall opening mark on either side
    
    					wallPossible = true;
    					break;
    				}
    			}
    
    		} while (!wallPossible); 
    
    
    		// Draw the wall
    
    		if (dir==0) 
    		{
    			for (int y = startY; y <= endY; y++)
    				maze [k][y] = '*';
    
    		}
    
    		else {
    			for (int x = startX; x <= endX; x++)
    				maze[k][x] = '*';
    
    		}
    
    		// Pick a random passage location on the wall
    		// Make the passage
    
    		int r = gen.nextInt(endY-startY) + 1;
    
    		if (dir == 0){
    			maze [k][r] = '_';
    		}
    		else{
    			maze [r][k] = '_';
    		}
    
    		// Recursively split the two spaces
    
    		// Find the coordinates of the first space
    
    		// Find the coordinates of the second space
    
    		// Call genFile for both space
    		// genFile( , , , );
    		// genFile( , , , );

    The last 6 steps that are COMMENTED OUT are the last steps that I need to finish this method please help.
    Last edited by Deejay1992; 11-27-2012 at 05:22 AM.

  2. #2
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Recursion

    Please use [ CODE] CODE HERE [ /CODE] (no spaces) to present your code, it is much easier to read.

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

    Default Re: Recursion

    Please go through the Forum Rules -- particularly the second paragraph. The other identical thread you started has been removed.

    Also go through Guide For New Members and BB Code List - Java Programming Forum and edit your post accordingly.

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

  4. #4
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Recursion

    So now that you did that, can you be more specific on your problem, can you not call the method you made? Make a main method and call genFile using the parameters you set.

  5. #5
    Deejay1992 is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Recursion

    I just need help with the last 6 comments at the end.

    The program can only contain 1 main method

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Recursion

    Cross posted at Recursive Maze Help

  7. #7
    Deejay1992 is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Recursion

    Is that illegal?

  8. #8
    Deejay1992 is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Recursion

    Quote Originally Posted by doWhile View Post
    Cross posted at Recursive Maze Help
    Can you help Sir doWhile??

  9. #9
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Recursion

    Is that illegal?
    No, just considered rude by many. Read the links in the response of your cross-post.

    Can you help Sir doWhile??
    I have tried elsewhere, but us helping you is quite dependent upon you helping us. 'I need help' is vague at best. Please provide requirements, a specific question, errors, etc...

  10. #10
    Deejay1992 is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Recursion

    Quote Originally Posted by doWhile View Post
    No, just considered rude by many. Read the links in the response of your cross-post.



    I have tried elsewhere, but us helping you is quite dependent upon you helping us. 'I need help' is vague at best. Please provide requirements, a specific question, errors, etc...
    You sound like me when I talk to my professor.

Similar Threads

  1. Recursion
    By fam2315 in forum New To Java
    Replies: 7
    Last Post: 03-22-2011, 12:23 AM
  2. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 06:26 PM
  3. Recursion
    By jachandru in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 12:52 PM
  4. Recursion help
    By rjg_2186 in forum New To Java
    Replies: 1
    Last Post: 01-02-2009, 08:03 AM
  5. Please help with recursion
    By pheonix in forum New To Java
    Replies: 9
    Last Post: 12-27-2008, 11:41 AM

Posting Permissions

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