Skip to content
Advertisement

How to rectify my Java exception overflow: index 3 of 3 out of bounds

I am running into an overflow exception. This is a homework assignment; I am not looking for direct answers as that will not help me learn. This assignment is for my Artificial Intelligence class. We are solving the n-puzzle problem using best-first and A* algorithms.

I have a 2D array that represents my game board. there are 8 tiles in total with one blank space. Those who are unfamiliar with n-puzzle. The point is to go from a starting configuration of tiles to the goal configuration by moving the blank tile shifting the other 8 pieces around. So far in this assignment, I am implementing the movement of the blank pieces swapping the blank with the upper, left, right, or closest down tile. I also have my functionality to display the board. I have all my directional movements implemented as well.

I tried declaring a temp 2d array and used the built-in function Arrays.copyOf(board, board.length). When compiling it complained about being passed a 2d array. I am just not sure how I am going out of bounds with my checks. Also if the array was updated then ‘b’ would be found at (i= 0, j = 1), adding one to j puts me at the edge of the array. This leads me to believe that I need to copy my 2d array before making more changes. If the array did not actually update ‘b’ would be on my edge, then I tried moving it right out of bounds. That makes sense to me, but it is showing the array made changes in my output.

I know this is a low-level question. I just can’t wrap my head around how to copy arrays in Java, Thanks kindly. My reference right now is “Thinking in Java 4th edition.” By Bruce Eckel

Below you will find my main code flow of execution, my moveBlankLeft/Right functions implemented, and the overflow exception produced from running the code.

       public static void main(String[] args)
        {
          bestFirst game = new bestFirst( );
          game.displayBoard( );
          game.moveBlankLeft(game.moves, game.defheight, game.defwidth);
          game.displayBoard( );
          game.moveBlankRight(game.moves, game.defwidth, game.defheight);
          game.displayBoard( );
        }
    int moveBlankLeft(int moves, int defwidth, int defheight)
    {
        moves = moves + 1;
        for(int i = 0; i < defheight; i++) 
        {
            for(int j = 0; j < defwidth; j++)
            {
                if((i < defheight && j < defwidth && i >= 0 && j >= 
                0)&&board[i][j]=='b')
                {
                    char temp = board[i][j - 1]; 
                    board[i][j] = temp;
                    board[i][j - 1] = 'b';
                }
            }
         }
          System.out.println(" Total moves in solving: " +moves);
          return 0;
    }
    int moveBlankRight(int moves, int defwidth, int defheight)
    {
        moves = moves + 1;
        for(int i = 0; i < defheight; i++) 
        {
            for(int j = 0; j < defwidth; j++)
            {
                if((i < defheight && j < defwidth && i >= 0 && j >= 
                0)&&board[i][j]=='b')
                {
                      char temp = board[i][j + 1]; <-- culprit line...
                      board[i][j] = temp;
                      board[i][j + 1] = 'b';
                }
             }
         }
         System.out.println(" Total moves in solving: " +moves);
         return 0;
     }
Welcome to the best-first n-puzzle challenge solver.
4 5 b
6 1 8        = initial board
7 3 2
 Total moves in solving: 1
4 b 5
6 1 8        = board after shiftBlankLeft(...)
7 3 2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
        at bestFirst.moveBlankRight(best-first.java:83)
        at bestFirst.main(best-first.java:161)

Advertisement

Answer

In the moveBlankRight method you should replace your implementation with :

if(j== defwidth-1){
    char temp= bord[i+1][0];
    board[i+1][0]='b';
    board[i][j]=temp;
}
else{
    char temp = board[i][j + 1]; 
    board[i][j] = temp;
    board[i][j + 1] = 'b';
}

Like that the output will be :

Welcome to the best-first n-puzzle challenge solver.
4 5 b
6 1 8        = initial board
7 3 2
 // move right of b
4 5 6
b 1 8        = board after shiftBlankRight
7 3 2

You should also add the condition if(i==defheight-1)

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement