2016-07-15 91 views
0

我正在研究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; 
    } 


} 
+0

在你內心的for循環應該是條件'山坳 Gendarme

+0

同樣的第二,如果,第二或幫助功能條件(只允許例外,但不是良好的做法:如果董事會保證有方形大小)。 – Aconcagua

回答

0

一個問題我看到的是,你打電話this.find(curr, row+1, col)兩次,第二個應該是this.find(curr, row+1, col+1)。它會阻止你無法查看測試用例,如果這實際上導致它總是失敗,可以對角地向下/向右查看。

0

您可能會發現這個有趣的,它水平找到的話,垂直和對角線(而不是相反方向):

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; 
} 

編輯:變找到方向相反的字符串,太:

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; 
} 

編輯2:這麼多布爾... –有點緊湊:

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; 

只是遺憾的是,Java不支持在這裏隱式地將boolean轉換爲int(比如e。 G。 C或C++做–哦,順便說一句,不C#),否則我們可以這樣寫:

flags |= (a == b) << n;