0

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".
 }
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Louis B
  • 342
  • 1
  • 5
  • 21

1 Answers1

1

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!

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
  • Sorry, it is written and Java and thanks a lot for the pointing out the >= stuff I totally missed it. And your right as soon as i fixed the last 2 if statements i didnt go out of bounds. My main problem still remains though (where and when to reset the true and false), I dont understand your point about sending it null and i would like to stick to true or false. The program also stops after what looks like 3 or 4 circles only printing out 2 words, so if you could just help on the placement of setting tracker to true or false that would be amazing! – Louis B Feb 12 '13 at 23:21