This is the letter-searching part of a Java Boggle program. Boggle is a game where you have a 5x5 board (in my case) and you try to find words by joining adjacent letters on the board.
For my assignment, I had to use recursion rather than an iterative approach. Here goes:
boolean findStart (String word, int x, int y){
found = false;
String firstLetter;
firstLetter=word.substring(0,1);
if (firstLetter.equals(letters[x][y])){
found = findWord(word, x, y, new boolean[5][5]);
}
else {
y++;
if (y > 4){
y = 0;
x++;
}
if (x > 4)
return false;
}
return findStart(word,x,y);
}
boolean findWord (String word, int x, int y, boolean[][] visited){
if (word.length() == 1){
found = true;
}
else
{
visited[x][y] = true;
List<Point> adjoining = getAdjoining(x,y,visited);
for (Point p : adjoining){
found = findWord(word.substring(1), p.x, p.y, visited);
if (found)
return true;
}
visited[x][y] = false;
}
return false;
}
Here are the errors:
Exception in thread "AWT-EventQueue-1" java.lang.StackOverflowError
at Assignment1B.getAdjoining(Assignment1B.java:117)
at Assignment1B.findWord(Assignment1B.java:103)
at Assignment1B.findWord(Assignment1B.java:106)
at Assignment1B.findStart(Assignment1B.java:80)
at Assignment1B.findStart(Assignment1B.java:92)
I didn't post my getAdjoining method, but it was just a method to find the adjoining letters on the board.
Where did I go wrong?
Thanks so much!