Word Search (wordsearch)
CS 162 - Spring 2008

Goals
- to jump into the course with both feet and have fun
- to refresh your Java programming skills
- to use functions extensively to organize a solution
- to use multi-dimensional arrays

Overview
Over the course of three deliveries, you will write a Java program that will play the game of Word Search. The game consists of an N x M puzzle of characters and a list of words. The object is to find each word in the puzzle. The words may appear in any direction (up, down, forward, backward, or any diagonal) but always in a straight line.

Specifications
Input for this program will come from two sources: a file whose name is specified on the command line (or as the first value in the input from the user) and the console (standard input). Information about the word puzzle will be in the file. The words to be found will come from standard input.


Dataflow diagram showing data from user and puzzle file
flowing through the program

  Input from
    File
  10 8
  hbtnroch
  aoehanrk
  moaisgag
  itivpcni
  rauioibn
  gterfied
  lonfknri
  ipumgera
  ptgravyn
  squasham

  Input from
     User
  turkey
  pumpkin
  pie
  thanks
  potato
  ham
  gravy
  cranberry
  indian
  pilgrim
  corn
  squash
  thanks
  stuffing
  hat
  boot
 
     Output
h b t n r o c h 
a o e h a n r k 
m o a i s g a g 
i t i v p c n i 
r a u i o i b n 
g t e r f i e d 
l o n f k n r i 
i p u m g e r a 
p t g r a v y n 
s q u a s h a m 

turkey at 4,2 SE
pumpkin not found
pie at 4,5 NW
pie at 4,5 SW
pie at 4,5 SE
thanks not found
potato at 8,2 N
ham at 1,1 S
ham at 10,6 NW
ham at 10,6 E
gravy at 9,3 E
cranberry at 1,7 S
indian at 4,8 S
pilgrim at 9,1 N
corn at 1,7 W
corn at 4,6 SW
squash at 10,1 E
thanks not found
stuffing at 10,1 NE
hat found at 2,4 SW
boot at 1,2 S
Begin by reading in the size of the puzzle. The information should appear as the number of rows (numRows) followed by the number of columns (numCols). The next numRows strings will each contain the numCols characters for that row of the puzzle. The program should handle up to a 20 by 20 word puzzle and should terminate gracefully if numRows or numCols is greater than 20 or less than 1.

Print the puzzle before reading and looking up the words. Print a blank space after each character to make the output more readable.

After the puzzle has been read and printed successfully, read words from standard input. Your program should print the word followed by the location (row,column coordinate of the first character) followed by the direction used to find the word. The character in the upper left corner is at location 1,1. The direction should be in compass coordinates (N, NE, E, SE, S, SW, W, NW) with N being up and E being right. If a word occurs more than once in the puzzle, you are to print a line for each occurrence. If it doesn't occur at all, you should say so.

Sample input and output for the completed program are provided in the handouts. However, you should test the program extensively with your own data that have varying sizes and characteristics.

 

Overall Hints for Solution
Give puzzle useful edges to aid word search. Declare the puzzle (a two-dimensional array) to be two characters bigger in each dimension to hold the puzzle contents plus a border all around of '=' (a non-letter). Initialize the puzzle to the border character. With the border around the edge, you will not need to use any special tests in the code to determine if you have 'run off the edge'. When you run into the border, the characters will not match and you will stop the search.

Break problem into several smaller problems. The intermediate assignments will guide you here. Design the solution carefully and use several functions. Each function should do one conceptual action. The comment on the function should clearly describe that action.

Suggestions for Last Steps
Skim this now and come back to it when you're ready for the last phase of the assignment. It will make much more sense.

Examine all directions at a location. At the end of the part of this assignment where you find the first character of each word, you write a rudimentary searchAt function. Now, flesh that out by considering all the possible directions.

You move forward character-by-character through a string by adding 1 to an index. To move to the right through a 2-dimensional array, you add 1 to the column index and leave the row index where it is. That is moving east.

To move southwest in a 2-dimensional array, add 1 to the row index and subtract 1 (add -1) to the column index. Draw pictures to see this.

Write two nested for loops with indexes called dR (for deltaRow) and dC (for deltaColumn) as values that change from -1 to 0 to 1. When they both equal 0 don't do anything. But otherwise, call one last function that checks for a word at a location and direction.

Check for word at a location and direction. You must write a function called searchThisWay that takes the puzzle, the word, a location (row,column coordinates), and a direction (the increment in the row,column coordinates to use for looking for the successive characters). It should return a string that is either empty or describes the word, the location, and the direction if the word occurs in the puzzle at that location proceeding in that direction.

The trick here is based on how strings are implemented in C. Save your original word in a variable for safekeeping. Then add a null character to the end of the word. Try these three lines:
   final char NULL = '\0';
   String originalWord = word;
   word = word + NULL;

Now, you can have three changing indexes moving through the word and the two dimensions of puzzle. Have a loop that keeps going until the characters don't match. When the loop ends, if the word index is pointing to the null, you found the whole word and need to return a descriptive string. Otherwise, return an empty string.

Write a function that returns a direction string given dR and dC. There are many ways to write this.