Reputation: 11
I am trying to solve a boggle word checker problem, I am almost there but I need some help with the logic. Boggle word checker takes a 2d array and a string and I want to determine whether that string is a valid boggle word returning either true or false. So valid guesses are strings which can be formed by connecting adjacent cells (horizontally, vertically, or diagonally) without re-using any previously used cells.
For example EAR is correct but EARS is not.
From what I can tell, it seems to work properly until it gets to the last letter and then I am having issues with it returning correctly T/F. I tried using an if statement so that it would stop the recursive call but it doesn't seem to be working. Any advice on how to figure it out would be amazing!
final private static char[][] board = {
{'E','A','R','A'},
{'N','L','E','C'},
{'I','A','I','S'},
{'B','Y','O','R'}
}
public class Boggle {
char[][] board;
String word;
public Boggle(final char[][] board, final String word) {
this.board = board;
this.word = word;
}
public boolean checkit() {
//make string array
//iterated through board
//if word[0] == board value
//check -> see if value above beloow left right is a match to word[1]
System.out.println("word: "+word);
char[] charArr = word.toCharArray();
int index = 0;
boolean valid = false;
for(int row=0; row< board.length; row++) {
for(int col=0; col<board[0].length; col++) {
if(board[row][col] == charArr[index]) {
return check(board, charArr, row, col, index);
}
}
}
return valid;
}
public boolean check(char[][] board, char[] charArr, int row, int col, int index) {
index++;
//while index< charArr.length, keep searching for matches
System.out.println("index: "+index + " chararr.length: "+charArr.length);
if(index >= charArr.length) return true;
else if(index < charArr.length) {
System.out.println("here");
//check up
if(row-1 >= 0) {
if(board[row-1][col] == charArr[index]) {
check(board, charArr, row-1, col, index);
}
}
//check up/right (diagonal)
else if((row-1 >= 0) && (col+1 < board[0].length)) {
if(board[row-1][col+1] == charArr[index]) {
check(board, charArr, row-1, col+1, index);
}
}
//check right
else if(col+1 < board[0].length) {
if(board[row][col+1] == charArr[index]) {
System.out.println("right");
check(board, charArr, row, col+1, index);
}
}
//check right/down (diagonal)
else if((col+1 < board[0].length) && (row+1 < board.length)) {
if(board[row+1][col+1] == charArr[index]) {
check(board, charArr, row+1, col+1, index);
}
}
//check down
else if(row+1 < board.length) {
if(board[row+1][col] == charArr[index]) {
check(board, charArr, row+1, col, index);
}
}
//check down/left (diagonal)
else if((col-1 >= 0) && (row+1 < board.length)) {
if(board[row+1][col-1] == charArr[index]) {
check(board, charArr, row+1, col-1, index);
}
}
//check left
else if(col-1 >= 0) {
if(board[row][col-1] == charArr[index]) {
check(board, charArr, row, col-1, index);
}
}
//check left/up (diagonal)
else if((col-1 >= 0) && (row-1 >= 0)) {
if(board[row-1][col-1] == charArr[index]) {
check(board, charArr, row-1, col-1, index);
}
}
//no match
System.out.println("false section");
return false;
} //end while
return false;
}
}
Upvotes: 0
Views: 222
Reputation: 25282
Problems I see include:
You don't look at the return result of check()
.
You allow a word to use the same letter twice.
The following code is redundant.
if(index >= charArr.length) return true; else if(index < charArr.length) {
If it gets past the first line, we know the condition must pass, so there is no need to check it again.
Upvotes: 0