Assignment 3: Word Search Puzzle

We have discussed recursion and in particular backtracking algorithms (such as Eight Queens Problem). In this assignment you will get some practice at recursive programming by writing a backtracking algorithm to find phrases in a puzzle.

Introduction

"Word search" puzzles are common in newspapers, on restaurant placemats, and even in their own collections within books. These vary in format from puzzle to puzzle, but one common format is as follows: A user is presented with a two-dimensional grid of letters and a list of words. The user must find all of the words from the list within the grid, indicating where they are by drawing ovals around them. Words may be formed in any direction: up, down, left or right (we will not allow diagonals even though most puzzles do allow them) but all of the letters in a word must be in a straight line.

This idea can be extended to allow phrases to be embedded in the grids. However, requiring an entire phrase to be in a straight line on the board is not practical, as the dimensions of the board would need to be overly large. Therefore, we will still require the letters in each word to be in a straight line, but we are allowed to change direction between words.

For example, we might be given the following 10x10 grid of letters:

a b s t r a c t i j
a t a d t d a t a j
t b c d c a g h t j
y b c d a t g h y j
p b c d r a g h p j
e b c d t f t h e j
s a r e s f a h s j
a r e d b f e h a j
r b c d a f n h r j
e r e a l l y r e j

and the following phrases:

  1. abstract
  2. abstract data types
  3. abstract data types are really neat
  4. abstract data types are really great

Assume the grid starts in the upper left corner with position (0,0), and that the coordinates are (row, column). Searching would yield:

  1. abstract found in two places:
  2. abstract data types found in two places:
  3. abstract data types are really neat found at:
  4. abstract data types are really great not found

Implementation Details

Your task is to write a Java program to read in a grid of letters from a file, and then interactively allow a user to enter phrases until he or she wants to quit. For each phrase your program must output whether or not the phrase is found and, if found, specifically where it is located.

Input

The grid of letters for your program will be stored in a text file formatted as follows:

For example:

2 3
asd
too

The user input will be phrases of words, with a single space between each word and no punctuation. Each phrase will be entered on a single line. The user may enter either upper or lower case letters, but the string should be converted to lower case before searching the grid. The program will end when the user enters no data for the current phrase (i.e. hits <Enter> without typing any characters beforehand). For example, user input could look like:

That last example is the user just pressing enter.

Output

If a phrase is not found in the grid, the output should simply state that.

If a phrase is found in the grid, your program must find one occurrence of the phrase, and the output must indicate this fact in two ways:

  1. Show the coordinates of each word in the phrase as a pair of (row, column). The first (row, column) must be the starting location for the word and the second must be the ending location.
  2. Show the grid with the letters of the phrase indicated in upper case.

For example, for the 10x10 grid above and the phrase "abstract data types" your output would be:

abstract: (8,4) to (1,4)
data: (1,5) to (1,8)
types: (2,8) to (6,8)

a b s t r a c t i j
a t a d T D A T A j
t b c d C a g h T j
y b c d A t g h Y j
p b c d R a g h P j
e b c d T f t h E j
s a r e S f a h S j
a r e d B f e h a j
r b c d A f n h r j
e r e a l l y r e j

Algorithm

Your search algorithm must be a recursive, backtracking algorithm. Note that you do not need recursion to match the letters within individual words (although you may do this recursively if you prefer). Where the recursion is necessary is when moving from one word to the next, since it is here where you may change direction. To make the program more consistent (and easier to grade), we will have the following requirements for the recursive process:

  1. No letter may appear more than one time in any part of a solution.
  2. When given a choice of directions, the options must be tried in the following order:
    1. right
    2. down
    3. left
    4. up
    Given this ordering and the grid above, the solution for "abstract data" would be [(8,4) to (1,4)], [(1,5) to (1,8)] rather than [(8,4) to (1,4)], [(1,3) to (1,0)], since the "right" direction is tried before the "left" direction.
  3. If the last letter in a word is at location (i, j), the first letter of the next word must be at one of locations (i, j+1), (i+1, j), (i, j-1), or (i-1, j).
  4. The direction chosen to find the first letter of a word is the same direction that must be used for all of the letters of the word. For example, in the 10x10 grid shown above:

Sample Data

The 10x10 grid shown above is available here: sample.txt. Test it with the phrases above; your results should match the results shown in file sample-out.txt.

Extra Credit

There are two opportunities for extra credit. You may do one or both (or neither).

  1. Combine the ideas in FindWord.java and Assignment 3 to allow both the characters in the words and the words themselves to change directions (and recursing on both the letters and the words). If you do this, make a second main program (Assign3B.java) and clearly explain how to run it in your Assignment Information Sheet. Also make up and include one or more data files that test the added functionality of this program.
  2. Find all occurrences of a phrase instead of just the first. If you do this, make another main program (Assign3C.java) and clearly explain how to run it in your Assignment Information Sheet.

Submission and Grading:

Complete the Assignment Information Sheet.

Make sure you submit all of the following in a single .zip file to get full credit. Name your main program Assign3.java.

As with Assignment 1 and 2, the idea for your submission is that your TA can compile and run your programs without any additional files, so be sure to test them thoroughly before submitting them. If you use an IDE for development, make sure your program runs from the command line without the IDE before submitting it. If you cannot get the program working as specified, clearly indicate any changes you made and clearly indicate why, so that the TA can best give you partial credit.

Submit your final zip file to CourseWeb in the Assignment 3 folder.

The grading rubric can be found here: Rubric (doc).

The assignment is due Thursday, October 29 by 11:59 pm. As with all programming assignments, you have unlimited uploads (before the deadline), so you may upload the assignment before the deadline. If you later decide to upload another, you may do so without penalty (as long as it's before the assignment deadline). The last submission uploaded will be the one graded.

If you would like ungraded feedback on a programming assignment, you may send an email to your TA or the instructor and ask for feedback; please send your code as well. If your question is basically "Are there any problems with my program?" or "Can you check my code?" tell us what you've already done to test your program; provide the output from the test runs of your program.

For more advice on submitting your assignment, see the Programming Assignments section of the Tips for Success page.