We are supposed to read data from a file like

2 2 0 0 w d d w 0 1 w w d d 1 0 d w w w 1 1 d w x d

The first two numbers represent the column and rows the rest of next two numbers indicate the starting point and what it is surrounded by w d d w (wall door door wall) x is exit.

The problem I have is when I try to do recursion like theMaze(currentRow + 1) or theMaze(currentRow, currentCol - 1) it gives me an out of bounds error. I do not know how to fix this.

Here are the two classes. I am sure the problem is in the Maze Class, but can't detect it.
Help!..

Only the solveMaze method and main can be changed, nothing else.(as per assignment instructions).

The goal is to reach the exit, and show the path to take. Go right, left, back

/////////////////MAZE CLASS//////////////////////

//CHANGE THE PRIVATE BACK TO IT'S ORIGINAL FORM.
package original;

import java.io.*;
import java.util.*;
public class Maze
{
    // The maze. ?
    private MazeCell [][] M;
    
    // M is an array of MazeCell.
    // Constructor for a maze of size length rows by width columns.
    public Maze (int length, int width)
    {
	M = new MazeCell [length] [width];
    }

    // Initialize a cell of the maze by setting the four sides
    // to one of DOOR, WALL or EXIT.  This method gets the object
    // representing the cell in position (row, col) and calls
    // the MazeCell constructor.
    
    //This is just one small block like 0,0
    public void initCell (int row, int col, MazeCell.WallType top, 
			  MazeCell.WallType right,
			  MazeCell.WallType bottom, 
			  MazeCell.WallType left)
    {
	M[row][col] = new MazeCell (top, right, bottom, left);
    }

    // Return the MazeCell object at position (row, col).
    public MazeCell getCell (int row, int col)
    {
	return M[row][col];
    }
    
    // Print out the maze.  Used for debugging purposes.
    public void print()
    {
	// Draw top and sides of the cells of the maze.  The
	// method draws the rows one at a time, starting at
	// the top of the maze.
	for (int row = 0; row < M.length; row++)
	    {
		System.out.println(renderTop(row));
		System.out.println(renderSides(row));
	    }
	
	// Draw bottom of row M.length - 1
	String theBottom = "";
	for (int col = 0; col < M[0].length; col++)
	    {
		if (M[0][col].getBottom() == MazeCell.WallType.WALL)
		    theBottom += "._";
		else if (M[0][col].getBottom() == MazeCell.WallType.EXIT)
		    theBottom += ".x";
		else
		    theBottom += ". ";
	    }
	theBottom += ".";
	System.out.println (theBottom);
    }
    
    // A method to help the print method.  This method constructs
    // a string representing the top of row "row" and returns it
    // to the caller.
    private String renderTop (int row)
    {
	String theTop = ".";
	for (int col = 0; col < M[row].length; col++)
	    {
		if (M[row][col].getTop() == MazeCell.WallType.WALL)
		    theTop += "_.";
		else if (M[row][col].getTop() == MazeCell.WallType.EXIT)
		    theTop += "x.";
		else // Must be a door
		    theTop += " .";
	    }
	return theTop;
    }
   

    // Another helper method.  This method constructs the left and
    // right sides of the cells in row "row" and returns this string
    // to the caller.
    private String renderSides (int row)
    {
	String theSide;
	
	// Handle the left side of the row as a special
	// case.
	if (M[row][0].getLeft() == MazeCell.WallType.WALL)
	    theSide = "|";
	else if (M[row][0].getLeft() == MazeCell.WallType.EXIT)
	    theSide = "x";
	else
	    theSide = " ";  // This shouldn't happen, since we
	                    // can't have a door that leads 
	                    // outside of the maze.
	
	// Now, draw the right side of every cell.
	for (int col = 0; col < M[row].length; col++)
	    if (M[row][col].getRight() == MazeCell.WallType.WALL)
		theSide += " |";
	    else if (M[row][col].getRight() == MazeCell.WallType.EXIT)
		theSide += " x";
	    else
		theSide += "  ";
	
	return theSide;
    }
    
    public static boolean solveMaze(Maze theMaze, int currentRow, int currentCol)
    {  
        System.err.println(currentRow + " " + currentCol);
        System.err.println(currentRow + " " + currentCol + " " + theMaze.M[currentRow][currentCol].getVisited());
                    
               if(theMaze.M[currentRow][currentCol].getVisited())               
               {
                   return true;                  
               }
        if(theMaze.M[currentRow][currentCol].getTop().equals(MazeCell.WallType.EXIT))
            {
                System.out.println("Success is eminent on top");
                return true;
            }
        else if(theMaze.M[currentRow][currentCol].getRight().equals(MazeCell.WallType.EXIT))
            {
                System.out.println("Success is eminent on the right");      
                return true;
            }
        else  if(theMaze.M[currentRow][currentCol].getBottom().equals(MazeCell.WallType.EXIT))
            {
                System.out.println("Success is eminent on the bottom");           
                 return true;
            }
        else      if(theMaze.M[currentRow][currentCol].getLeft().equals(MazeCell.WallType.EXIT))
            {
                System.out.println("Success is eminent on the left");
                return true;
//            theMaze.M[currentRow][currentCol].setVisited();
            }
            theMaze.M[currentRow][currentCol].setVisited();
            //System.out.println(currentRow + " " + currentCol);
            
         if(theMaze.M[currentRow][currentCol].getTop().equals(MazeCell.WallType.DOOR))
            { 
                System.out.println("Go Forward");
                solveMaze(theMaze,currentRow+1, currentCol);
                //System.out.println("DOOR TO THE TOP");
                return true;
            }
            
            else if(theMaze.M[currentRow][currentCol].getRight().equals(MazeCell.WallType.DOOR))
            {
                System.out.println("Go Right");
                solveMaze(theMaze,currentRow, currentCol+1);
                //System.out.println("DOOR TO THE Right");
                return true;
            }
            else if(theMaze.M[currentRow][currentCol].getBottom().equals(MazeCell.WallType.DOOR))
            {
                
                System.out.println("Go Back");
                solveMaze(theMaze,currentRow-1, currentCol);
                //System.out.println("DOOR TO THE BOTTOMs");
            }
            else if(theMaze.M[currentRow][currentCol].getLeft().equals(MazeCell.WallType.DOOR))
            {
                
                System.out.println("Go Left");
                solveMaze(theMaze,currentRow, currentCol - 1);
            }//*/
             
            System.err.println(currentRow + " " + currentCol + " " + theMaze.M[currentRow][currentCol].getVisited());
        
            /*
      Check if visisted;                |  if room[c][r] visited return true/false
     Look for exit;                      |  check for exit, look up,right,down, left;
     Mark visited;                       |  return true
     return false;                       |  Let the recursion begin ha ha ha ha ha ha;
     Look for door;                      |
     Go through door;                    |
     solveMaze(row + 1, col);            | Go forward
     solveMaze(row, col + 1);            | Go Right
     solveMaze(row - 1, col);            | Go down
     solveMaze(row, col -1);             | Go left
     */
        return true;
     }
    
    public static void main(String [] args)
    {
        DataInputStream dis = null; 
        String record = null; 
        int recCount = 0; 
        
     BufferedReader in = new BufferedReader( new InputStreamReader(System.in));

     Scanner stdin = new Scanner(System.in); //ALLOWS DATA INPUT
     System.out.println("Enter file name ");
     String input = stdin.nextLine(); //USER INPUTS DATA
       
  try { 

           File f = new File(input); 
           FileInputStream fis = new FileInputStream(f); 
           BufferedInputStream bis = new BufferedInputStream(fis); 
           dis = new DataInputStream(bis);  

           while ( (record=dis.readLine()) != null )

           { 
              recCount++; 
            //  System.out.println(recCount + ": " + record); 
           
                String delims =" " ; // DEFINES CHARACTER BEING USED
                
     StringTokenizer st = new StringTokenizer(record, delims);// ALLOWS THE OPERATORS
     
     int row = Integer.parseInt(st.nextToken()); // whole changes string to int
     
     int col = Integer.parseInt(st.nextToken());  // numerator    
            
        for(int w = 0; w < row * col; w++) 
        {// READS ALL DATA
     int frow = Integer.parseInt(st.nextToken()); // denominator
    
     int fcol = Integer.parseInt(st.nextToken());
      
     char del, del1, del2,del3; 
             
     del = ( st.nextToken() ).charAt( 0 ); // changes string to character
     
     del1 = ( st.nextToken() ).charAt( 0 );  // CHECKS CHARACTER ENTERED.
    
     del2 = ( st.nextToken() ).charAt( 0 );
      
     del3 = ( st.nextToken() ).charAt( 0 );
        
            //System.out.print(frow + "  ");
            //System.out.print(fcol);
            System.out.print("  " + del);
            System.out.print(del1);
            System.out.print(del2);
            System.out.print(del3);
            System.out.println(" ");
        //    System.err.println("YOU MADE PRIVATE M STATIC CHANGE IT BACK !"); 
            
          
        Maze N = new Maze(row,col);
        MazeCell q;
        q = new MazeCell(MazeCell.char2Wall(del),MazeCell.char2Wall(del1),
                MazeCell.char2Wall(del2),MazeCell.char2Wall(del3));
        
              N.initCell(frow, fcol, q.char2Wall(del), q.char2Wall(del1), q.char2Wall(del2), q.char2Wall(del3));
              N.getCell(frow,fcol);
  solveMaze(N,frow,fcol);
  
            //  N.M[row-1][col-1] = N.getCell(0,0);

      } //END OF FOR LOOP
  }
  }
  
    catch (IOException e)
        { 
           // catch io errors from FileInputStream or readLine() 
           System.out.println("Invalid filename !");// 

        }             
      
           } 

}

///////////////////////MAZECELL CLASS///////////////////////////////////

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package original;

/**
 *
 * @author Torbecire
 */
public class MazeCell {

    //Mazecell holds the enumtypes and tells us what the walls are.
    
    // enum's are static by default.  
    // So, to reference this type in another class, call the type
    // MazeCell.WallType.  To reference one of the members of the
    // enum, call it MazeCell.WallType.DOOR, for example.
    public enum WallType {DOOR, WALL, EXIT};

    // A cell will be an array of 4 WallType objects.
    // Cell[0] represents the top of the cell.
    // Cell[1] represents the right side of the cell.
    // Cell[2] represents the bottom of the cell.
    // Cell[3] represents the left side of the cell.
    private WallType [] cell;

// Each cell has a field indicating whether we have visited it yet.
    private boolean visited;

    // The constructor.  Get four new sides, initialize them
    // from the parameters and mark the cell as unvisited.
    public MazeCell (WallType top, WallType right, 
		     WallType bottom, WallType left)
    {
	cell = new WallType[4];
	cell[0] = top;
	cell[1] = right;
	cell[2] = bottom;
	cell[3] = left;
        
	visited = false;
    }

    // Accessors for the four sides, plus an accessor and mutator
    // for the visited field.
    public WallType getTop()
    {
	return cell[0];
    }

    public WallType getRight()
    {
	return cell[1];
    }

    public WallType getBottom()
    {
	return cell[2];
    }

    public WallType getLeft()
    {
	return cell[3];
    }

    public void setVisited()
    {
	visited = true;
    }

    public boolean getVisited()
    {
	return visited;
    }


    // Convert a character to a WallType value.
    // 'D', 'd' -> WallType.DOOR
    // 'W', 'w' -> WallType.WALL
    // 'X', 'x' -> WallType.EXIT
    public static WallType char2Wall (char c)
    {
	 switch (c)
 	    {
 	    case 'D':
 	    case 'd':
 		return WallType.DOOR;
 	    case 'W':
 	    case 'w':
 		return WallType.WALL;
 	    case 'X':
 	    case 'x':
 		return WallType.EXIT;
 	    default:
 		System.out.println ("Error, bad character in char2Wall.");
 		return null;
 	    }
     }

}

Recommended Answers

All 2 Replies

as per assignment instructions

Is this a school assignment?

No, It's part of a challenge set for me by one of my fellow programmers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.