为Boggle搜索编程

时间:2017-05-03 19:36:55

标签: java boggle

出现了一个问题,我必须进行递归的boggle搜索。我已经完成了类似的程序(如wordsearches,mazes,connect4等),但无法使用这个程序。

我无法确切地告诉你弹出的错误(因为大多数时候这个词存在于网格中,但它坚决提示我它不是)

代码随机生成一个表,其中随机大写字母(ASCII 65-90?)转换为char。

字典功能(我为测试目的而注释掉的功能)会检查字典输入(对于boggle游戏)是否存在于字典中并作出相应的响应。

我觉得我应该更详细地解释一些变量的作用:

  • 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();

    }

}

可能我选择订购我的方法是非常规的,但这是我到目前为止所做的。

0 个答案:

没有答案