Here is my code.... It is supposed to go throught this maze and can move where there are "." and blocked by "#". I keep getting an out of bounds error. Can anyone help me?

import java.io.*;
public class ProgramMAZE
{ public static char maze[][]=
            {{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
            {'#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#'},
            {'.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#'},
            {'#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#'},
            {'#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '.'},
            {'#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
            {'#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
            {'#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
            {'#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#'},
            {'#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#'},
            {'#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#'},
            {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}};
       public static int a1=2;
    public static int a2=0;
 public static int facingRight=1, facingLeft=2, facingUp=3, facingDown=4, direction=0;
    public static void main(String args[])
    throws java.io.IOException
    {
        BufferedReader br = new BufferedReader (new InputStreamReader(System.in));

          maze[a1][a2]='X';
          direction=facingRight;
        for(int j=1; j>0; j++){       
            for(  int x=0; x<=11; x++){
                for(  int y=0; y<=11; y++){
                    System.out.print(maze[x][y] + "  "); 
                }
                System.out.println();
            }

            System.out.println();


         mazeTraverse();
        }
    }
    public static void mazeTraverse(){
          // Direction is facing right
if(direction==1 && maze[a1+1][a2]=='#' && maze[a1][a2+1]=='.' && maze[a1-1][a2]=='#' && a2>0 && a2<11 && a1>0 && a1<11)
{maze[a1][a2+1]='X';
 direction=1;
     }
else if(direction==1 && maze[a1][a2+1]=='#' && maze[a1+1][a2]=='#' && maze[a1-1][a2]=='.' && a2>0 && a2<11 && a1>0 && a1<11)
       {   maze[a1-1][a2]='X';
          direction=3;


}
else if(direction==1 && maze[a1-1][a2]=='#' && maze[a1][a2+1]=='#' && maze[a1+1][a2]=='.' && a2>0 && a2<11 && a1>0 && a1<11)
        {  
            maze[a1+1][a2]='X';
            direction=4;


}
else if(direction==1 && maze[a1][a2-1]=='.' && maze[a1-1][a2]=='#' && maze[a1+1][a2]=='#' && maze[a1][a2+1]=='#' && a2>0 && a2<11 && a1>0 && a1<11)
       {  
           maze[a1][a2-1]='X';
           direction=2;


    }
       // Direction is facing left 
else if(direction==2 && maze[a1][a2-1]=='.' && maze[a1-1][a2]=='#' && maze[a1+1][a2]=='#' && maze[a1][a2+1]=='#' && a2>0 && a2<11 && a1>0 && a1<11)
       {  
           maze[a1][a2-1]='X';
           direction=2;


     }
else if(direction==2 && maze[a1][a2-1]=='#' && maze[a1-1][a2]=='.' && maze[a1+1][a2]=='#' && maze[a1][a2+1]=='#' && a2>0 && a2<11 && a1>0 && a1<11)
       {  
           maze[a1-1][a2]='X';
           direction=3;      
}
else if(direction==2 && maze[a1][a2-1]=='#' && maze[a1-1][a2]=='#' && maze[a1+1][a2]=='.' && maze[a1][a2+1]=='#' && a2>0 && a2<11 && a1>0 && a1<11)
{maze[a1+1][a2]='X';
 direction=4;                  
}
else if(direction==2 && maze[a1][a2-1]=='.' && maze[a1-1][a2]=='#' && maze[a1+1][a2]=='#' && maze[a1][a2+1]=='.' && a2>0 && a2<11 && a1>0 && a1<11)
{maze[a1][a2+1]='X';
 direction=1;       
}
           //Direction is facing up
else if(direction==3 && maze[a1][a2-1]=='.' && maze[a1-1][a2]=='#' && maze[a1+1][a2]=='#' && maze[a1][a2+1]=='#' && a2>0 && a2<11 && a1>0 && a1<11)
{maze[a1][a2-1]='X';
 direction=2;                  
}
else if(direction==3 && maze[a1][a2-1]=='#' && maze[a1-1][a2]=='.' && maze[a1+1][a2]=='#' && maze[a1][a2+1]=='#' && a2>0 && a2<11 && a1>0 && a1<11)
{ maze[a1-1][a2]='X';
  direction=3;                 
}
else if(direction==3 && maze[a1][a2-1]=='#' && maze[a1-1][a2]=='#' && maze[a1+1][a2]=='.' && maze[a1][a2+1]=='#' && a2>0 && a2<11 && a1>0 && a1<11)
{ maze[a1+1][a2]='X';
  direction=4;                 
}
else if(direction==3 && maze[a1][a2-1]=='.' && maze[a1-1][a2]=='#' && maze[a1+1][a2]=='#' && maze[a1][a2+1]=='.' && a2>0 && a2<11 && a1>0 && a1<11)
{maze[a1][a2+1]='X';
 direction=1;                   
}
           // Direction is facing down
else if(direction==4 && maze[a1][a2-1]=='.' && maze[a1-1][a2]=='#' && maze[a1+1][a2]=='#' && maze[a1][a2+1]=='#' && a2>0 && a2<11 && a1>0 && a1<11)
{  maze[a1][a2-1]='X';
   direction=2;                
}
else if(direction==4 && maze[a1][a2-1]=='#' && maze[a1-1][a2]=='.' && maze[a1+1][a2]=='#' && maze[a1][a2+1]=='#' && a2>0 && a2<11 && a1>0 && a1<11)
{ maze[a1-1][a2]='X';
  direction=3;
 }
else if(direction==4 && maze[a1][a2-1]=='#' && maze[a1-1][a2]=='#' && maze[a1+1][a2]=='.' && maze[a1][a2+1]=='#' && a2>0 && a2<11 && a1>0 && a1<11)
{ maze[a1+1][a2]='X';
  direction=4;
 }
else if(direction==4 && maze[a1][a2-1]=='.' && maze[a1-1][a2]=='#' && maze[a1+1][a2]=='#' && maze[a1][a2+1]=='.' && a2>0 && a2<11 && a1>0 && a1<11)
{ maze[a1][a2+1]='X';
  direction=1;
  }

    }
}

Recommended Answers

All 2 Replies

can anyone help? please!!!!!

I keep getting an out of bounds error.

Can you post the full text of the error message that shows where the error happens and what the value of the index is?

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.