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; } }