gpt4 book ai didi

java - 我的 Boggle 程序找不到所有有效单词 - Java

转载 作者:行者123 更新时间:2023-12-01 15:06:09 26 4
gpt4 key购买 nike

所以我正在用java编写一个Boggle程序,但我很难让它找到每个可能的单词。

它几乎找到了所有这些,但我一生都无法弄清楚为什么它不能找到所有这些。

private void findWords( TreeSet<String> foundWords, String word, Tile t ){
int i=t.getRow();
int j=t.getCol();

//Make sure the tile isn't visited
if(visited[i][j]) return;

//Set the current tile to visited
visited[i][j]=true;

//Decide what the current word is
if(t.getLetter().equalsIgnoreCase("q")) word=word+"qu";
else word=word+t.getLetter();

//If the string is a word
if(Boggle.dictionary.search(word)==1){
//If the word length is greater than or equal to the prefix length
if(word.length()>=Dictionary.prefixLength){
//If the word has not already been found
if(!foundWords.contains(word)){
//Add the word to the found list
foundWords.add(word);
}
}
}

//Recurse through all neighbor tiles
for(int curRow=0; curRow<=nRows; curRow++){
for(int curCol=0; curCol<=nCols; curCol++){
//Make sure it is not out of bounds
if((i+curRow<nRows)&&(j+curCol<nCols)){
findWords(foundWords, word, board[i + curRow][j + curCol]);
findWords(foundWords, word, board[i - curRow][j - curCol]);

findWords(foundWords, word, board[i + curRow][curCol]);
findWords(foundWords, word, board[i - curRow][curCol]);

findWords(foundWords, word, board[curRow][j + curCol]);
findWords(foundWords, word, board[curRow][j - curCol]);

findWords(foundWords, word, board[i + curRow][j - curCol]);
findWords(foundWords, word, board[i - curRow][j + curCol]);
}
}
}

//Reset the tile to be not visited
visited[i][j] = false;
}

这是有问题的方法;它会递归地在 Boggle 板上查找单词。

有人知道为什么它只找到大约 75% 的单词吗?

如果需要,我可以附加更多代码。

最佳答案

我建议编写一些测试用例 - 我总是发现这样做可以立即找到问题,或者至少允许您使用调试器单步执行代码,并找到现实与您的期望不同的地方。

编辑:另外,你的两个 for 循环看起来很奇怪。您不应该查看每个方向上 -1,0 和 1 的偏移量(并折扣 0,0),而不是 0 -> nRows 吗?看来您只朝一个方向看。

关于java - 我的 Boggle 程序找不到所有有效单词 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12921953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com