Results 1 to 6 of 6
Thread: 3D Maze
- 11-14-2008, 12:05 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 11
- Rep Power
- 0
3D Maze
I am suppose to write a 3D maze program, but for some reason it doesn't seem to want to compile. Can anyone help me?
code posted below:
//************************************************** ******************
// Maze3D.java CSE 110 Fall 2008
// Represents a 3D maze of characters. The goal is to get
// from the top left back corner (0,0,0)
// to the bottom right front corner (n-1,n-1,n-1),
// following a path of 1's.
//************************************************** ******************
public class Maze3D {
private final int TRIED = 3;
private final int PATH = 7;
private final int BLOCKED = 0;
private final int OPEN = 1;
private int[][][] grid = {
{ { 1, 1, 1, 0, 0 }, { 0, 0, 1, 0, 0 }, { 0, 1, 1, 1, 0 },
{ 0, 1, 0, 0, 0 }, { 0, 1, 1, 0, 0 } },
{ { 0, 1, 0, 0, 0 }, { 0, 1, 1, 0, 0 }, { 1, 1, 1, 1, 0 },
{ 1, 1, 0, 1, 1 }, { 1, 0, 0, 0, 1 } },
{ { 1, 1, 1, 0, 0 }, { 0, 0, 1, 0, 0 }, { 0, 0, 0, 1, 0 },
{ 0, 1, 1, 1, 1 }, { 0, 1, 0, 0, 0 } },
{ { 0, 0, 0, 0, 0 }, { 0, 1, 1, 0, 0 }, { 0, 1, 0, 1, 1 },
{ 0, 0, 0, 0, 1 }, { 0, 1, 1, 0, 0 } },
{ { 0, 0, 1, 1, 0 }, { 0, 0, 1, 0, 0 }, { 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 1 }, { 0, 0, 1, 0, 1 } } };
// ------------------------------------------------------------------
// Attempts to recursively traverse the maze. Inserts special
// characters indicating locations that have been tried and that
// eventually become part of the solution.
// ------------------------------------------------------------------
public boolean traverse(int row, int column, int depth) {
boolean done = false;
if(traverse(row,column,depth))
{
grid[row][column][depth]= TRIED;
if(row==grid.length-1&&column==grid[0].length-1&&depth==grid[0][0].length-1)
done=true;
else
{
done=traverse(row+1,column,depth);
if(!done)
done=traverse(row,column+1,depth);
if(!done)
done=traverse(row,column,depth+1);
if(!done)
done=traverse(row-1,column,depth);
if(!done)
done=traverse(row,column-1,depth);
if(!done)
done=traverse(row,column,depth-1);
}
if (done)
grid[row][column][depth]=PATH;
}
return done;
}
// ------------------------------------------------------------------
// Determines if a specific location is valid.
// ------------------------------------------------------------------
private boolean isValid(int row, int column, int depth) {
boolean result = false;
if(row>=0 && row<grid.length && column>=0 && column<grid[row].length && depth>=0 &&
depth<grid[column].length)
if (grid[row][column][depth]==1)
result = true;
return result;
}
// Print path in sequence of coordinates (row, col, depth)
public void printPath() {
int row = 0, col = 0, depth = 0;
while (row < grid.length && col < grid[0].length
&& depth < grid[0][0].length) {
System.out.println("[" + row + ", " + col + ", " + depth + "]");
// this will temporarily marks visited path
grid[row][col][depth] *= -1;
if (isPath(row, col, depth + 1))
depth++;
else if (isPath(row, col, depth - 1))
depth--;
else if (isPath(row, col + 1, depth))
col++;
else if (isPath(row, col - 1, depth))
col--;
else if (isPath(row + 1, col, depth))
row++;
else if (isPath(row - 1, col, depth))
row--;
else {
break;
}
}
// revert back to the original (non-negative)
for (row = 0; row < grid.length; row++)
for (col = 0; col < grid[row].length; col++)
for (depth = 0; depth < grid[row][col].length; depth++)
if (grid[row][col][depth] < 0)
grid[row][col][depth] *= -1;
}
// Check if current coordinate (row, col, depth) belongs to path
private boolean isPath(int row, int col, int depth) {
boolean result = true;
if ((isPath(row,column,depth)) == PATH)
result = true;
return result;
}
// ------------------------------------------------------------------
// Returns a string representation of the maze.
// ------------------------------------------------------------------
public String toString() {
String result = "\n";
int row, column, depth;
for (row = 0; row < grid.length; row++) {
for (column = 0; column < grid[row].length; column++) {
for (depth = 0; depth < grid[row][column].length; depth++)
result += grid[row][column][depth] + "";
result += "\n";
}
result += "\n";
}
return result;
}
}
//************************************************** ******************
// MazeSearch.java Java Foundations
//
// Demonstrates recursion by solving a maze traversal.
//************************************************** ******************
public class MazeSearch
{
//------------------------------------------------------------------
// Creates a new maze, prints its original form, attempts to
// solve it, and prints out its final form.
//------------------------------------------------------------------
public static void main (String[] args)
{
Maze3D labyrinth = new Maze3D();
System.out.println (labyrinth);
if (labyrinth.traverse (0, 0, 0)) {
System.out.println ("The maze was successfully traversed!");
labyrinth.printPath();
}
else
System.out.println ("There is no possible path.");
System.out.println (labyrinth);
}
}
-
Usually when the compiler complains, it's very specific in what it's complaining about and where the complaint is coming from. Can you share these details with us?
-
OK, this:
is a bit funky. Sure the column I suppose should be col, but what exactly are you trying to do here? Are you sure that you want a recursive method here? I think not. I think that you may be best served by redoing this method from scratch.Java Code:private boolean isPath(int row, int col, int depth) { boolean result = true; if ((isPath(row, column, depth)) == PATH) result = true; return result; }
- 11-14-2008, 12:44 AM #4
Member
- Join Date
- Oct 2008
- Posts
- 11
- Rep Power
- 0
yea that was the only one that I wasn't sure about. But in the question I am given I am suppose to "Check if current coordinate (row, col, depth) belongs to path" using isPath. I'm just unsure how to go about doing that.
-
Well, I suppose that you want to peek into your 3D grid to see if you're on the path, don't you think?
Never randomly type code without having a plan else you'll have disaster.
- 11-14-2008, 12:51 AM #6
Member
- Join Date
- Oct 2008
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Help me with create a 2D Maze in Java
By hnam31190 in forum New To JavaReplies: 4Last Post: 11-02-2008, 01:37 AM -
Java Maze Problem
By mousey182 in forum New To JavaReplies: 2Last Post: 03-28-2008, 06:29 PM -
Maze Help
By Soda in forum Advanced JavaReplies: 1Last Post: 12-22-2007, 03:26 AM -
maze problem (file to a 2d array)
By rnavarro9 in forum New To JavaReplies: 4Last Post: 11-09-2007, 07:36 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks