私は Boggle ゲームに取り組んでおり、「単語」が「グリッド」で見つかる場合に true を返す findWord というメソッドを作成しています。それ以外の場合は false を返します。それ以外の場合、プライベート メンバー変数 grid は文字グリッドを持ちます。ただし、メインメソッドを実行すると、「見つかりません」という出力が保持され、どこで間違いを犯したのかわかりませんでした。これは私のコードです
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;
}
}