在java中使用2d数组进行单词搜索-通过第二个字母
因此,我的任务是使用2d数组网格创建一个单词搜索程序 到目前为止,我的代码可以在一个网格中找到所有2个字母的单词,但如果长度超过2个字母,代码就会跳过它们。我现在凌晨2点就累得要死了,所以我可能错过了一些明显的东西,但如果不是,帮我解决一下 当前搜索代码:在java中使用2d数组进行单词搜索-通过第二个字母,java,arrays,2d,wordsearch,Java,Arrays,2d,Wordsearch,因此,我的任务是使用2d数组网格创建一个单词搜索程序 到目前为止,我的代码可以在一个网格中找到所有2个字母的单词,但如果长度超过2个字母,代码就会跳过它们。我现在凌晨2点就累得要死了,所以我可能错过了一些明显的东西,但如果不是,帮我解决一下 当前搜索代码: /** * Searches for a word in this LetterGrid using traditional WordSearch * rules. * * @param wor
/**
* Searches for a word in this LetterGrid using traditional WordSearch
* rules.
*
* @param word
* the word to look for
*/
public boolean wordSearch(String word)
{
// Check each letter in grid
for (int row = 0; row < this.noOfRows; row++)
{
for (int col = 0; col < this.rowLength; col++)
{
if (grid[row][col] == word.charAt(0) && word.length() > 1)
{
return gridCheck(row, col, word, 1);
}
else if (grid[row][col] == word.charAt(0))
{
return true;
}
}
}
return false;
}
public boolean gridCheck(int row, int col, String word, int charToFind)
{
if (charToFind == word.length() - 1)
{
return true;
}
else if (charToFind < word.length() - 1)
{
// Where is the letter being checked? -contingency check-
// if letter being checked is not touching any edge [most likely]
if (row > 0 && row < this.noOfRows && col > 0
&& col < this.rowLength)
{
// FOR CODES SEE CHECKPLACES.TXT
// A
if (grid[row - 1][col - 1] == word.charAt(charToFind))
{
gridCheck(row - 1, col - 1, word,
word.charAt(charToFind + 1));
}
// B
else if (grid[row - 1][col] == word.charAt(charToFind))
{
gridCheck(row - 1, col, word, word.charAt(charToFind + 1));
}
// C
else if (grid[row - 1][col + 1] == word.charAt(charToFind))
{
gridCheck(row - 1, col + 1, word,
word.charAt(charToFind + 1));
}
// D
else if (grid[row][col - 1] == word.charAt(charToFind))
{
gridCheck(row, col - 1, word, word.charAt(charToFind + 1));
}
// E
else if (grid[row][col + 1] == word.charAt(charToFind))
{
gridCheck(row, col + 1, word, word.charAt(charToFind + 1));
}
// F
else if (grid[row + 1][col - 1] == word.charAt(charToFind))
{
gridCheck(row + 1, col - 1, word,
word.charAt(charToFind + 1));
}
// G
else if (grid[row + 1][col] == word.charAt(charToFind))
{
gridCheck(row + 1, col, word, word.charAt(charToFind + 1));
}
// H
else if (grid[row + 1][col + 1] == word.charAt(charToFind))
{
gridCheck(row + 1, col + 1, word,
word.charAt(charToFind + 1));
}
}
// Letter is touching top left corner
if (row == 0 && col == 0)
{
// E
if (grid[row][col + 1] == word.charAt(charToFind))
{
gridCheck(row, col + 1, word, word.charAt(charToFind + 1));
}
// H
else if (grid[row + 1][col + 1] == word.charAt(charToFind))
{
gridCheck(row + 1, col + 1, word,
word.charAt(charToFind + 1));
}
// G
else if (grid[row + 1][col] == word.charAt(charToFind))
{
gridCheck(row + 1, col, word, word.charAt(charToFind + 1));
}
}
// Letter is touching top Right corner
if (row == 0 && col == this.rowLength)
{
// D
if (grid[row][col - 1] == word.charAt(charToFind))
{
gridCheck(row, col - 1, word, word.charAt(charToFind + 1));
}
// F
else if (grid[row + 1][col - 1] == word.charAt(charToFind))
{
gridCheck(row + 1, col - 1, word,
word.charAt(charToFind + 1));
}
// G
else if (grid[row + 1][col] == word.charAt(charToFind))
{
gridCheck(row + 1, col, word, word.charAt(charToFind + 1));
}
}
// Letter is Touching bottom Left Corner
if (row == this.noOfRows && col == 0)
{
// B
if (grid[row - 1][col] == word.charAt(charToFind))
{
gridCheck(row - 1, col, word, word.charAt(charToFind + 1));
}
// C
else if (grid[row - 1][col + 1] == word.charAt(charToFind))
{
gridCheck(row - 1, col + 1, word,
word.charAt(charToFind + 1));
}
// E
else if (grid[row][col + 1] == word.charAt(charToFind))
{
gridCheck(row, col + 1, word, word.charAt(charToFind + 1));
}
}
// Letter is touching bottom right corner
if (row == this.noOfRows && col == this.rowLength)
{
// A
if (grid[row - 1][col - 1] == word.charAt(charToFind))
{
gridCheck(row - 1, col - 1, word,
word.charAt(charToFind + 1));
}
// B
else if (grid[row - 1][col] == word.charAt(charToFind))
{
gridCheck(row - 1, col, word, word.charAt(charToFind + 1));
}
// D
if (grid[row][col - 1] == word.charAt(charToFind))
{
gridCheck(row, col - 1, word, word.charAt(charToFind + 1));
}
}
// letter is on top row of grid
if (row == 0 && col > 0 && col < this.rowLength)
{
// D
if (grid[row][col - 1] == word.charAt(charToFind))
{
gridCheck(row, col - 1, word, word.charAt(charToFind + 1));
}
// E
else if (grid[row][col + 1] == word.charAt(charToFind))
{
gridCheck(row, col + 1, word, word.charAt(charToFind + 1));
}
// F
else if (grid[row + 1][col - 1] == word.charAt(charToFind))
{
gridCheck(row + 1, col - 1, word,
word.charAt(charToFind + 1));
}
// G
else if (grid[row + 1][col] == word.charAt(charToFind))
{
gridCheck(row + 1, col, word, word.charAt(charToFind + 1));
}
// H
else if (grid[row + 1][col + 1] == word.charAt(charToFind))
{
gridCheck(row + 1, col + 1, word,
word.charAt(charToFind + 1));
}
}
// Letter is on bottom row of grid
if (row == this.noOfRows && col > 0 && col < this.rowLength)
{
// FOR CODES SEE CHECKPLACES.TXT
// A
if (grid[row - 1][col - 1] == word.charAt(charToFind))
{
gridCheck(row - 1, col - 1, word,
word.charAt(charToFind + 1));
}
// B
else if (grid[row - 1][col] == word.charAt(charToFind))
{
gridCheck(row - 1, col, word, word.charAt(charToFind + 1));
}
// C
else if (grid[row - 1][col + 1] == word.charAt(charToFind))
{
gridCheck(row - 1, col + 1, word,
word.charAt(charToFind + 1));
}
// D
else if (grid[row][col - 1] == word.charAt(charToFind))
{
gridCheck(row, col - 1, word, word.charAt(charToFind + 1));
}
// E
else if (grid[row][col + 1] == word.charAt(charToFind))
{
gridCheck(row, col + 1, word, word.charAt(charToFind + 1));
}
}
// Letter is on Leftmost column of grid
if (col == 0 && row > 0 && row < this.noOfRows)
{
// B
if (grid[row - 1][col] == word.charAt(charToFind))
{
gridCheck(row - 1, col, word, word.charAt(charToFind + 1));
}
// C
else if (grid[row - 1][col + 1] == word.charAt(charToFind))
{
gridCheck(row - 1, col + 1, word,
word.charAt(charToFind + 1));
}
// E
else if (grid[row][col + 1] == word.charAt(charToFind))
{
gridCheck(row, col + 1, word, word.charAt(charToFind + 1));
}
// G
else if (grid[row + 1][col] == word.charAt(charToFind))
{
gridCheck(row + 1, col, word, word.charAt(charToFind + 1));
}
// H
else if (grid[row + 1][col + 1] == word.charAt(charToFind))
{
gridCheck(row + 1, col + 1, word,
word.charAt(charToFind + 1));
}
}
// Letter is on rightmost column of grid
if (col == this.rowLength && row > 0 && row < this.noOfRows)
{
// A
if (grid[row - 1][col - 1] == word.charAt(charToFind))
{
gridCheck(row - 1, col - 1, word,
word.charAt(charToFind + 1));
}
// B
else if (grid[row - 1][col] == word.charAt(charToFind))
{
gridCheck(row - 1, col, word, word.charAt(charToFind + 1));
}
// D
else if (grid[row][col - 1] == word.charAt(charToFind))
{
gridCheck(row, col - 1, word, word.charAt(charToFind + 1));
}
}
}
// If word is not found
return false;
}
谢谢大家的帮助=)
-Apok我看到的第一件事是,我认为您的代码将在下面计算“计算机”一词:
xxxxxxcxxxxxx
xxxxxoxxxxxxx
xxxxxmxxxxxxx
xxxxxxpuxxxxx
xxxxxretxxxxx
其中X是任意字母
我认为您需要实现一些能够在调用之间保持搜索方向的功能
例如,如果您在网格中的某个点找到单词“computer”的第一个字母,然后在西北方向找到“o”,那么您的代码应该继续沿西北方向搜索,直到找到单词中不存在的字符为止(当然,如果它碰到了“墙”)类似的东西可能会起到作用,尽管您需要添加额外的代码以将搜索限制在网格的范围内(这也是C语言,不是java语言,但基本相同)
private Point FindWordInWordSeacrh(字符串字)
{
字符[,]网格=新字符[10,10];
对于(int x=0;x<10;x++)
{
对于(int y=0;y<10;y++)
{
int-iWOrdCharIndex=0;
if(网格[x,y]==word[iWOrdCharIndex])
{
//如果找到单词的第一个字母
iWOrdCharIndex++;
//设置方向向量以继续搜索
对于(int-xDir=-1;xDir
Places to check the X = current letter to check around
Row Col Code A B C
-1 -1 A
-1 0 B D X E
-1 1 C
0 -1 D F G H
0 1 E
1 -1 F
1 0 G
1 1 H
xxxxxxcxxxxxx
xxxxxoxxxxxxx
xxxxxmxxxxxxx
xxxxxxpuxxxxx
xxxxxretxxxxx
private Point FindWordInWordSeacrh(String word)
{
char[,] grid = new char[10, 10];
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
int iWOrdCharIndex = 0;
if (grid[x,y] == word[iWOrdCharIndex])
{
// if the first letter of the word is found
iWOrdCharIndex++;
// Set the direction vector to continue the search
for (int xDir = -1; xDir <= 1; xDir++)
{
for (int yDir = -1; yDir <= 1; yDir++)
{
// Diretion vector set so check for remaining letters of word
for (int iCharPos = 1; iCharPos < word.Length; iCharPos++)
{
// Check the next letters of the word along this direction vector
if (grid[x+xDir, y+yDir] != word[iCharPos])
{
// break loop and chnage direction vector if looking in wrong direction
break;
}
else if (iCharPos == word.Length)
{
// retun the starting point if word found
return new Point(x, y);
}
}
}
}
}
}
}
return null;
}