Project 2: Word Search

Due: September 26


Write a Java program that reads in a text file like the sample below (wordSearch1.txt). The input file for the program will have the letters in the word search grid, followed by a blank line and then words to find in the puzzle.

The puzzle grid will be square, so you can find out the size of the puzzle by reading the first line of the input file and counting the number of letters on the line. In the example below, there are 10 letters on the first line, so the size of the puzzle grid is 10 by 10.

I recommend that you use at least two classes in your program. The class with the main method of the program should be called Search. The program should get the name of the input file as a command-line parameter, so to use the input file shown below, you would use a command line like this:

java Search wordSearch1.txt

See the notes page for this assignment for some sample code that uses command-line parameters.

In addition to the Search class, you might want to make a class that is used to represent the puzzle. Think in terms of object-oriented design and figure out the best way to divide up the functionality (methods) between the Search class and the puzzle class.

For each word listed after the grid, your program should attempt to find the word in the puzzle. If it finds the word, it should print the word with the row and column of the first letter followed by the row and column of the last letter. Row and column numbers start at 0 as in Java arrays, so in a 10 by 10 puzzle, the largest row or column number will be 9 and the smallest will be 0. Make sure you print coordinates in the right order with the row number first and the column number second.

If your program does not find the word in the puzzle, it should print a message to that effect, as shown in the example below (JABBERWOCKY).

Print the messages for words in the order that the words appear in the input file.

Words can be vertical (top-to-bottom or bottom-to-top), horizontal (left-to-right or right-to-left), or diagonally (four different directions), so there are a total of eight different orientations of words. Although this puzzle has examples of all eight orientations, it's missing some good test cases that you should try with your program.

Your program should work with any size of puzzle up to 100x100, as long as the grid is square (same number of lines as there are letters in the first line), and for any number of search words up to 200. Be sure to define those limits with named constants.

Sample input file

Here is a link to a text file with the contents shown above:   wordSearch1.txt

Here's an image of the puzzle that makes it easier for a person to find the words:

Sample program output

HORSE found at start: 0, 0 end: 4, 4
COW found at start: 3, 6 end: 1, 6
RHINO found at start: 0, 8 end: 0, 4
CAT found at start: 6, 2 end: 6, 4
DOG found at start: 5, 6 end: 7, 6
ALLIGATOR found at start: 9, 0 end: 9, 8
CHICKEN found at start: 8, 9 end: 2, 9
FROG found at start: 8, 3 end: 5, 0
BANTHA found at start: 2, 7 end: 7, 7
MOOSE found at start: 0, 3 end: 4, 3
LLAMA found at start: 8, 8 end: 8, 4

Avoid repeated code

You should avoid using repeated code in your program. In particular, instead of having eight different loops to search for words, with each loop looking for a word in a different direction (top to bottom, left to right, etc.) you should have only one loop. Use the same loop to look for words in all eight directions.

Repeated code is difficult to maintain because it's hard to keep it consistent. It's also harder to test. I will deduct up to ten points for repeated code in this program.


Here are some notes that might be helpful: notes.html

Turn in

Put your source files into a jar or zip file and turn the jar or zip file in on Canvas. Make sure that the class with the main method of your program is named Search. Your files should not be inside a folder in the jar or zip file, and there should not be any files other than source files, class files, and an optional README.txt file. Files and directories that the jar command puts in automatically are okay. Class files will be ignored.


10 Correctly submitted: jar file, Search class, no package
10 Gets input file name from command line parameter
20 Processes input file correctly and searches for all words in input list
40 Finds words in eight directions
10 Prints starting and ending coordinates correctly
10 Prints message for words that are not in the puzzle

-5 Coordinates are printed in the wrong order. The row number should be first and the column number should be second.
-10 The messages for words are not printed in the same order that they occur in the input file.
-10 Repeated code.
-10 No source files submitted.