ITEC 120
HW8: Word Search

Academic Integrity

Be reminded that this homework assignment must be your own work.

Objectives

After successfully completing this assignment you will be able to use a 2D array and design a solution to a complex problem. You'll understand passing parameters to methods, and you'll get experience working with code written by others. You'll use test driven development to incrementally solve a problem.

Assignment

You will write a program that will find words in a word search puzzle such as this one:

i y h t p u f s
v p v a r d m p
u e a l i q m a
q j n l g g s t
d a a i r c a u
k v n r a i p l
s a a o m t s a
b b b g s p a m

Download: WordSquare.java, WordTest.java, WordSrv.java, WordDrv.java and WordPuzzles.txt.

Compile all the .java files, and run WordDrv.java. It doesn't do much now, but it does call WordSquare.create(1), which reads the first word puzzle from the data file and returns it in a 2D array of char.

Next, WordDrv calls WordSquare.wordList(1), which returns the words to search for in an array of Strings.

One of the first things you should do is print out the 2D array of char (as shown above) so you can see what the puzzle looks like (you should write a method to do this). Also print out the array of words to be found (or not found):

[banana, inheritance, gorilla, java, maps, spasm, soup, valid]

You'll write the code to search for the words in the puzzle, in the method called findWords in WordSrv.java. When a word is not found, you should print a message that indicates it wasn't found. When a word is found, you'll put it in a new 2D array of char. A handle to this 2D array of char is the return value of the findWords method. When you create it, fill it with periods: '.' When you find a word, put it in this array at the same position where the word was found in the puzzle array.

So the output for this puzzle would be:

Not found: inheritance
Not found: soup

. . . . . . . .
. . . a . d . .
. . a l i . m .
. j n l . . s .
. a a i . . a .
. v n r . . p .
. a a o . . s .
. . b g s p a m

The 2D puzzle will be square, and the solution array and puzzle arrays should be the same size.

 

The findWords method

This method represents the bulk of your work on this project. This is the method that is tested with the test driver, which will be used to grade your program. The method is in WordSrv.java, and it's header is:

public char[][] findWords(char[][] puzzle, String[] words)

which you can't change, or you'll break the test driver.

This method takes a handle to a puzzle and an array of words to find in the puzzle, and returns the solution array (as shown above, the found words).

If you try to write all your code in this method, it will quickly become unmanageable and confusing. Think about how you can break this problem up into parts. You are STRONGLY encouraged to write well named helper methods that execute logical pieces of this task.

Almost noone can sit down and write this program start to finish before compiling and running it. Try to get a piece of the solution running before coding everything. For example, you could create the solution array, fill it with periods '.' and print it out before even trying to search for a word. (This is assuming you've first written a print method to print a 2D array.)

Once that compiles and runs correctly, you might want to start with finding words horizontally and forward only. Get that to compile and run. Then, perhaps, you could find horizontal backwards words. Get that to run before moving on to vertical words. Eventually, you can work on diagonal words.

 

Windows users:

The default width of your command window makes it very difficult to read the beautiful output of your test driver. You can change your default width. Here's how. You might also want to check out Console.

 

Grading

This assignment is worth 150 points.

The program is due Wednesday night, Dec 9, at 11:30pm.

 

Submit Your Assignment

Submit your WordSrv.java and WordDrv.java to the HW8 dropbox on D2L.