-
Title:
06-23 Row Plays
-
Description:
-
There's our whole row, and while I'm at it I might as well define a hand.
-
Now my next target, the next bird to cross off the list is to define a function row_plays,
-
which takes a hand and a row in this format and return a set of legal plays from the row.
-
Now, rather than just return legal words, I'm using this notion of a play,
-
where a play is a pair of location within the row and the word that we want to play.
-
You can imagine it's going to take the same general approach that we've used before,
-
start with an empty set, do something to it, and then return the results that we built up.
-
What is it that we want to do? We want to consider each possible allowable prefix,
-
and to that we want to add all the suffixes, keeping the words.
-
Now, prefixes of what? That's the first thing to figure out.
-
What I'm going to do is enumerate the row--enumerate actually just the good bits.
-
The row from the first position to the last position, and that tells me I don't want the borders.
-
I don't want to consider playing on the borders.
-
I just want to consider playing on the interior of the row.
-
Enumerate that starting from position number 1.
-
One would be where the A is.
-
Now I have an index--a number 1, 2, 3--and I have the square, which is going to be a,
-
and then an anchor and then an anchor and so on.
-
Where do I want to consider my rows?
-
We're going to anchor them on an anchor so I can ask a square an instance of an anchor.
-
If it is an anchor, then there's two possibilities.
-
If it's an anchor like this, there's only one allowable prefix.
-
The prefix which is the letters that are already there just to the left of the anchor.
-
We want to just consider that one prefix and then add all the suffixes.
-
If it's an anchor like this one, then there can be many prefixes.
-
We want all possible prefixes that fit into these spots here,
-
consider each one of those, and for each one of those consider adding on the suffixes.
-
What I'm going to do is define a function, legal _prefix,
-
which gives me a description of the legal prefix that can occur at position i within a row.
-
There are two possibilities.
-
I could combine the possibilities into one, but I'm going to have a tuple of two values returned.
-
I'm going to have legal_prefix return the actual prefix as a string
-
if there is one, like in this case, and return the maximum size otherwise.
-
For this anchor here, this would be legal_prefix of one, two, three, four, five, six--
-
that's for legal_prefix when i = 6.
-
The result would be that there are now characters to the left.
-
It'll be the empty string for the first element of the tuples.
-
The maximum size of the prefix that I'm going to allow is two characters.
-
Now, if I asked here--that's index number one, two, three, four, five, six, seven, eight, nine--
-
when i = 9, the result would be that the prefix is BE,
-
and the maximum size is the same as the minimum size. It's the exact size of 2.
-
I define legal_prefix in order to tell me what to do next based on the two types of anchors.
-
Now, I can go back to row plays.
-
I can call legal_prefix, get my results, and say if there is a prefix,
-
then I want to add to the letters already on the board.
-
Otherwise, I have an empty space to the left, and I want to go through all possible prefixes.
-
Here's what we do if there is a prefix already there.
-
Now we can calculate the start of our position.
-
Remember a row play is going to return the starting location of the word.
-
We can figure that out. It's the i position of the anchor minus the length of the prefix.
-
In fact, let me go and change this comment here.
-
I is not very descriptive.
-
Let's just call that start.
-
Now we know what the starting location is for the word.
-
When we find any words we can return that.
-
Then we go ahead and add suffixes.
-
With the suffixes, some of the letters are going to come out of the hand.
-
We're adding suffixes to the prefix that's already there on the board.
-
Starting in the start location, going through the row,
-
accumulating the results into the result set,
-
and then I needed this one more argument.
-
I actually made a mistake and left this out the first time, and it didn't work.
-
We'll see in a bit what that's there for.
-
Now if we have empty space to the left of the anchor,
-
now we've got to go through all the possible prefixes,
-
but we already wrote that function--find_prefixes. That's good.
-
Looks like we're converging. We're not writing that much new stuff.
-
Now, out of all the possible prefixes for the hand, we only want to look at the ones
-
that are less than or equal to the maximum size.
-
If the prefix is too big, it won't fit into the empty spot.
-
It will run into another word, and we don't want to allow that.
-
We can calculate the start position again.
-
Then we do the same thing. We add suffixes. What do we add them to?
-
We'll the prefix that we just found from the hand.
-
Since the prefix came from the hand, the remaining letters left in the hand
-
we have to subtract out those prefix letters.
-
Here we didn't have to subtract them out, because they prefix letters were already on the board.
-
We're adding to the prefix from the start, from the row, results are accumulated,
-
and we have this anchored equals false again.
-
We're almost there. Just two things left to do--add_suffixes and legal_prefix.
-
Add_suffixes we had before, but it's going to be a little bit more complicated now,
-
because we're dealing with the anchors.
-
Legal_prefix is just a matter of looking to the left and see how much space is there.