9 Replies Latest reply: Jan 5, 2008 6:31 PM by 807601 RSS

    recursive maze game

    807601
      hi, i am trying to develop a recursive maze game. i am able to traversal the maze bt i get an array index out of bound exception. the code which i have develped is (wall of maze is denoted by # n way is denoted as O, path with X )

      public void mazeTraversal(int a , int b)
      {
      print();
      if(a < 0 || b < 0 || a > maze.length || b > maze.length)//to check for boundaries of the maze.
      {
      return;
      }//end if
      maze[a]='X';// assumes that starting location has a path
      print();
      //east direction from the current location
      if(a>=0&&b>=0&&a<=maze.length&&b<=maze.length&&maze[a][b+1]=='0')

      maze[a][b+1]='X';
      mazeTraversal(a,b+1);
      }//end if
      //south direction from the current location
      else if(a>=0&&b>=0&&a<=maze.length&&b<=maze.length&&maze[a+1][b]=='0')
      {
      maze[a+1][b]='X';
      mazeTraversal(a+1,b);
      } //end else if

      //north direction from the current location
      else if(a>=0&&b>=0&&a<=maze.length&&b<=maze.length&&maze[a-1][b]=='0')
      {
      maze[a-1][b]='X';
      mazeTraversal(a-1,b);
      }//end else if
      //west direction from the current location
      else if(a>=0&&b>=0&&a<=maze.length&&b<=maze.length&&maze[a][b-1]=='0')
      {
      maze[a][b-1]='X';
      mazeTraversal(a,b-1);
      }//end else if

      else
      System.out.println("Sorry... no way out");

      }//end method

      thks in advance
        • 1. Re: recursive maze game
          807601
          Post your code using the code tags. You put {code} at the beginning of your code and again at the end - that way your code will be readable.

          Copy and post the exact error message and indicate which line of your code it is referring to.

          Edited by: pbrockway2 on Jan 6, 2008 1:27 PM

          Note that the length of an array is how many elements it has in it. This will be one greater than the highest legal array index. This is because array indices start from zero. For instance if we have an array
              a[0] a[1] a[2] a[3] a[4]
          then the length will be 5.

          For this reason is is usual to check for a valid index with something like:
          if(ndx < arr.length) { // not ndx <= length
              arr[ndx] = whatever;
          }
          • 2. Re: recursive maze game
            807601
            public void mazeTraversal(int a , int b)
                {
                   System.out.println("a:"+a +" b :"+b);
                   print();
                   if(a < 0 || b < 0 || a > maze.length || b > maze.length)//to check for boundaries of the maze.
                   {
                       return; 
                   }//end if 
                   maze[a]='X';// assumes that starting location has a path
            print();
            //east direction from the current location
            if(a>=0&&b>=0&&a<=maze.length&&b<=maze.length&&maze[a][b+1]=='0')

            maze[a][b+1]='X';
            mazeTraversal(a,b+1);
            }//end if
            //south direction from the current location
            else if(a>=0&&b>=0&&a<=maze.length&&b<=maze.length&&maze[a+1][b]=='0')
            {
            maze[a+1][b]='X';
            mazeTraversal(a+1,b);
            } //end else if

            //north direction from the current location
            else if(a>=0&&b>=0&&a<=maze.length&&b<=maze.length&&maze[a-1][b]=='0')
            {
            maze[a-1][b]='X';
            mazeTraversal(a-1,b);
            }//end else if
            //west direction from the current location
            else if(a>=0&&b>=0&&a<=maze.length&&b<=maze.length&&maze[a][b-1]=='0')
            {
            maze[a][b-1]='X';
            mazeTraversal(a,b-1);
            }//end else if

            else
            System.out.println("Sorry... no way out");

            }//end method
            error message :
            
            #00000Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
                 at Maze.mazeTraversal(Maze.java:46)
                 at Maze.mazeTraversal(Maze.java:50)
                 at Maze.mazeTraversal(Maze.java:57)
                 at Maze.mazeTraversal(Maze.java:57)
                 at Maze.mazeTraversal(Maze.java:57)
                 at Maze.mazeTraversal(Maze.java:50)
                 at Maze.mazeTraversal(Maze.java:50)
                 at Maze.mazeTraversal(Maze.java:50)
                 at Maze.mazeTraversal(Maze.java:50)
                 at Maze.mazeTraversal(Maze.java:50)
                 at Maze.mazeTraversal(Maze.java:65)
                 at Maze.mazeTraversal(Maze.java:65)
                 at Maze.mazeTraversal(Maze.java:72)
                 at Maze.mazeTraversal(Maze.java:72)
                 at Maze.mazeTraversal(Maze.java:72)
                 at Maze.mazeTraversal(Maze.java:65)
                 at Maze.mazeTraversal(Maze.java:65)
                 at Maze.mazeTraversal(Maze.java:65)
                 at Maze.mazeTraversal(Maze.java:65)
                 at Maze.mazeTraversal(Maze.java:65)
                 at Maze.mazeTraversal(Maze.java:50)
                 at Maze.mazeTraversal(Maze.java:50)
                 at Maze.mazeTraversal(Maze.java:50)
                 at Maze.mazeTraversal(Maze.java:50)
                 at Maze.mazeTraversal(Maze.ja000#0#                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
            • 3. Re: recursive maze game
              807601
              its giving error while calling mazeTraversal recursively.
              • 4. Re: recursive maze game
                807601
                i still have the same problem.
                • 5. Re: recursive maze game
                  807601
                  i still have the same problem.
                  Did you change the inequalities for every use of .length? (Including the boundary checks?) An AIOOB exception only occurs when access an array at an index <0 or >=length.

                  What is your code now, and the error message? Remember to indicate which line of your code the message is referring to.
                  • 6. Re: recursive maze game
                    807601
                    public void mazeTraversal(int a , int b)
                        { 
                           maze[a]='X';
                    print();
                    if(a < 0 || b < 0 || a > maze.length-1 || b > maze.length-1)//to check for boundaries of the maze.
                    {
                    return;
                    }//end if
                    // assumes that starting location has a path
                    //print();
                    //east direction from the current location
                    else if(maze[a][b+1]=='0')//ERROR

                    maze[a][b+1]='X';
                    mazeTraversal(a,b+1);//ERROR
                    }//end if
                    //south direction from the current location
                    else if(maze[a+1][b]=='0')//ERROR
                    {
                    maze[a+1][b]='X';
                    mazeTraversal(a+1,b);//ERROR
                    } //end else if

                    //north direction from the current location
                    else if(maze[a-1][b]=='0')//ERROR
                    {
                    maze[a-1][b]='X';
                    mazeTraversal(a-1,b);//ERROR
                    }//end else if
                    //west direction from the current location
                    else if(maze[a][b-1]=='0')//ERROR
                    {
                    maze[a][b-1]='X';
                    mazeTraversal(a,b-1);//ERROR
                    }//end else if

                    else
                    System.out.println("Sorry... no way out");

                    }//end method
                    giving error at line mazeTraversal()
                    First Maze:
                    -------------------------
                    
                    ############
                    #000#000000#
                    00#0#0####0#
                    ###0#0000#0#
                    #0000###0#00
                    ####0#0#0#0#
                    #00#0#0#0#0#
                    ##0#0#0#0#0#
                    #00000000#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -----------------------
                    First Maze Traversal :
                    -------------------------
                    ############
                    #000#000000#
                    X0#0#0####0#
                    ###0#0000#0#
                    #0000###0#00
                    ####0#0#0#0#
                    #00#0#0#0#0#
                    ##0#0#0#0#0#
                    #00000000#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #000#000000#
                    XX#0#0####0#
                    ###0#0000#0#
                    #0000###0#00
                    ####0#0#0#0#
                    #00#0#0#0#0#
                    ##0#0#0#0#0#
                    #00000000#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #X00#000000#
                    XX#0#0####0#
                    ###0#0000#0#
                    #0000###0#00
                    ####0#0#0#0#
                    #00#0#0#0#0#
                    ##0#0#0#0#0#
                    #00000000#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XX0#000000#
                    XX#0#0####0#
                    ###0#0000#0#
                    #0000###0#00
                    ####0#0#0#0#
                    #00#0#0#0#0#
                    ##0#0#0#0#0#
                    #00000000#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#0#0####0#
                    ###0#0000#0#
                    #0000###0#00
                    ####0#0#0#0#
                    #00#0#0#0#0#
                    ##0#0#0#0#0#
                    #00000000#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XX#000000#
                    XX#X#0####0#
                    ###0#0000#0#
                    #0000###0#00
                    ####0#0#0#0#
                    #00#0#0#0#0#
                    ##0#0#0#0#0#
                    #00000000#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#0####0#
                    ###X#0000#0#
                    #0000###0#00
                    ####0#0#0#0#
                    #00#0#0#0#0#
                    ##0#0#0#0#0#
                    #00000000#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#0####0#
                    ###X#0000#0#
                    #00X0###0#00
                    ####0#0#0#0#
                    #00#0#0#0#0#
                    ##0#0#0#0#0#
                    #00000000#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#0####0#
                    ###X#0000#0#
                    #00XX###0#00
                    ####0#0#0#0#
                    #00#0#0#0#0#
                    ##0#0#0#0#0#
                    #00000000#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#0####0#
                    ###X#0000#0#
                    #00XX###0#00
                    ####X#0#0#0#
                    #00#0#0#0#0#
                    ##0#0#0#0#0#
                    #00000000#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#0####0#
                    ###X#0000#0#
                    #00XX###0#00
                    ####X#0#0#0#
                    #00#X#0#0#0#
                    ##0#0#0#0#0#
                    #00000000#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#0####0#
                    ###X#0000#0#
                    #00XX###0#00
                    ####X#0#0#0#
                    #00#X#0#0#0#
                    ##0#X#0#0#0#
                    #00000000#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#0####0#
                    ###X#0000#0#
                    #00XX###0#00
                    ####X#0#0#0#
                    #00#X#0#0#0#
                    ##0#X#0#0#0#
                    #000X0000#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#0####0#
                    ###X#0000#0#
                    #00XX###0#00
                    ####X#0#0#0#
                    #00#X#0#0#0#
                    ##0#X#0#0#0#
                    #000XX000#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#0####0#
                    ###X#0000#0#
                    #00XX###0#00
                    ####X#0#0#0#
                    #00#X#0#0#0#
                    ##0#X#0#0#0#
                    #000XXX00#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#0####0#
                    ###X#0000#0#
                    #00XX###0#00
                    ####X#0#0#0#
                    #00#X#0#0#0#
                    ##0#X#0#0#0#
                    #000XXXX0#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#0####0#
                    ###X#0000#0#
                    #00XX###0#00
                    ####X#0#0#0#
                    #00#X#0#0#0#
                    ##0#X#0#0#0#
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#0####0#
                    ###X#0000#0#
                    #00XX###0#00
                    ####X#0#0#0#
                    #00#X#0#0#0#
                    ##0#X#0#X#0#
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#0####0#
                    ###X#0000#0#
                    #00XX###0#00
                    ####X#0#0#0#
                    #00#X#0#X#0#
                    ##0#X#0#X#0#
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#0####0#
                    ###X#0000#0#
                    #00XX###0#00
                    ####X#0#X#0#
                    #00#X#0#X#0#
                    ##0#X#0#X#0#
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#0####0#
                    ###X#0000#0#
                    #00XX###X#00
                    ####X#0#X#0#
                    #00#X#0#X#0#
                    ##0#X#0#X#0#
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#0####0#
                    ###X#000X#0#
                    #00XX###X#00
                    ####X#0#X#0#
                    ##0#X#0#X#0#
                    ##0#X#0#X#0#
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#0####0#
                    ###X#00XX#0#
                    #00XX###X#00
                    ####X#0#X#0#
                    #00#X#0#X#0#
                    ##0#X#0#X#0#
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#0####0#
                    ###X#0XXX#0#
                    #00XX###X#00
                    ####X#0#X#0#
                    #00#X#0#X#0#
                    ##0#X#0#X#0#
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#0####0#
                    ###X#XXXX#0#
                    #00XX###X#00
                    ####X#0#X#0#
                    #00#X#0#X#0#
                    ##0#X#0#X#0#
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#000000#
                    XX#X#X####0#
                    ###X#XXXX#0#
                    #00XX###X#00
                    ####X#0#X#0#
                    #00#X#0#X#0#
                    ##0#X#0#X#0#
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    
                    ############
                    #XXX#X00000#
                    XX#X#X####0#
                    ###X#XXXX#0#
                    #00XX###X#00
                    ####X#0#X#0#
                    #00#X#0#X#0#
                    ##0#X#0#X#0#
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#XX0000#
                    XX#X#X####0#
                    ###X#XXXX#0#
                    #00XX###X#00
                    ####X#0#X#0#
                    #00#X#0#X#0#
                    ##0#X#0#X#0#
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#XXX000#
                    XX#X#X####0#
                    ###X#XXXX#0#
                    #00XX###X#00
                    ####X#0#X#0#
                    #00#X#0#X#0#
                    ##0#X#0#X#0#
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#XXXX00#
                    XX#X#X####0#
                    ###X#XXXX#0#
                    #00XX###X#00
                    ####X#0#X#0#
                    #00#X#0#X#0#
                    ##0#X#0#X#0X
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#XXXXX0#
                    XX#X#X####0#
                    ###X#XXXX#0#
                    #00XX###X#00
                    ####X#0#X#0#
                    #00#X#0#X#0#
                    ##0#X#0#X#0#
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#XXXXXX#
                    XX#X#X####0#
                    ###X#XXXX#0#
                    #00XX###X#00
                    ####X#0#X#0#
                    #00#X#0#X#0#
                    ##0#X#0#X#0#
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#XXXXXX#
                    XX#X#X####X#
                    ###X#XXXX#0#
                    #00XX###X#00
                    ####X#0#X#0#
                    #00#X#0#X#0#
                    ##0#X#0#X#0#
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#XXXXXX#
                    XX#X#X####X#
                    ###X#XXXX#X#
                    #00XX###X#00
                    ####X#0#X#0#
                    #00#X#0#X#0#
                    ##0#X#0#X#0#
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    ------------------------Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
                         at Maze.mazeTraversal(Maze.java:46)
                         at Maze.mazeTraversal(Maze.java:49)
                         at Maze.mazeTraversal(Maze.java:55)
                         at Maze.mazeTraversal(Maze.java:55)
                         at Maze.mazeTraversal(Maze.java:55)
                         at Maze.mazeTraversal(Maze.java:49)
                         at Maze.mazeTraversal(Maze.java:49)
                         at Maze.mazeTraversal(Maze.java:49)
                         at Maze.mazeTraversal(Maze.java:49)
                         at Maze.mazeTraversal(Maze.java:49)
                    
                    
                    ############
                    #XXX#XXXXXX#
                    XX#X#X####X#
                    ###X#XXXX#X#
                    #00XX###X#X0
                    ####X#0#X#0#
                    #00#X#0#X#0#
                    ##0#X#0#X#0#
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    ############
                    #XXX#     at Maze.mazeTraversal(Maze.java:62)
                    at Maze.mazeTraversal(Maze.java:62)
                         at Maze.mazeTraversal(Maze.java:68)
                         at Maze.mazeTraversal(Maze.java:68)
                         at Maze.mazeTraversal(Maze.java:68)
                         at Maze.mazeTraversal(Maze.java:62)
                         at Maze.mazeTraversal(Maze.java:62)
                         at Maze.mazeTraversal(Maze.java:62)
                         at Maze.mazeTraversal(Maze.java:62)
                         at Maze.mazeTraversal(Maze.java:62)
                         at MazXXXXXX#
                    
                    
                    XX#X#X####X#
                    ###X#XXXX#X#
                    #00XX###X#XX
                    ####X#0#X#0#
                    #00#X#0#X#0#
                    ##0#X#0#X#0#
                    #000XXXXX#0#
                    ######0###0#
                    #000000#000#
                    ############
                    -------------------------
                    
                    e.mazeTraversal(Maze.java:49)
                         at Maze.mazeTraversal(Maze.java:49)
                         at Maze.mazeTraversal(Maze.java:49)
                         at Maze.mazeTraversal(Maze.java:49)
                         at Maze.mazeTraversal(Maze.java:55)
                         at Maze.mazeTraversal(Maze.java:55)
                         at Maze.mazeTraversal(Maze.java:55)
                         at Maze.mazeTraversal(Maze.java:55)
                         at Maze.mazeTraversal(Maze.java:49)
                         at Maze.mazeTraversal(Maze.java:55)
                         at Maze.mazeTraversal(Maze.java:55)
                         at Maze.mazeTraversal(Maze.java:55)
                         at Maze.mazeTraversal(Maze.java:49)
                         at Maze.mazeTraversal(Maze.java:49)
                         at Maze.mazeTraversal(Maze.java:62)
                         at Maze.mazeTraversal(Maze.java:49)
                         at TestMaze.main(TestMaze.java:51)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                    • 7. Re: recursive maze game
                      807601
                      i really need a solution to this problem. can u plz help me in solving this one

                      thks
                      • 8. Re: recursive maze game
                        807601
                        Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
                        at Maze.mazeTraversal(Maze.java:46)
                        I have no idea which line 46 is. You haven't said.

                        But your logic still leaves you open to accessing the array at invalid indices.

                        Take the east segment for instance:
                        if(maze[a][b+1]=='0')
                        {  
                            maze[a][b+1]='X';
                            mazeTraversal(a,b+1);//ERROR
                        }
                        This is going to cause an AIOOB exception if b is equal to maze.length-1 (ie 11). That's because the expression maze[a][b+1] ie maze[a][12] is outside the legal values for the array. This problem arises when the (a,b) coordinates refer to a point on the eastern side of the array. Your original code checked for this - albeit wrongly - and made the if condition a little more elaborate.
                        if(maze[a][b+1]=='0' && ???)
                        {  
                            maze[a][b+1]='X';
                            mazeTraversal(a,b+1);//ERROR
                        }
                        • 9. Re: recursive maze game
                          800308
                          maybe something like this from my minesweeper game...

                          this method traverses the matrix starting from the given cell... recursing those neighbouring cells which have no neighbouring mines, and displaying (but not recursing) those cells which have neighbouring mines.

                          "cells" is a matrix of Cell[settings.rows][settings.cols]
                            public void revealCells(Cell cell, Map<Cell,Object> ancestors) {
                              if (ancestors.containsKey(cell)) return;
                              if (cell.isRevealed()) return;
                              ancestors.put(cell, null);
                              cell.reveal();
                              if(cell.getNeighbours()==0) {
                                isVirgin = false;
                                int firstRow = Math.max(cell.row-1, 0);
                                int lastRow  = Math.min(cell.row+1, settings.rows-1);
                                int firstCol = Math.max(cell.col-1, 0);
                                int lastCol  = Math.min(cell.col+1, settings.cols-1);
                                for(int r=firstRow; r<=lastRow; r++) {
                                  for(int c=firstCol; c<=lastCol; c++) {
                                    Cell x = cells[r][c];
                                    if (x == cell) continue;
                                    if( !x.isBomb() && !x.isRevealed() ) {
                                      revealCells(x, ancestors);
                                    }
                                  }
                                }
                              }
                            }