keep going out of bounds in my recursive method to navigate and find all combinations of letters in a 4X4 matrix

This is part of a Boggle solver. I made a recursive method to traverse through a string matrix. MyLetteres has all the chars stored (ex. a, b, c...), MyLetteres1 is initialized as empty, and tracker is to false. These variables mark which coordinates in the matrix I have already visited (I cannot revisit coordinates). I can only move to adjacent coordinates (cannot skip). The parameter String word is initialized with a single letter (the starting point). int x and int y are my (x,y) coordinates. Ignore int p and int n.

The thing I am having trouble with is that I cannot seem to properly mark the coordinates that the method has already gone over, and then I cannot seem to reset tracker (last line) to false for the next getPaths() to run.

Here's my code please help!

public void getPaths(int p, int n,int x, int y,String word, boolean tracker[][],boolean MyLetteres1[][],String MyLetteres[][],boolean checker)throws IOException{
    if(word.length()>1)
        checker=Check(word);//Check() just checks to see if its a word.
    tracker[x][y]=true;//makes sure that the checkers never goes back over starting letter
    if(x+1<MyLetteres.length&&tracker[x+1][y]==false){//up{
        //checker=Check(word);//checks to see if its word 
        //reverse(word);
        System.out.print("1 ("+x+","+y+"), ");//for debugging purposes
        getPaths(n,p,x+1, y,word+MyLetteres[x+1][y], tracker,MyLetteres1,MyLetteres,true);//recursive part
    }
    if(x-1>0&&tracker[x-1][y]==false){//down
        //checker=Check(word);
        //reverse(word);
        System.out.print("2 ("+x+","+y+"), ");
        getPaths(n,p,x-1, y ,word+MyLetteres[x-1][y], tracker,MyLetteres1,MyLetteres,true);
    }
    if(y+1<MyLetteres.length&&tracker[x][y+1]==false){//right
        //checker=Check(word);
        //reverse(word);
        System.out.print("3 ("+x+","+y+"), ");
        getPaths(n, p,x , y+1,word+MyLetteres[x][y+1], tracker,MyLetteres1,MyLetteres,true);
    }
    if(y-1>0&&tracker[x][y-1]==false){//left
        //checker=Check(word);
        //reverse(word);
        System.out.print("4 ("+x+","+y+"), ");
        getPaths(n,p,x , y-1,word+MyLetteres[x][y-1], tracker,MyLetteres1,MyLetteres,true);
    }
    if(x+1<MyLetteres.length&&y+1<MyLetteres.length&&tracker[x+1][y+1]==false){//right, up
        //checker=Check(word);
        //reverse(word);
        System.out.print("5 ("+x+","+y+"), ");
        getPaths(n,p,x+1, y+1,word+MyLetteres[x+1][y+1], tracker,MyLetteres1,MyLetteres,true);
    }
    if(x-1>0&&y-1>0&&tracker[x-1][y-1]==false){//down, left
        //checker=Check(word);
        //reverse(word);
        System.out.print("6 ("+x+","+y+"), ");
        getPaths(n,p,x-1, y-1,word+MyLetteres[x-1][y-1], tracker,MyLetteres1,MyLetteres,true);
    }
    if(x-1>0&&y+1<MyLetteres.length&&tracker[x-1][y+1]==false){//down, right
        //checker=Check(word);
        //reverse(word);
        System.out.print("7 ("+x+","+y+"), ");
        getPaths(n,p,x+1, y-1, word+MyLetteres[x-1][y+1],tracker,MyLetteres1,MyLetteres,true);
    }
    if(x+1<MyLetteres.length&&y-1>0&&tracker[x+1][y-1]==false){//up, left
        //checker=Check(word);
        //reverse(word);
        System.out.print("8 ("+x+","+y+"), ");
        getPaths(n, p,x-1 , y+1, word+MyLetteres[x+1][y-1],tracker,MyLetteres1,MyLetteres,true);
    }
    tracker=deepCopyBoolean(MyLetteres1);//MyLetteres1 never changes so this is my attempt at resetting tracker (which does change) back to all false so that when the program starts a new path, nothing has been "visited".
 }


ANSWERS:


There are a lot of things you can do to help us or yourself with this. Most importantly, if you're going out of bounds, you should know on what line it's happening. Your debugger can tell you this, and telling us this would help a lot. It would also help to tell us what language you're coding in. It looks like Java to me.

In this case, it looks like in your last two if statements, you're passing the wrong x and y values in to getPaths. You have

if(x-1>0&&y+1 ... getPaths(n,p,x+1,y-1

and

if(x+1<MyLetteres.length&&y-1>0...getPaths(n, p, x-1, y+1

Fix those up, and you'll probably fix your out of range exception.

Incidentally, when you're checking your range, you probably want to check for x >= 0 and y >= 0 instead of x > 0 and y > 0. It's valid for an array index to equal 0.

Also, it's technically inaccurate to check if x + 1 < MyLetteres.length and if y + 1 < MyLetteres.length, since that's saying "is x + 1 less than the 'height' of the board" and "is y + 1 less than the 'height' of the board". Since your board is a square, it works out ok, but to be precise, you should have x + 1 < MyLetteres[0].length, which is the way to say "is x + 1 less than the 'width' of the board.

Finally, as for resetting the tracker, I would recommend allowing it to be null, and passing it as null in the top-level call. If it's null, you know you're at the start of a new word, and you can create your own and pass it into all the subsequent recursive calls.

Good luck!



 MORE:


 ? where is mistake ? php letter matrix boggle
 ? How to create a Boggle Board from a list of Words? (reverse Boggle solver!)
 ? How to find all possible words that can be created from a list of letters
 ? What is the fastest way to match string prefixes for the building of a Radix search tree?
 ? Depth first search or backtrack recursion for finding all possible combination of letters in a crossword puzzle/boggle board?
 ? Why does DFS work when trying to find all valid words in a Boggle board
 ? Java - Boggle / Word Matrix Solver Path Issues
 ? Algorithm for searching withing a group of words
 ? Algorithm for searching withing a group of words
 ? Algorithm for searching withing a group of words