def __checkToMoveX(self, x=None): ret = RetType(RetType.TRUE, "") if x == None: x = self.__toMoveX assert type(x) == type(0), "illegal column index" if x < 0 or x > COLUMN: ret.setFalse() return ret
def mainLoop(self): os.system('cls') self.__promote = "Ready." self.__show() status = RetType() while status.normalOp(): try: ch = getCh() if ch in self.__keyFuncDict.keys(): status = self.__keyFuncDict[ch](ch) assert type(status) == type(RetType()) except AssertionError as err: status = RetType(RetType.ASSERTION_ERROR, err.__str__()) except IOError as err: status = RetType(RetType.IO_ERROR, err.__str__()) if not status.normalExit(): self.__promote = status self.__show()
def checkSelected(self, z, x, y): ret = RetType(RetType.FALSE, "") if self.__sZ == z and self.__sX == x: if z == 1: ret.setTrue() else: if y >= self.__sY: ret.setTrue() return ret
def __onQ(self, ch): if self.__checkAndExit(): return RetType(RetType.NORMAL_EXIT, "") else: return RetType()
def __onLeftRight(self, ch): self.__moveLR(ch) self.__show() return RetType()
def __onH(self, ch): self.__getHint() self.__show() return RetType()
def __onF5(self, ch): self.__debug() return RetType()
def __onF2(self, ch): self.__restart() return RetType()
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
def __onUpDown(self, ch): self.__moveUD(ch) self.__show() return RetType()
def __onEnter(self, ch): self.__pileUp() self.__show() return RetType()
def __onCtrl_Z(self, ch): self.__recall() self.__show() return RetType()
def __onCtrl_N(self, ch): self.__newGame() return RetType()
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
def __onS(self, ch): # do settings pass return RetType()
def __onF(self, ch): self.__freeUp() self.__show() return RetType()
def __onSpace(self, ch): self.__chooseOrMove() self.__show() return RetType()
def __onF1(self, ch): pass return RetType()
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
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