我的博格尔项目赢了';找不到所有有效的单词-Java
所以我正在用java编写一个Boggle程序,我很难让它找到所有可能的单词 它找到了几乎所有的东西,但就我而言,我不明白为什么它不能找到所有的东西我的博格尔项目赢了';找不到所有有效的单词-Java,java,recursion,boggle,Java,Recursion,Boggle,所以我正在用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(visi
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;
}
private void findWords(树集foundWords、字符串词、Tile t){
int i=t.getRow();
int j=t.getCol();
//确保没有人来参观瓷砖
如果(访问[i][j])返回;
//将当前互动程序设置为已访问
访问[i][j]=真实;
//确定当前单词是什么
如果(t.getLetter().equalsIgnoreCase(“q”))word=word+qu;
else word=word+t.getLetter();
//如果字符串是一个单词
if(Boggle.dictionary.search(word)==1){
//如果单词长度大于或等于前缀长度
if(word.length()>=字典.前缀长度){
//如果这个词还没有找到
如果(!foundWords.contains(word)){
//将单词添加到查找到的列表中
foundWords.add(word);
}
}
}
//在所有相邻分片中递归
对于(int curRow=0;curRow,我建议编写一些测试用例-我总是发现这样做要么马上发现问题,要么至少允许您使用调试器逐步检查代码,并找到实际情况与您的期望不同的地方
编辑:另外,你的两个for循环看起来很奇怪。你不应该在每个方向上看-1,0和1的偏移量(并且不考虑0,0),而不是0->nRows?看起来你只是在看一个方向。什么是nRows
?在这种情况下,单元测试是你的朋友,我想.nRows是Boggle.java传入的行数,我没有包括在这里面。如果能说得更清楚的话,我可以发布整个程序,但它是5个类文件和一个字典它在矩阵[[A,N],[T,I]]上工作吗?--在“ANT”、“TAN”、“TIN”和“ANTI”(可能还有“TI”)中的“it”处生成“AN”?你的编辑极大地改变了你的代码。我有几个测试用例;这只是我程序的一小部分。我添加了几个递归调用findWords的用例,我想我得到了每个用例。它确实找到了更多的单词,但仍然不是全部。我只是不认为在一篇文章中放入5个类的代码加上一个字典文件会是一个错误好主意。