I'm working on a Boggle game and some people told me the best way to search words is using recursion. I'm trying out a searchWord method to search the word. If the first letter is found, the method calls itself and drops the first letter. The method returns true when the length == 0 (word is found) or false (when the letter is not found). Problem is sometimes in boggle one "dice" has a same letter multiple times around it... To solve that I need to count that letter and if it's there more than once, it should search for the next appearance of that letter (search for the same word without dropping first letter). I need a way to memorize that letter and the index of the letter around which the multiple letters bound so it can be used when the letter is not found to check if there would be other possibilities. Since it's a recursive method I have no idea how to put these in a variable because they would be changed whenever the method calls itself...
Hope you guys can help! Here's the method:
public boolean searchWord(String word, int i, int j) {
boolean res;
if (word.length() == 0) {
res = true;
} else {
String theWord = word.toUpperCase();
int[] indexes = searchLetter(Character.toString(theWord.charAt(0)), i, j);
if (indexes[0] > -1) {
res = searchWord(theWord.substring(1), indexes[0], indexes[1]);
} else if(countLetter(Character.toString(/*The value that has to be memorized*/), /*index 1 that has been memorized*/, /*index 2 that has been memorized*/) > 1) {
res = searchWord(theWord, i, j);
} else {
res = false;
}
}
return res;
}
Note: yes I use Strings which is weird because chars may be a better option but I could change that later.