0

我正在开发 Boggle 游戏,我正在创建一个名为 findWord 的方法,如果可以在“网格”中找到“单词”,则该方法返回 true。返回 false 否则私有成员变量 grid 有字母 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;
    }


}
4

2 回答 2

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(例如 C 或 C++ 会 - 哦,顺便说一句,C# 会不会?),否则我们可以这样写:

flags |= (a == b) << n;
于 2016-07-15T16:07:00.223 回答
0

我看到的一个问题是你打电话this.find(curr, row+1, col)两次,第二个应该是this.find(curr, row+1, col+1). 它会阻止您在不查看测试用例的情况下对角向下/向右检查,我不能说这是否真的导致它总是失败。

于 2016-07-15T16:01:25.727 回答