Results 1 to 6 of 6

Thread: 3D Maze

  1. #1
    Join Date
    Oct 2008
    Posts
    11
    Rep Power
    0

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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    OK, this:
    Java Code:
      private boolean isPath(int row, int col, int depth)
      {
    
        boolean result = true;
    
        if ((isPath(row, column, depth)) == PATH) result = true;
    
        return result;
    
      }
    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.

  4. #4
    Join Date
    Oct 2008
    Posts
    11
    Rep Power
    0

    Default

    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  6. #6
    Join Date
    Oct 2008
    Posts
    11
    Rep Power
    0

    Default

    haha yea.
    thanks
    I'll see what I can do, and then try to compile later tonight when I get home.

Similar Threads

  1. Help me with create a 2D Maze in Java
    By hnam31190 in forum New To Java
    Replies: 4
    Last Post: 11-02-2008, 02:37 AM
  2. Java Maze Problem
    By mousey182 in forum New To Java
    Replies: 2
    Last Post: 03-28-2008, 07:29 PM
  3. Maze Help
    By Soda in forum Advanced Java
    Replies: 1
    Last Post: 12-22-2007, 04:26 AM
  4. maze problem (file to a 2d array)
    By rnavarro9 in forum New To Java
    Replies: 4
    Last Post: 11-09-2007, 08:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •