|
Iterative Maze HELP!!!
I've been working on this for awhile now, and it's due Sunday night. We're suppose to use Stacks or Queues to find ALL possible solutions to a maze formated like this
0 0 1
1 0 0
where 0 is open space, 1 is a wall.
This is what I've got so far
Code:
/*
* Created on Feb 19, 2004
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package IterativeMaze;
import FileWriter.MazeWrite;
import stack.*;
import Points.Point;
public class MouseMaze {
//If points exceeds x, don't print it out
private int x;
//The maze
private int[][] maze;
//Stack
private DequeStack stackPoints;
private DequeStack path;
//Points
public int paths;
//Points
Point toCheck;
/**
* Constructor for IterativeMaze
* @param array, the maze
* @param x, how many points it can have
*/
public MouseMaze(int[][] array, int x){
maze = array;
this.x = x;
stackPoints = new DequeStack();
path = new DequeStack();
}
/**
* Prints the maze
*/
public void traverse(){
//New line
System.out.println();
//Loops through the maze
for(int i = 0; i < maze.length; i++){
for(int j = 0; j < maze[0].length; j++){
System.out.print(maze[i][j] + " ");
}
System.out.println();
}
}
public void reset(){
//New line
System.out.println();
//Loops through the maze
for(int i = 0; i < maze.length; i++){
for(int j = 0; j < maze[0].length; j++){
if(maze[i][j] == 3)
maze[i][j] = 0;
}
}
}
public boolean valid(int row, int col){
return (row >= 0 && row < maze.length) && (col >= 0 && col < maze[0].length);
}
public void solve(int row, int col){
Point points = new Point(row, col, row,col);
stackPoints.push(points);
while(!stackPoints.isEmpty()){
toCheck = (Point)stackPoints.pop();
if(valid(toCheck.x, toCheck.y)){
if(toCheck.x == maze.length - 1 && toCheck.y == maze[0].length -1){
traverse();
System.out.println("FOUND!!!!!!")
paths++;
trace(toCheck.oldX, toCheck.oldY);
}
else if(maze[toCheck.x][toCheck.y] == 0){
if(toCheck.x == 0 && toCheck.y == 0){
}
else if(toCheck.oldX < toCheck.x){
maze[toCheck.x][toCheck.y] = 10;//10 = up
}
else if(toCheck.oldX > toCheck.x){
maze[toCheck.x][toCheck.y] = 11;//11 = down
}
else if(toCheck.oldY < toCheck.y){
maze[toCheck.x][toCheck.y] = 12;//12 = left
}
else if(toCheck.oldY > toCheck.y){
maze[toCheck.x][toCheck.y] = 13;//13 = right
}
traverse();
stackPoints.push(new Point(toCheck.x + 1,toCheck.y, toCheck.x, toCheck.y));
stackPoints.push(new Point(toCheck.x - 1,toCheck.y, toCheck.x, toCheck.y));
stackPoints.push(new Point(toCheck.x,toCheck.y + 1, toCheck.x, toCheck.y));
stackPoints.push(new Point(toCheck.x,toCheck.y - 1, toCheck.x, toCheck.y));
}
}
}
}
public void trace(int newX, int newY){
path.makeEmpty();
Point newPoint = new Point(newX,newY, maze.length - 1, maze[0].length -1);
while(newX >= 0 && newY >= 0){
path.push(new Point(newX,newY,newX,newY));
if(maze[newX][newY] == 10){
maze[newX][newY] = 0;
newX = newX - 1;
System.out.println("X1"+newX);
}
else if(maze[newX][newY] == 11){
maze[newX][newY] = 0;
newX = newX + 1;
System.out.println("X2"+newX);
}
else if(maze[newX][newY] == 12){
maze[newX][newY] = 0;
newY = newY - 1;
System.out.println("Y1"+newY);
}
else if(maze[newX][newY] == 13){
maze[newX][newY] = 0;
newY = newY + 1;
System.out.println("Y2"+newY);
}
traverse();
}
path.push(new Point(newX,newY,newX,newY));
}
/*
public void solve(int row, int col){
Point points = new Point(row,col);
stackPoints.push(points);
toCheck = new Point();
while(!stackPoints.isEmpty()){
toCheck = (Point)stackPoints.pop();
if(valid(toCheck.x,toCheck.y) && maze[toCheck.x][toCheck.y] == 0){
//Found a solution
if(toCheck.x == maze.length - 1 && toCheck.y == maze[0].length - 1){
traverse();
paths++;
//trace();
}
else{
maze[toCheck.x][toCheck.y] = 3;
stackPoints.push(new Point(toCheck.x + 1,toCheck.y));
stackPoints.push(new Point(toCheck.x - 1,toCheck.y));
stackPoints.push(new Point(toCheck.x,toCheck.y + 1));
stackPoints.push(new Point(toCheck.x,toCheck.y - 1));
}
}
}
}
public void trace(){
int newX = maze.length - 1;
int newY = maze[0].length - 1;
Point newPoint = new Point(newX,newY);
path = new DequeStack();
while(newX != -1 && newY != -1){
path.push(newPoint);
if(maze[newX-1][newY] == 3 && valid(newX-1, newY)){
newPoint = new Point(newX-1,newY);
path.push(newPoint);
newX = newX - 1;
}
else if(maze[newX+1][newY] == 3 && valid(newX+1, newY)){
newPoint = new Point(newX+1, newY);
path.push(newPoint);
newX = newX + 1;
}
else if(maze[newX][newY-1] == 3 && valid(newX, newY -1)){
newPoint = new Point(newX, newY - 1);
path.push(newPoint);
newY = newY - 1;
}
else if(maze[newX][newY+1] == 3 && valid(newX, newY + 1)){
newPoint = new Point(newX, newY +1);
path.push(newPoint);
newY = newY + 1;
}
}
}*/
}
Anyone have any ideas, this stupid things due Sunday night. It's a 2 part program, the first half, the recursive part, is done, but I can't get this to work.
|