Ejemplo n.º 1
0
 def checkChooseable(self, coord):
     ret = RetType(RetType.TRUE, "")
     try:
         self.__checkCoord(coord)
         x, y, z = coord
         if z == 1:
             if self.__cells[x] == None:
                 ret.setFalse()
                 ret.setMessage("no card can be selected")
             else:
                 pass
         else:
             if len(self.__table[x]) == 0:
                 ret.setFalse()
                 ret.setMessage(
                     "no card can be selected in an empty column")
             elif y == len(self.__table[x]) - 1:
                 pass
             else:
                 if self.__table[x][y].color == self.__table[x][
                         y + 1].color or not self.__table[x][
                             y].rank == self.__table[x][y + 1].rank + 1:
                     ret.setFalse()
                     ret.setMessage(
                         "only cards with contiguous ranks and alternant colors can be selected once"
                     )
                 else:
                     return self.checkChooseable((x, y + 1, z))
     except AssertionError as err:
         ret.setFalse()
         ret.exitMsg(str(err))
         raise err
     finally:
         return ret
Ejemplo n.º 2
0
 def checkPileUpAble(self, coord):
     ret = RetType(RetType.TRUE, "")
     try:
         self.__checkCoord(coord)
         x, y, z = coord
         if z == 0 and (y < 0 or y < len(self.__table[x]) - 1):
             ret.setFalse()
             ret.setMessage("exactly one card can be piled up once")
         else:
             cd = None
             if z == 0:
                 cd = self.__table[x][y]
             else:
                 cd = self.__cells[x]
             if cd == None:
                 ret.setFalse()
                 ret.setMessage("no card selected")
             else:
                 st = cd.suit.value
                 rk = cd.rank
                 if not self.__finished[st] == rk - 1:
                     ret.setFalse()
                     msg = cd.color + FreeCellCard(rk - 1, st).__str__(
                     ) + colorama.Style.RESET_ALL + " has to be piled up first"
                     ret.setMessage(msg)
     except AssertionError as err:
         ret.setFalse()
         ret.exitMsg(err.__str__())
         raise err
     finally:
         return ret
Ejemplo n.º 3
0
 def checkFreeUpAble(self, coord):
     ret = RetType(RetType.TRUE, "")
     try:
         self.__checkCoord(coord)
         x, y, z = coord
         if z == 1:
             ret.setFalse()
             ret.setMessage("the card has already been freed")
         else:
             if len(self.__table[x]) == 0:
                 ret.setFalse()
                 ret.setMessage("no card can be moved in an empty column")
             elif not y == len(self.__table[x]) - 1:
                 ret.setFalse()
                 ret.setMessage("exactly one card can be freed once")
             else:
                 numEmptycell = self.__cells.count(None)
                 if numEmptycell == 0:
                     ret.setFalse()
                     ret.setMessage("no vacant position left")
     except AssertionError as err:
         ret.setFalse()
         ret.exitMsg(err.__str__())
         raise err
     finally:
         return ret
Ejemplo n.º 4
0
 def checkMoveable(self, toX, coord, checkedChooseablility=False):
     ret = RetType(RetType.TRUE, "")
     try:
         if not checkedChooseablility:
             ret = self.checkChooseable(coord)
         if ret:
             x, y, z = coord
             self.__checkToMoveX(toX)
             cd = None
             if z == 0:
                 cd = self.__table[x][y]
             else:
                 cd = self.__cells[x]
             if cd == None:
                 ret.setFalse()
                 ret.setMessage("no card is selected")
             elif not self.getColumnLength(toX) == 0 and (
                     self.__table[toX][-1].color == cd.color
                     or not self.__table[toX][-1].rank - 1 == cd.rank):
                 ret.setFalse()
                 ret.setMessage(
                     "card (or cards) can be moved either to an empty column or underneath a destination card, in the latter situation, the color of the card (or the top one of the cards) must be different from the destination card, and the rank must be exactly one smaller"
                 )
             else:
                 numEmptyCol = 0
                 for i in range(COLUMN):
                     if len(self.__table[i]) == 0:
                         numEmptyCol += 1
                 if len(self.__table[toX]) == 0:
                     numEmptyCol -= 1
                 numEmptyCell = self.__cells.count(None)
                 numMaxMove = (numEmptyCell + 1) * (numEmptyCol + 1)
                 numToMove = 1  # corresponds to the case of self.__sZ = 1
                 if z == 0:
                     numToMove = len(self.__table[x]) - y
                 if numToMove > numMaxMove:
                     ret.setFalse()
                     ret.setMessage(
                         "at most %d cards can be moved at present" %
                         (numMaxMove))
     except AssertionError as err:
         ret.setFalse()
         ret.exitMsg(err.__str__())
         raise err
     finally:
         return ret