用于Boggle搜索的java编程
出现了一个问题,我必须在哪里进行递归boggle搜索。我也做过类似的程序(比如WordSearchs、Maze、connect4等),但这个程序无法运行
我不能确切地告诉您出现了哪些错误(因为大多数时候,这个词出现在网格中,但它坚定地提示我,它不是)
该代码随机生成一个表,其中包含随机大写字母(ASCII 65-90?)转换为字符
字典特性(我为了测试而注释掉的特性)检查字典中是否存在单词输入(用于boggle游戏),并做出相应的响应
我觉得我应该更详细地解释一些变量的作用:
pos[][]
是我玩boggle的表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();
}
}
也许我对方法排序的选择是非常规的,但这就是我目前所得到的
共 (0) 个答案