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".
}