In the next two exercises, we will build an application.
In particular, we will build our own version of the single-person
word game called TextTwist. If you've never played
this before, you ought to spend 10 minutes trying it.
Here's one
version of the game. You can google "TextTwist" to see
other versions.
As you can see, a scrambled word is presented to the user. The user
must not only guess the scrambled word, but also all "subwords",
valid words that can be spelled using letters from the original word.
We will build a much-simplified version of the interface
for this game (so that our code is not too bulky) over the next
two weeks. I will provide all the GUI code, while you write some
of the word-manipulation methods.
This first week, your job is to identify all the possible
subwords of a given word:
- First, download SubWordFinder.java
in which you will write all your code. You will also need
WordTool.java and
words, the dictionary.
- Next, observe that the code to read in the dictionary is
already provided, as is a method called isDictionaryWord()
that checks whether a given string is a proper word.
- Thus, your job is to generate all possible subwords. You can
do this by generating all possible substrings and checking each
substring to see if you've got a valid dictionary word.
- There is one more requirement that is a little complicated
to understand at first. If you look at the TextTwist game, you notice
that words are organized by length. That is, all the 3-letter
subwords are listed first, then all the 4-letter subwords, then
all the 5-letter subwords etc. We will follow this approach.
- Note: We won't consider words smaller than 3 letters.
- You will use a linked-list to hold all the 3-letter subwords.
Then, use another linked-list for all the 4-letter subwords ... and
so on. Thus, you will use one linked list for each subword-length.
- Consider the original word "house". It turns out that the
sub words are:
Subwords of length 3: hoe, hue, she, sou, use, sue
Subwords of length 4: hose, shoe
Subwords of length 5: house
(Yes, we include as one of the subwords, the original word itself).
Thus, you will generated three linked lists for this example.
- Because we want to store related information together, we will
use an object for this purpose:
class WordInfo {
String originalWord; // Store the full word here. You must do this.
int subWordLength; // What is the length of each subword here. Again, you need to set this correctly.
LinkedList<String> subWords; // The list of subwords of that length.
// This will print out all the subwords into a string.
public String toString ()
{
// ...
}
}
We will use one instance of this class for each subword length.
Thus, for subwords of length 3, one instance of this class will
have the linked list, the original word ... etc.
You are to fill instances of this class correctly in findSubWords().
- Notice that findSubWords() should return an array,
which is captured in the variable subWordInfo in main().
The idea is, subWordInfo[3] is an instance of WordInfo
that has all the information related to subwords of size 3.
Similarly, subWordInfo[4] has all the information related
to subwords of size 4 ... and so on.
- How big is the array? Big enough to allow us to access
subWordInfo[5] for the "house" example (5 letters max).
- Thus, your overall objective is to find the subwords and
put them in the form described above, in an array of instances
of WordInfo (inside of which is each linked list).
- To start with, try to first write out to screen all the valid
subwords of a given length.
- Write a recursive method for generating all possible substrings
of a given length. It will help to review the permutation-seating
example from Module 4. What you will write is a little variation
of that.
- After you've got all the subwords of a given length printing to screen, you
can then put them into a linked-list. It is this linked list
that is placed into the linked-list of the WordInfo
instance.
- Simply repeat this for each possible length (from 3 onwards).
It will help at this stage to draw out the memory picture as
required for the submission below. This memory picture should help
you understand what exactly you need to do to make it all work.
Submission:
- As usual, submit your file using these
submission instructions.
- Consider the variable subWordInfo in main()
just after the method findSubWords() has been called
(and after it has returned). Draw a complete memory picture,
showing what this variable points to, and what else is in memory.
This picture should show where the strings corresponding to
the subwords of "house" are. There's no need to show the dictionary.
You'll need a large sheet of paper nonetheless.
What else is due: