/** * Word Search * Input is a puzzle in a file and words to find in the puzzle * The puzzle file name can be provided on the command line or * as the first input from the user. * Puzzle file must contain the numberOfRows and numberOfColumns * followed by numberOfRows strings of numberOfColumns characters. * These may be on one line or multiple lines. * For example: * 2 3 * abc * def * Author: Beth Katz and _______ * January 2008 */ import java.io.*; import java.util.*; public class Findfirst { public static final char BORDER = '='; static Scanner console = new Scanner(System.in); public static void main(String[ ] args) { String filename = null; Scanner puzzleFile; try { if (args.length > 0) { // file name is on command line filename = args[0]; } else { // file name is typed by user System.out.println("Enter puzzle file name: "); filename = console.next( ); } puzzleFile = new Scanner(new File(filename)); char[][] puzzle = readPuzzle(puzzleFile); puzzleFile.close( ); // close when no longer needed // System.out.println(puzzle.length + " " + puzzle[0].length); printPuzzle(puzzle); findWords(puzzle); System.out.println(); System.out.println("Game over."); } catch (FileNotFoundException e) { System.out.println("File " + filename + " could not be opened"); } catch (NoSuchElementException e) { System.out.println("No file name obtained from user"); } catch (IllegalStateException e) { // handle many other exceptions here System.out.println(e.getMessage( )); } } //----------------------------------------------------- /** * reads the puzzle from the already opened puzzleFile */ static char[][] readPuzzle(Scanner puzzleFile) { int numRows; int numCols; char[][] puzzle; String exceptionMessage1 = "Puzzle file must contain the numberOfRows and numberOfColumns\n" + "followed by numberOfRows strings of numberOfColumns characters.\n" + "For example: 2 3 abc def"; String exceptionMessage2 = "Rows must be between 1 and 20 (inclusive) but is "; String exceptionMessage3 = "Columns must be between 1 and 20 (inclusive) but is "; try { numRows = puzzleFile.nextInt( ); numCols = puzzleFile.nextInt( ); if (numRows < 1 || numRows > 20) throw new IllegalStateException(exceptionMessage2 + numRows); if (numCols < 1 || numCols > 20) throw new IllegalStateException(exceptionMessage3 + numCols); puzzle = new char[numRows+2][numCols+2]; initPuzzle(puzzle); for (int row = 1; row < puzzle.length-1; row++) { String s = puzzleFile.next(); for (int col = 1; col < puzzle[row].length-1; col++) { puzzle[row][col] = s.charAt(col-1); } } } catch (NoSuchElementException e) { throw new IllegalStateException(exceptionMessage1); } catch (StringIndexOutOfBoundsException e) { throw new IllegalStateException(exceptionMessage1); } return puzzle; } //---------------------------------------- static void printPuzzle (char[][] p) { for (int r = 1; r < p.length-1; r++) { for (int c = 1; c < p[r].length-1; c++) { System.out.print(p[r][c] + " "); } System.out.println(); } System.out.println(); } //---------------------------------------- static void initPuzzle (char[][] p) { for (int r = 0; r < p.length; r++) { for (int c = 0; c < p[r].length; c++) { p[r][c] = BORDER; } } } //---------------------------------------- static void findWords (char[][] p) { String prompt = "Enter a word: "; Scanner input = new Scanner(System.in); System.out.print(prompt); while (input.hasNext()) { String word = input.next(); String s = locationInPuzzle (p, word); // System.out.println(word); if (s.length() == 0) { System.out.println (word + " not found."); } else { System.out.println (s); } System.out.print(prompt); } } //---------------------------------------- static String locationInPuzzle (char[][] p, String word) { String s = ""; for (int r = 1; r < p.length-1; r++) { for (int c = 1; c < p[r].length-1; c++) { s += searchAt(p, word, r, c); } } return s; } //---------------------------------------- static String searchAt (char[][] p, String word, int r, int c) { String s = ""; if (word.charAt(0) == p[r][c]) { s = word + " could start at " + r + "," + c + "\n"; } return s; } }