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

Thread: Maze Solver with Stacks

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Maze Solver with Stacks

    I need to create a maze solver using a stack (not recursively). The maze always starts at [0][0] and ends at [mazeSize-1][mazeSize-1]. 0=open path, 1=wall.
    How can I make my program walk the maze and push the path to a stack (the col & row # of the maze array) and pop the stack when it hits the wall and try a different direction?

    Here's what I have (it's totally wrong):
    public class DoMaze {
    	private int mSize, x, y;
    	int[][] maze;
    	LinkedStack<String> stack;
     
    	public DoMaze()
    	{
    		x = 0;
    		y = 0;
    		stack = new LinkedStack<String>();
    	}
     
    	public void setMaze(int[][] m, int size)
    	{
    		maze = m;
    		mSize = size;
     
    		walk();
    	}
     
    	public void walk()
    	{
    		stack.push(""+x+","+y);
     
    		while (!isDone())
    		{
    			if (stack.isEmpty())
    				break;
     
    			moveRt();
    			stack.push(""+x+","+y);
    			if (isBlocked())
    				stack.pop();
     
    			moveDwn();
    			stack.push(""+x+","+y);
    			if (isBlocked())
    				stack.pop();
     
    			moveLt();
    			stack.push(""+x+","+y);
    			if (isBlocked())
    				stack.pop();
     
    			moveUp();
    			stack.push(""+x+","+y);
    			if (isBlocked())
    				stack.pop();
     
    		}
     
    		if (stack.isEmpty())
    		{
    			System.out.println("No solutions found!");
    		}
    		else
    		{
    			while (!stack.isEmpty())
    				System.out.println(stack.pop());
    		}
    	}
     
    	public boolean isDone()
    	{
    		if (x == mSize-1 && y == mSize-1)
    			return true;
     
    		return false;
    	}
     
     
    	public boolean isBlocked()
    	{
    		return (maze[x][y] == 1);
    	}
     
    	public void moveDwn()
    	{
    		if (y+1 < mSize)
    			y=y+1;
    	}
     
    	public void moveUp()
    	{
    		if (y-1 > -1)
    			y=y-1;
    	}
     
    	public void moveLt()
    	{
    		if (x-1 > -1)
    			x=x-1;
    	}
     
    	public void moveRt()
    	{
    		if (x+1 < mSize)
    			x=x+1;
    	}
     
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Maze Solver with Stacks

    (it's totally wrong)
    What makes you say it's totally wrong? What does it do or not do?

    Your question kinda says, "give me the code that makes my program do what I want it to".
    Try to describe what the program does correctly,
    what it does incorrectly,
    where in the code (marked with comments in the code) the program seems to show undesired operation,
    any opinion you have as to what you think is wrong,
    and finally describe the correct behavior you are trying to get in the end.

    Breaking the question down into something like this can even lead to discovering the problem and solution on your own. But even if it does not, it does assist others in figuring out the trouble (usually much faster) and offering advice.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Maze Solver with Stacks

    When I run it, my pc starts to freeze up like it's in an infinite loop so I have no clue what it does correctly. I don't know how to make it walk the maze while pushing and popping the stack.

Similar Threads

  1. stacks
    By bd0652 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 10th, 2012, 11:48 AM
  2. copying and comparing stacks
    By colerelm in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 22nd, 2011, 01:22 AM
  3. Help with Stacks
    By animelook in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 17th, 2010, 12:58 AM
  4. help for stacks
    By msa0127a in forum Algorithms & Recursion
    Replies: 0
    Last Post: October 3rd, 2010, 12:11 AM
  5. Palindrome Stacks
    By mgutierrez19 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 03:05 AM