public class SlowSearch extends SearchAlgorithm {
/**
* Constructor
* @param puzzle puzzle board that will be used by the algorithms
* @param searchWords an array of all the words it's suppose to find
*/
public SlowSearch(Board puzzle, LinkedList<String> searchWords) {
super(puzzle, searchWords);
}
/**
* Search algorithm, finds the first letter of the word it's searching
* for on the board, then passes that coordinate onto dirSearch for
* ordinal direction searching
*/
public void search() {
// iterate through each word
while (nextWord()) {
// iterate through each (x,y) coordinate
boolean notFound = true;
for (int y = 0; notFound && y < height; y++) {
for (int x = 0; notFound && x < width; x++) {
// call dirSearch if we found our letter
if (currentWord.charAt(0) == puzzle.getCharAt(x, y)) {
if (dirSearch(x, y))
notFound = false;
}
}
}
}
}
/**
* Ordinally searches for the word in each direction
*
* @return true if the word has been found
*/
private boolean dirSearch(int x, int y) {
boolean mightBeGood;
for (int dir = 0 ; dir < puzzle.directions.length; dir++) {
mightBeGood = true;
int ny = 0;
int nx = 0;
// search the entire word's length
for (int delta = 1; delta < currentWordLen; delta++) {
if (currentWord.charAt(delta) != puzzle.getCharAt(
(nx = x + puzzle.directions[dir][0] * delta),
(ny = y + puzzle.directions[dir][1] * delta))
)
// word's no longer good
mightBeGood = false;
}
// add if the word was found
if (mightBeGood) {
currentPos.add(new Point(x, y));
currentPos.add(new Point(puzzle.relativeX(nx),
puzzle.relativeY(ny)));
return(true);
}
}
return(false);
}
} //SlowSearch