我正在研究Boggle遊戲,並且我正在創建一個名爲findWord的方法,如果可以在「網格」中找到「單詞」,它將返回true。否則返回false私有成員變量網格具有字母網格。然而,當我運行我的主要方法時,它會一直打印出「找不到」,我無法弄清楚我犯了什麼錯誤!這是我的代碼查找單詞字母網格
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;
}
}
在你內心的for循環應該是條件'山坳
Gendarme
同樣的第二,如果,第二或幫助功能條件(只允許例外,但不是良好的做法:如果董事會保證有方形大小)。 – Aconcagua