def double_down(self, card, additionalBet=None): if additionalBet == None: additionalBet = self._bet if not self.can_double(): raise RuleError("Can't double down on this hand: " + str(self)) if additionalBet > self._bet: raise RuleError( 'Double Down bet can not be more than original bet.') if self.isVerbose: print(" doubled down and received: " + str(card)) self._bet += additionalBet self.hit(card) self._isDoubled = True self._isStanding = True
def sameRank(self, other): """Returns True if both cards are the same rank.""" if type(other) != type(self): raise TypeError(f"Can't compare {type(other)} to a Card.") if not self.isShowing and not other.isShowing: raise RuleError('card is not showing, you can not use sameRank()') return self.rank == other.rank
def hit(self, card): if not self.can_hit(): raise RuleError(f"Can't hit on this hand: {self}") if self.isVerbose: print(" hit and received: " + str(card)) self.append(card) self.check_blackjack() self.check_21() self.check_busted()
def split(self): """ Causes the hand to split. Returns one of the cards in the hand so that that card can be used to form a new hand. """ if not self.can_split(): raise RuleError("Can't split this hand: " + str(self)) if self.isVerbose: print(" splitting hand") self._isSplit = True card = self.pop() return card
def parse_rules(rules): parsed = dict() for section in rules: parsed[section] = dict() for o in rules[section]: # print(section, o) value = rules[section][o] if o == "scale_stat": value = value.split(",") elif o == "scale_method": if value not in SCALES: raise RuleError("No such scale method: " + value) elif o == "cap": try: value = int(value) except ValueError: raise RuleError("{0}/{1} not an integer.".format(o, v)) elif o == "scale_amount": if "." in value: try: value = float(value) except ValueError: raise RuleError("{0}/{1} not a number.".format(o, v)) else: try: value = int(value) except ValueError: raise RuleError("{0}/{1} not a number.".format(o, v)) else: print("'{0}' option not handled, passing as string.".format(o)) parsed[section][o] = value return parsed
def apply_rules(self, rules): if self.name not in rules: raise RuleError("No '{}' section in rules.".format(self.name)) r = rules[self.name] if r["scale_method"] == "exp": self.scale_method = self.lvl_scale_exp elif r["scale_method"] == "flat": self.scale_method = self.scale_flat else: print("Missing scaling method for '{}'".format(r["scale_method"])) self.scale_method = self.lvl_scale_exp self.scale_amount = r["scale_amount"] self.scale_stat = r["scale_stat"]
def get_rank(self): """Returns the rank of the card.""" if not self.is_showing(): raise RuleError('card is not showing, you can not use rank()') return self.__rank
def get_name(self): """Returns the name of the card.""" if not self.is_showing(): raise RuleError('card is not showing, you can not use name()') return self.__name
def get_suit(self): """Returns the suit of the card.""" if not self.is_showing(): raise RuleError('card is not showing, you can not use suit()') return self.__suit
def get_soft_value(self): """Returns the soft value of the card.""" if not self.is_showing(): raise RuleError( 'card is not showing, you can not use hard_value()') return self.__softValue
def get_is_ace(self): """Returns True if the card is an ace.""" if not self.is_showing(): raise RuleError('card is not showing, you can not use is_ace()') return self.__isAce
def get_is_facecard(self): """Returns True if the card is a facecard.""" if not self.is_showing(): raise RuleError( 'card is not showing, you can not use is_face_card()') return self.__isFacecard
def same_rank(self, other): """Returns True if both cards are the same rank.""" if not self.is_showing() and not other.is_showing(): raise RuleError('card is not showing, you can not use same_rank()') return self.rank == other.rank
def same_suit(self, other): """Returns True if both cards are the same suit.""" if not self.is_showing() and not other.is_showing(): raise RuleError('card is not showing, you can not use same_suit()') return self.suit == other.suit
def hardValue(self): """Returns the hard value of the card.""" if not self.isShowing: raise RuleError( 'card is not showing, you can not use hard_value()') return self.__hardValue