I'm working on Boggle game and I'm creating a method called findWord which return true if "word" can be found in "grid". return false otherwise private member variable grid has the letter grid. however, when i run my main method it keeps print out "not found" and I couldn't figere out where i made a mistake! this is my code
public class BoggleGame_old {
LetterGrid grid;
private char[][]board;
boolean[][] visited;
public BoggleGame_old(LetterGrid g)
{
grid = g;
}
public boolean findWord(String word) {
for(int row=0;row<this.board.length;row++){
for(int col=0;col<this.board.length;col++){
if(this.find(word, row, col)){
return true;
}
}
}
return false;
}
//helping function
private boolean find(String word, int row, int col){
if(word.equals(""))
{
return true;
}
else if(row<0||row>=this.board.length||
col<0||col>=this.board.length||
this.board[row][col] != word.charAt(0))
{
return false;
}
else{
char c=this.board[row][col];
this.board[row][col]='*';
String curr=word.substring(1,word.length());
boolean res=this.find(curr, row-1, col-1)||
this.find(curr, row-1, col)||
this.find(curr, row-1, col+1)||
this.find(curr, row, col-1)||
this.find(curr, row, col+1)||
this.find(curr, row+1, col-1)||
this.find(curr, row+1, col)||
this.find(curr, row+1, col);
this.board[row][col]=c;
return res;
}
}
One issue I see is that you call this.find(curr, row+1, col)
twice, the second one should be this.find(curr, row+1, col+1)
. It would stop you from being able to check diagonally down/right, without seeing test cases I can't say if that's actually causing it to always fail.
You might find this interesting, it finds words horizontally, vertically and diagonally (but not in reverse direction):
public boolean findWord(String word)
{
if(word == null || word.isEmpty())
return true;
int rowMax = board.length - word.length();
int colMax = board[0].length - word.length();
if(rowMax < 0 || colMax < 0)
return false;
for (int row = 0; row < rowMax; ++row)
{
for (int col = 0; col < colMax; ++col)
{
boolean v = true;
boolean h = true;
boolean d = true;
for(int c = 0; c < word.length(); ++c)
{
v &= board[row + c][col] == word.charAt(c);
h &= board[row][col + c] == word.charAt(c);
d &= board[row + c][col + c] == word.charAt(c);
if(!(v | h | d))
break;
}
if(v|h|d)
return true;
}
}
return false;
}
Edit: Variant finding strings in opposite direction, too:
public boolean findWord(String word)
{
if(word == null || word.isEmpty())
return true;
int rowMax = board.length - word.length();
int colMax = board[0].length - word.length();
if(rowMax < 0 || colMax < 0)
return false;
StringBuilder reverse = new StringBuilder(word).reverse();
for (int row = 0; row < rowMax; ++row)
{
for (int col = 0; col < colMax; ++col)
{
boolean v = true;
boolean h = true;
boolean d = true;
boolean rv = true;
boolean rh = true;
boolean rd = true;
for(int c = 0; c < word.length(); ++c)
{
v &= board[row + c][col] == word.charAt(c);
h &= board[row][col + c] == word.charAt(c);
d &= board[row + c][col + c] == word.charAt(c);
rv &= board[row + c][col] == reverse.charAt(c);
rh &= board[row][col + c] == reverse.charAt(c);
rd &= board[row + c][col + c] == reverse.charAt(c);
if(!(v | h | d | rv | rh | rd))
break;
}
if(v | h | d | rv | rh | rd)
return true;
}
}
return false;
}
Edit 2: So many booleans... – a little bit compacter:
int flags = 0;
for(int c = 0; flags != 0b111111 && c < word.length(); ++c)
{
flags |= board[row + c][col ] == word.charAt(c) ? 0 : 1 << 0;
flags |= board[row ][col + c] == word.charAt(c) ? 0 : 1 << 1;
flags |= board[row + c][col + c] == word.charAt(c) ? 0 : 1 << 2;
flags |= board[row + c][col ] == reverse.charAt(c) ? 0 : 1 << 3;
flags |= board[row ][col + c] == reverse.charAt(c) ? 0 : 1 << 4;
flags |= board[row + c][col + c] == reverse.charAt(c) ? 0 : 1 << 5;
}
if(flags != 0b111111)
return true;
Just a pity that Java does not support converting boolean to int implicitly here (like eg C or C++ do – oh, by the way, does C#?), otherwise we could have written:
flags |= (a == b) << n;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.