This method has two applications:
To display the current state of the puzzle, we need to
know what cells are or are not yet solved. If there
is only one possible character choice at a position,
we can confidently display that character there. If
there are still multiple choices there, we display
the cell using the UNK_CELL character to
show that it remains unsolved.
Also, this method is a convenient place to display
a space where there is no cell, so it returns '
' in that case.
During the cyclic reductionphase, this function returns only letters that are common to all the choices of slots that intersect this cell. This allows us to discard choices that don't have any of these letters.
# - - - W o r d B a n k . c e l l C h o i c e s
def cellChoices ( self, coord ):
'''What characters are possible choices at a given location?
'''
The first order of business is to determine whether there is a cell at this location, and return a space if not.
#-- 1 --
# [ if there is a cell at coord in self.puzzle ->
# cell := the Cell instance at coord
# else -> return a space character ]
cell = self.puzzle.whatCell ( coord )
if cell is None:
return ' '
So that this method can be used to echo the puzzle layout
before its slot choices have been added, if self.__totalChoices is zero, we can simply display
the text of the cell, and we're done.
#-- 2 --
if self.__totalChoices == 0:
return cell.text
However, because this method is also used during the solution process, in that case we'll want to show the solution for cells that have been solved.
For each slot that intersects this cell (there may be
either one or two), there is a set of choices for
the letter that occurs at this position. However,
when there is an intersection, the same letter must be used
from each choice. Hence, the result we want is the
intersection of those sets. The set.__iand__ operator is the method in the set type
that implements the “&”
operator, set intersection.
For the method that finds the characters that are in
choices for a particular slot, see Section 71, “WordBank.slotCharChoices(): What letters
are choices at a given position?”. For the method that
generates all the slots that contain a given cell, see
Section 51, “Cell.genSlots(): Generate the slots
intersecting this cell”.
#-- 3 --
return reduce ( set.__and__,
[ self.slotCharChoices ( slot, coord )
for slot in cell.genSlots() ] )