Please read the remarks about programming problems for this course.


Task

In this assignment you will augment your code from Part 1 to solve a word search puzzle.

Organization

Copy all of your C++ files from the previous assignment into a new directory (folder or project). As before, this program will consist of the four C++ files wspuzzle.cpp, wordsearch.h, wordsearch.cpp, and uniformrandom.h.

You should add one function declaration to wordsearch.h:

LetterMatrix solve(const LetterMatrix& puzzle, const std::vector<std::string>& wordlist);
and then implement it in wordsearch.cpp. This function accepts a word search puzzle, and a list of words. It should return a key with all letters of the puzzle removed that are not part of one or more of the words in the word list.

You can use your code from before to create a puzzle and key and then check your new solve function with the puzzle to see if it matches the key.

Even if you did not successfully complete Part 1, you still can complete this assignment. Recall that that in LetterMatrix.h operator>> is overloaded for the LetterMatrix type. This allows you to easily load a puzzle or a key from a file; for example,

// Read in a puzzle from a file LetterMatrix puz; ifstream fin("testpuzzle1.lm"); if (fin) { fin >> puz; // Read in the whole puzzle (or key) all at once fin.close(); } else cout << "Cannot open file" << endl;
Notice that you do not even need to use a loop; you simply read in the whole puzzle (or key) in one simple input statement. You will find a ready-made puzzle file, key file, and word list file here (testpuzzle1.lm, testkey1.lm, and wordlist.txt—the same puzzle, key, and word list that appeared in the Word Search Part 1 description). With the ability to read stored puzzles from a files, you can follow these steps to check your solve function:

  1. Load in a puzzle.
  2. Load in an associated key.
  3. Load in a word list.
  4. Pass the puzzle and word list to your solve function.
  5. Compare the result of your solve function to the key you previously read from the file. You can use the operator== operator (also overloaded for LetterMatrix) to check your generated key and the actual key for equality.

While very unlikely, it is possible for your generated key to differ from the solution that your solve function returns. This especially is true if the hidden words are short. (It is possible that the randomly filled letters coincidentally make up a word in the word list.) That is acceptable; you will just have to check such a solution by hand.

Submission

Submit your completed wordsearch.cpp C++ source file to http://eclass.e.southern.edu. Be sure your name appears within a comment as the first line in each source file you submit.