出现了一个问题,我不得不进行递归搜索。我做过类似的程序(如 wordsearches、mazes、connect4 等),但无法让这个程序工作。
我不能确切地告诉你弹出了什么错误(因为大多数时候这个词出现在网格中,但它坚定地提示我它不是)
该代码随机生成一个表,其中随机大写字母(ASCII 65-90?)被转换为字符。
字典功能(我出于测试目的而注释掉的功能)检查字典中是否存在单词输入(用于拼图游戏)并做出相应的响应。
我觉得我应该更详细地解释一些变量的作用:
pos[][]
是我玩错了的桌子posbackup[][]
是我在搜索过程中找不到单词时的备份(因此原始值不会丢失)dem
是一个随机生成的整数,它与表格的尺寸相关(dem
基本上是 的拼写错误dim
)。
这是代码:
package boggle;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;
//ASCII conversion 65-90; Uppercase A-Z
public class Boggle {
ArrayList<String> s = new ArrayList<>();
char[][] pos ;
int max =90;
int min = 65;
int dem;
char[][] posbackup ;
public boolean Solve(int x, int y,String word, int c) throws IOException{
if(x>=pos.length || x<0 || y>pos[0].length || y<0) return false;
if(pos[x][y]=='+') return false;
if(word.length()==0 || c>word.length())return true;
if(pos[x][y]!= word.charAt(0)) return false;
pos[x][y]='+';
word = word.substring(1, word.length());
if(Solve(--x,y, word,c) == true) return true;
if(Solve(x,--y,word,c) == true) return true;
if(Solve(++x,y,word,c) == true) return true;
if(Solve(x,++y,word,c) == true) return true;
if(Solve(--x,--y, word,c) == true) return true;
if(Solve(++x,--y,word,c) == true) return true;
if(Solve(++x,--y,word,c) == true) return true;
if(Solve(++x,++y,word,c) == true) return true;
pos[x][y] = posbackup[x][y]; //Gives an out of bound error when removed it solves the table but not properly
return false;
}
public void Output() throws IOException, FileNotFoundException,
InterruptedException{
for(int c=0; c<dem; c++){
for(int d=0;d<dem;d++){
System.out.print(pos[c][d]+" ");
}
System.out.println();
}
System.out.println("Word found! Here is the path taken");
WordInput(1);
}
public void Backup(){
for(int c=0; c<dem; c++){
for(int d=0;d<dem;d++){
pos[c][d] = posbackup[c][d];
}
}
}
public void Position(String word) throws IOException, InterruptedException{
System.out.println("Word found in the dictionary!");
boolean match=false;
System.out.println("Working...");
Thread.sleep(1000);
for(int x =0; x<pos.length;x++){
for(int y =0; y<pos[0].length; y++){
if(pos[x][y] == word.charAt(0)){
if(Solve(x,y,word,0)==true)match = true;
if(match== false)Backup();
}
}
}
if(match == true) Output();
else{System.out.println("Word not found, try again"); WordInput(1);}
}
public void WordInput(int c) throws FileNotFoundException, IOException, InterruptedException{
Scanner get = new Scanner(System.in);
System.out.println("Enter a word from the dictionary");
BufferedReader read = new BufferedReader(new FileReader("dictionary.txt"));
String line="";
if(c<=0){
while((line=read.readLine())!=null){
line = line.toUpperCase();
s.add(line);
c++;
}
Collections.sort(s); // tfw it actually worked
}
String word = get.next();
word = word.toUpperCase();
// if(s.contains(word))
{
Position(word);
// will be replaced with a binary search l8tr
}
/* else
{
System.out.println("Word not found in dictionary, please try again.");
WordInput(c);
}*/
}
public void PrintAdd( ) throws IOException, FileNotFoundException,
InterruptedException{
Random r = new Random();
int random;
dem = r.nextInt((20-5)+1)+5;
pos = new char[dem][dem];
posbackup = new char[dem][dem];
for(int c=0;c<dem;c++){
for(int d=0; d<dem;d++){
random = r.nextInt((max-min)+1)+min;
pos[c][d] = ((char)random);
posbackup[c][d] = pos[c][d];
System.out.print(pos[c][d]+" ");
}
System.out.println();
}
WordInput(0);
}
public static void main(String[] args) throws FileNotFoundException,
IOException, InterruptedException {
Boggle b = new Boggle();
b.PrintAdd();
}
}
可能是我选择排序我的方法是非常规的,但这就是我到目前为止所得到的。