Next / Previous / Contents / Shipman's homepage

19. Puzzle.__findSlots(): Locate all the slots

kkck
# - - -   P u z z l e . _ _ f i n d S l o t s

    def __findSlots ( self ):
        '''Find all horizontal and vertical slots

          [ self.__cellMap contains a set of Cells ->
              self.__slotMap  +:=  entries for the slots in
                  self.__cellMap ]
        '''

The process of finding slots is the same for horizontal and vertical orientations: scan through a sequence of cells, looking for two or more that are contiguous. For that logic, refer to Section 20, “Puzzle.__genSlots(): Detect slots in a row or column”. For the logic that adds a new slot, see Section 21, “Puzzle.__addSlot(): Add a new slot”.

kkck
        #-- 1 --
        for rowx in range(self.size[0]):
            #-- 1 body --
            # [ self  :=  self with any horizontal slots in
            #       row (rowx) added ]
            for start, length in self.__genClumps ( rowx, 0 ):
                #-- 1.1 body --
                # [ (start is the starting coordinate of a slot) and
                #   (length is the length of a slot) ->
                #      self  :=  self with a horizontal slot added in
                #          columns [start:start+length] ]
                self.__addSlot ( 0, Coord(rowx, start), length )

        #-- 2 --
        for colx in range(self.size[1]):
            #-- 2 body --
            # [ self  :=  self with any vertical slots in
            #       column (colx) added ]
            for start, length in self.__genClumps ( colx, 1 ):
                #-- 2.1 body --
                # [ (start is the starting coordinate of a slot) and
                #   (length is the length of a slot) ->
                #      self  :=  self with a vertical slot added in
                #          columns [start:start+length] ]
                self.__addSlot ( 1, Coord(start, colx), length )

Now that we know the complete set of slots, we must define the self.__firstSlot and self.__slotSuccessor attributes that the recursive algorithm needs to walk through the slots.

kkck
        #-- 3 --
        # [ self.__firstSlot  :=  as invariant
        #   self.__slotSuccessor  :=  as invariant ]
        slotList = self.__slotMap.values()
        slotList.sort()
        self.__firstSlot = slotList[0]
        self.__slotSuccessor = {}
        for i in range(len(slotList)-1):
            self.__slotSuccessor[slotList[i]] = slotList[i+1]
        self.__slotSuccessor[slotList[-1]] = None