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 7 of 7

Thread: Recursive Maze Help

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Recursive Maze Help

    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:

    // 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 steps are the last steps that I need to finish this method please help.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Recursive Maze Help

    Please don't triple post your question in the forum. Once will do. If an answer is that important to you, please read the forum FAQ to see how to format it so folks can read the code, understand it, and be able to help you.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Recursive Maze Help

    Do you have any advice for me??

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Recursive Maze Help

    Quote Originally Posted by Deejay1992 View Post
    Do you have any advice for me??
    You have not yet said what your goals are, or even asked a question related to your code. I'd presume you would rather us not guess...my advice would be to ask a specific question, read the link in my signature entitled 'Getting Help', format your code with proper indentations, and post an SSCCE (again, see my signature)

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Recursive Maze Help

    I just need to know how to recursively split the two spaces:

    if (dir == 0){
    			maze [k][r] = '_';
    		}
    		else{
    			maze [r][k] = '_';
    		}

    Find the coordinates for space 1 and 2
    Then call genFile for both spaces
    genFIle( , , , );
    genFIle( , , , );

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Recursive Maze Help

    I suggest reading post #4 again...sorry but I don't know what you mean by 'space' or 'split'. Explaining these as if we don't know what the heck you are talking about (which is close to the truth) will do a lot to help you get help.

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/65552-recursion.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Recursive Maze Help

    Quote Originally Posted by copeg View Post
    I suggest reading post #4 again...sorry but I don't know what you mean by 'space' or 'split'. Explaining these as if we don't know what the heck you are talking about (which is close to the truth) will do a lot to help you get help.

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/65552-recursion.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Well then let the people who have answered this question choose to not answer. I will not filter my curiosity. Plus I've searched all over searched all over with the same question. Nothing relates to this project.

Similar Threads

  1. Recursive Maze Help
    By Deejay1992 in forum Algorithms & Recursion
    Replies: 1
    Last Post: November 26th, 2012, 11:25 PM
  2. Recursive Maze Help
    By Deejay1992 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 26th, 2012, 11:25 PM
  3. Recursive Maze Help
    By chono in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 22nd, 2012, 08:02 PM
  4. BackTracking in a maze
    By skoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 26th, 2012, 10:23 AM
  5. Maze Game
    By whatsbruin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 15th, 2011, 05:59 PM