我正在尝试编写一个拼字游戏字谜生成器。
到目前为止,我的代码可以正常工作,但是速度非常慢,并且有错误。一个是它会不止一次地使用字母。例如:输入的字母:“ABCDEFG”。它会产生AB,但也会产生AA,这是不对的。
请帮忙。
public class Scrabble1
{
private String[] dictionary2 = new String[97];
private String[] dictionary3 = new String[978];
private String[] dictionary4 = new String[3904];
private String[] dictionary5 = new String[8635];
private String[] dictionary6 = new String[15225];
private String[] dictionary7 = new String[23097];
public void sampleMethod(String s) throws FileNotFoundException
{
File in2 = new File( "dictionary2.txt" );
File in3 = new File( "dictionary3.txt" );
File in4 = new File( "dictionary4.txt" );
File in5 = new File( "dictionary5.txt" );
File in6 = new File( "dictionary6.txt" );
File in7 = new File( "dictionary7.txt" );
Scanner dict2 = null,dict3 = null,dict4 = null,dict5 = null,dict6 = null,dict7 = null;
try
{
dict2 = new Scanner(in2);
dict3 = new Scanner(in3);
dict4 = new Scanner(in4);
dict5 = new Scanner(in5);
dict6 = new Scanner(in6);
dict7 = new Scanner(in7);
int c = 0;
while(dict2.hasNext()&&dict3.hasNext()&&dict4.hasNext()&&dict5.hasNext()&&dict6.hasNext()&&dict7.hasNext())
{
dictionary2[c] = dict2.next();
dictionary3[c] = dict3.next();
dictionary4[c] = dict4.next();
dictionary5[c] = dict5.next();
dictionary6[c] = dict6.next();
dictionary7[c] = dict7.next();
c++;
}
}
catch( FileNotFoundException e )
{
System.err.println( e.getMessage () );
System.exit(1);
}
finally
{
dict2.close();
dict3.close();
dict4.close();
dict5.close();
dict6.close();
dict7.close();
}
// for(int i= 0; i<80612; i++)
//System.out.println(dicArray[i]);
String temp = "";
//All 2 letter anagrams
for(int k=0; k<=6; k++)
for(int i=0; i<=6; i++)
for(int d= 0; d<97; d++)
{
temp = "" + s.charAt(k) + s.charAt(i);
if(temp.equals(dictionary2[d]))
System.out.println(temp );
}
//All 3 letter anagrams
for(int j = 0; j<=6; j++)
for(int k=0; k<=6; k++)
for(int i=0; i<=6; i++)
for(int d= 0; d<978; d++)
{
temp = "" + s.charAt(j) + s.charAt(k)+ s.charAt(i);
if(temp.equals(dictionary3[d]))
System.out.println(temp );
}
//All 4 letter anagrams
for(int j = 0; j<=6; j++)
for(int k = 0; k<=6; k++)
for(int i=0; i<=6; i++)
for(int l=0; l<=6; l++)
for(int d= 0; d<-3904; d++)
{
temp = "" + s.charAt(j) + s.charAt(k)+ s.charAt(i)+ s.charAt(l);
if(temp.equals(dictionary4[d]))
System.out.println(temp );
}
//All 5 letter anagrams
for(int j = 0; j<=6; j++)
for(int k = 0; k<=6; k++)
for(int i=0; i<=6; i++)
for(int l=0; l<=6; l++)
for(int f=0; f<=6; f++)
for(int d= 0; d<8635; d++)
{
temp = "" + s.charAt(j) + s.charAt(k)+ s.charAt(i)+ s.charAt(l)+s.charAt(f);
if(temp.equals(dictionary5[d]))
System.out.println(temp );
}
//All 6 letter anagrams
for(int j = 0; j<=6; j++)
for(int k = 0; k<=6; k++)
for(int i=0; i<=6; i++)
for(int l=0; l<=6; l++)
for(int f=0; f<=6; f++)
for(int g=0; g<=6; g++)
for(int d= 0; d<15225; d++)
{
temp = "" + s.charAt(j) + s.charAt(k)+ s.charAt(i)+ s.charAt(l)+ s.charAt(f)+ s.charAt(g);
if(temp.equals(dictionary6[d]))
System.out.println(temp );
}
//All 7 letter anagrams.
for(int j = 0; j<=6; j++)
for(int k = 0; k<=6; k++)
for(int i=0; i<=6; i++)
for(int l=0; l<=6; l++)
for(int f=0; f<=6; f++)
for(int g=0; g<=6; g++)
for(int p=0; p<=6; p++)
for(int d= 0; d<23097; d++)
{
temp = "" + s.charAt(j) + s.charAt(k)+ s.charAt(i)+ s.charAt(l)+ s.charAt(f)+ s.charAt(g)+ s.charAt(p);
if(temp.equals(dictionary7[d]))
System.out.println(temp );
}
}
}
字典文件只是按字长排序。