Consider the following program:
import java.util.ArrayList;
public class WordSearch {
static char[][] board;
static int board_x, board_y;
static ArrayList<String> search_words;
public static void main(String args[])
{
board = new char[][]{
{ 's', 't', 'a', 'c', 'k' },
{ 'x', 'f', 'l', 'o', 'w' },
{ 'x', 'x', 'x', 'v', 'x' },
{ 'x', 'x', 'x', 'e', 'x' },
{ 'x', 'x', 'x', 'r', 'x' },
};
// You could also get these from board.size, etc
board_x = 5;
board_y = 5;
search_words = new ArrayList<String>();
search_words.add("stack");
search_words.add("over");
search_words.add("flow");
search_words.add("not");
for(String word : search_words){
find(word);
}
}
public static void find(String word)
{
// Search for the word laid out horizontally
for(int r=0; r<board_y; r++){
for(int c=0; c<=(board_x - word.length()); c++){
// The pair (r,c) will always be where we start checking from
boolean match = true;
for(int i=0; i<word.length(); i++){
if(board[r][c + i] != word.charAt(i)){
match = false;
System.out.format(" '%s' not found starting at (%d,%d) -- first failure at %d\n", word, r, c, i);
break;
}
}
if(match){
System.out.format("Found match (horizontal) for '%s' starting at (%d,%d)\n", word, r, c);
}
}
}
}
}
The board is a 2-dimensional char array and the list of words you're searching for is an ArrayList called search_words.
After some simple sample initialization of the board and the search_words list, it iterates through the words in the list, searching for each if it lies horizontally.
This idea could be extended to search vertically or diagonally as well with some tweaks.
The logic here is what you should take away from the sample program, not necessarily the structure. If I were doing this for anything serious, I'd probably have a Board class, probably with a .find(word) method.
Finally, the verbose output is:
Found match (horizontal) for 'stack' starting at (0,0)
'stack' not found starting at (1,0) -- first failure at 0
'stack' not found starting at (2,0) -- first failure at 0
'stack' not found starting at (3,0) -- first failure at 0
'stack' not found starting at (4,0) -- first failure at 0
'over' not found starting at (0,0) -- first failure at 0
'over' not found starting at (0,1) -- first failure at 0
'over' not found starting at (1,0) -- first failure at 0
'over' not found starting at (1,1) -- first failure at 0
'over' not found starting at (2,0) -- first failure at 0
'over' not found starting at (2,1) -- first failure at 0
'over' not found starting at (3,0) -- first failure at 0
'over' not found starting at (3,1) -- first failure at 0
'over' not found starting at (4,0) -- first failure at 0
'over' not found starting at (4,1) -- first failure at 0
'flow' not found starting at (0,0) -- first failure at 0
'flow' not found starting at (0,1) -- first failure at 0
'flow' not found starting at (1,0) -- first failure at 0
Found match (horizontal) for 'flow' starting at (1,1)
'flow' not found starting at (2,0) -- first failure at 0
'flow' not found starting at (2,1) -- first failure at 0
'flow' not found starting at (3,0) -- first failure at 0
'flow' not found starting at (3,1) -- first failure at 0
'flow' not found starting at (4,0) -- first failure at 0
'flow' not found starting at (4,1) -- first failure at 0
'not' not found starting at (0,0) -- first failure at 0
'not' not found starting at (0,1) -- first failure at 0
'not' not found starting at (0,2) -- first failure at 0
'not' not found starting at (1,0) -- first failure at 0
'not' not found starting at (1,1) -- first failure at 0
'not' not found starting at (1,2) -- first failure at 0
'not' not found starting at (2,0) -- first failure at 0
'not' not found starting at (2,1) -- first failure at 0
'not' not found starting at (2,2) -- first failure at 0
'not' not found starting at (3,0) -- first failure at 0
'not' not found starting at (3,1) -- first failure at 0
'not' not found starting at (3,2) -- first failure at 0
'not' not found starting at (4,0) -- first failure at 0
'not' not found starting at (4,1) -- first failure at 0
'not' not found starting at (4,2) -- first failure at 0