我做了这个boggle类型的游戏,到目前为止效果很好。这里的方法是搜索 10x10 的网格来查看用户输入的单词是否正确。我遇到的问题是此代码仅适用于从左到右和从上到下出现的单词。现在我不指望做对角线,但我想让它以相反的顺序出现的单词也被系统接受。有人告诉我,您输入的单词必须翻转(反转)才能查看它是否正确匹配(这是有道理的。毕竟,您正在寻找一个反词)。那么我将如何实现这一目标?现在,我不太擅长编码,所以,如果你有时间,请写下我必须使用的代码。谢谢你。
public static boolean wordSearch (char[][] table, String search)
{
int ctr = search.length();
String temp = "";
// LEFT TO RIGHT / X-AXIS
for (int row = 0; row < 10; row++) //Checks each row (x) one by one
{
for (int a = 0; a <= (10 - ctr); a++)
{
StringBuilder s = new StringBuilder(10-ctr);//Does... something
for (int x = a; x <= (a+ctr-1); x++) //Checks every possibility in the row
{
s.append(table[row][x]);
temp = s.toString();
if (temp.equals(search))
{
return true;
}
}
}
}
// TOP TO BOTTOM / Y-AXIS
for (int column = 0; column < 10; column++)
{
for (int b = 0; b <= (10 - ctr); b++)
{
StringBuilder v = new StringBuilder(10-ctr);
for (int y = b; y <= (b+ctr-1); y++)//checks every possibility in grid
{
v.append(table[y][column]);
temp = v.toString();
if (temp.equals(search))
{
return true;
}
}
}
}
return false;//if word isn't in grid it returns as false, which then displays an error message
}