// WordSearch.java // David Kosbie, 15-111/AB, Spring 2007 import java.util.*; /* This code was developed during recitation. It is meant to demonstrate the techniques required to write a Word Search program. It is not polished (and, in fact, is VERY light on comments), and it may well have a bug or two lurking in it. Here is the format of our input file (WordSearchInput.txt): B Q G A R K M O T A C R D O G M R A Q Q Z B J B DOG CAT BARK */ public class WordSearch { private static ArrayList board = new ArrayList(); private static Scanner scanner; public static void readBoard() { String line; while (true) { line = scanner.nextLine(); if (line.equals("")) // line is empty, we're done break; board.add(line.replaceAll(" ","").toUpperCase()); } } public static void printBoard() { int rows = getRows(); for (int row=0; row= rows) || (targetCol < 0) || (targetCol >= cols)) // we're off the board, no match return; char boardChar = board.get(targetRow).charAt(targetCol); char wordChar = word.charAt(offset); if (boardChar != wordChar) // mismatch, so we're done return; } System.out.printf("%s at %d,%d direction %d,%d\n", word, row, col, drow, dcol); } public static void main(String[] args) { createScanner(); readBoard(); printBoard(); processWords(); } }