そこで、2 次元配列グリッドを使用して単語検索プログラムを作成することになりました。
これまでのところ、私のコードはグリッド内の 2 文字の単語をすべて見つけることができますが、長さが 2 文字を超えると、コードはそれらをスキップします。私は現在、午前 2 時に疲れ果てているので、おそらく何かが欠けているのは明らかですが、そうでない場合は助けてください。
現在の検索コード:
/**
* 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;
}
[必要な場合] グリッド オブジェクトのコンストラクタは次のとおりです。
/**
* Constructs a new LetterGrid Object
*
* @param letterGridFile
* the file to use
*/
public LetterGrid(String letterGridFile)
{
try
{
// Init Scanner
Scanner fileIn = new Scanner(new File(letterGridFile));
// Init ArrayList
ArrayList<String> nextLine = new ArrayList<String>(10);
// Read Data
while (fileIn.hasNextLine())
{
nextLine.add(fileIn.nextLine());
noOfRows++;
}
fileIn.close();
rowLength = nextLine.size();
grid = new char[noOfRows][rowLength];
// Add data to grid
for (int rowCount = 0; rowCount < noOfRows; rowCount++)
{
grid[rowCount] = (nextLine.get(rowCount).toCharArray());
}
}
// In case file name is mistyped or nonexistent
catch (IOException exp)
{
System.out.println("Oops, something went wrong.");
System.out.println("--> File Not Found");
}
}
そして最後に、私が検索に使用しているリファレンス:
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
助けてくれてありがとう =)
-アポック