I am writing a program that is a word search solver and was curious if there was a better approach than what I was taking. The program contains a grid of characters that are suppose to be the puzzle followed by a line break and words to be located in the matrix.
My question is that in locating for the beginning and ending characters of each string would it be better to just strong arm it through the puzzle with nested loops for each direction or could this process be streamlined using some sort of straight line detection algorithm?
The Words can be horizontal, vertical or diagonal and the reverse of each.
Really what I am looking to do is plot the beginning and ending points of a char array (The Word) inside the 2d char array (Puzzle)
Example of puzzle
HGAMONIHRA
AOMOKAWONS
NFROLBOBDN
ARFSIHCAGE
LNIEEWONOK
GOLFUNDTHC
KOCATAOHBI
AMRERCGANH
SLGFAMALLC
ALLIGATORX
HORSE
COW
RHINO
JABBERWOCKY
CAT
DOG
ALLIGATOR
CHICKEN
FROG
BANTHA
MOOSE
LLAMA
Assuming I use just a series of loops I am unsure of the best way to do that to check in all the directions. So far I just have it working searching the words horizontally
boolean over = false;
boolean foundit = false;
String word = new String(letters);
for (int i = 0; (i < puzzle.length) && (!over); i++) {
for (int j = 0; (j < puzzle[i].length) && (!over); j++) {
// Use (i,j) as the starting point.
foundit = true;
// Look through each letter in word
for (int k = 0; (k < letters.length) && (foundit); k++) {
if ((j + k >= puzzle[i].length)
|| (letters[k] != puzzle[i][j + k])) {
// It didn't Match
foundit = false;
}
}
// Success if we made it through all the characters
if (foundit) {
System.out.println(word + " found in row=" + i
+ " col=" + j);
over = true;
}
}
}
if (!foundit) {
System.out.println(word + " not found");
}
Any Pointers on finding them vertically, diagonal and reverse?