예제 #1
0
 def blackjackPayoutSet(self, v):
     if not 'n' in v or not 'd' in v:
         raise BJException(
             "The blackjack payout must be a ratio written as #:#.")
     if v['d'] == 0:
         raise BJException("Hitting Blackjack must not end the universe.")
     self.blackjackPayout = v
예제 #2
0
 def numDecksSet(self, v):
     if v < 1:
         raise BJException("The number of decks must be positive.")
     if v > 100:
         raise BJException(
             "The number of decks must not be greater than 100.")
     self.numDecks = v
def closestBelow(list, v):
	if len(list) == 0:
		raise BJException("received an empty list")
	if list[0] > v:
		raise BJException("no element that meets criteria")
	for i in range(len(list)):
		if list[i] > v:
			return i-1
	return len(list)-1
예제 #4
0
	def __init__(self, gender, shoe, reshuffleThresh, delay):
		self.gender = gender
		if self.gender != Male and self.gender != Female:
			raise BJException("The dealer's gender must be male or female.")
		self.shoe = shoe
		self.reshuffleThresh = reshuffleThresh
		self.delay = delay
예제 #5
0
def optimalFind(ruleset, cards_mine, card_dealer):
    ideal = None
    total_mine = getBestScore(cards_mine)
    # Find the column in all matrices based on the dealer's card.
    if card_dealer.rank == Card.R_Ac:
        col = 9
    elif card_dealer.rank >= Card.R_10:
        col = 8
    else:
        col = card_dealer.rank - 2

    if len(cards_mine) == 2 and cards_mine[0].rank == cards_mine[1].rank:
        rank = cards_mine[0].rank
        if rank == Card.R_Ac:
            row = 0
        elif rank >= Card.R_10:
            row = 1
        else:
            row = 10 - rank + 1
        ideal = pairs[row][col]
    else:
        complement = getAceComplement(cards_mine)
        if complement is None:
            # there is no Ace in this hand
            row = 20 - total_mine
            ideal = hardTotals[row][col]
        else:
            row = 9 - complement.rank
            ideal = softTotals[row][col]

    # Do we need to make an adjustment?
    if ruleset.hitSoft17:
        complement = getAceComplement(cards_mine)
        if total_mine == 11 and card_dealer == Card.R_Ac:
            ideal = D
        elif complement == Card.R_7 and card_dealer == Card.R_2:
            ideal = D
        elif complement == Card.R_8 and card_dealer == Card.R_6:
            ideal = D

    if ideal == U and not ruleset.allowSurrender:
        ideal = H
    elif ideal == Dh and not ruleset.allowDouble:
        ideal = H
    elif ideal == Ds and not ruleset.allowDouble:
        ideal = S

    if ideal == H:
        return bbj.BJPlayerAction.Hit
    elif ideal == S:
        return bbj.BJPlayerAction.Stand
    elif ideal == Dh or ideal == Ds:
        return bbj.BJPlayerAction.Double
    elif ideal == P:
        return bbj.BJPlayerAction.Split
    elif ideal == U:
        return bbj.BJPlayerAction.Surrender
    raise BJException("Could not find a proper action to return!")
예제 #6
0
	def __init__(self, strategyName=None):
		self.strategy = None
		if strategyName is not None:
			for s in strategylist:
				if s[0] == strategyName:
					self.strategy = s
			if self.strategy is None:
				raise BJException("The requested card-counting strategy is unrecognized.")
		self.reset()
예제 #7
0
 def __init__(self):
     Deck.__init__(self)
     numSuitsNow = 0
     for suit in itersuit():
         for rank in iterrank():
             self.cards.append(Card(suit, rank))
         numSuitsNow += 1
     if numSuits != numSuitsNow:
         raise BJException(
             "Blackjack requires %d suits; instead we have %d." %
             (numSuits, numSuitsNow))
     self.shuffle()
예제 #8
0
def getGameSettings(args):
    settings = {}
    settings['ruleset'] = BJRuleset(
        4,  # number of decks
        10,  # table minimum
        100,  # table maximum
        50,  # reshuffle when <= cards remain
        {
            'n': 3,
            'd': 2
        },  # Blackjack payout
        True,  # allow double
        True,  # allow surrender
        False,  # allow double after split
        False,  # allow hit on split aces
        False,  # hit soft 17
        False,  # five card charlie
    )
    settings['dealergender'] = Male if random.randint(0, 1) == 1 else Female
    settings['coplayright'] = 1
    settings['coplayleft'] = 1
    settings['dealerdelay'] = 2
    settings['initbalance'] = 500
    settings['benevolent'] = False
    settings['cardcount'] = BJCardCount()
    settings['cardcount-quiz'] = 0
    settings['cardcount-show'] = False
    try:
        opts, args = getopt.getopt(args, '', [
            'help',
            'version',
            'dealergender=',
            'numdecks=',
            'coplayright=',
            'coplayleft=',
            'dealerdelay=',
            'tablemin=',
            'tablemax=',
            'shufflethresh=',
            'bjpayout=',
            'initbalance=',
            'countstrategy=',
            'countquiz=',
            'coloring=',
            'benevolent',
            'disallow-double',
            'disallow-surrender',
            'allow-das',
            'allow-hitsa',
            'soft17hit',
            'enable-5cc',
            'show-cardcount',
        ])
    except getopt.GetoptError as e:
        print(e)
        return None

    for o, a in opts:
        if o == '--help':
            usage(sys.argv[0])
            return None
        if o == '--version':
            print("%s" % bbj.metadata.versionstr)
            return None
        elif o == '--dealergender':
            if a == 'm' or a == 'f':
                settings['dealergender'] = Male if a == 'm' else Female
            else:
                raise BJException(
                    "The dealer gender must be 'm' for male or 'f' for female."
                )
        elif o == '--numdecks':
            try:
                v = int(a)
            except ValueError:
                raise BJException("The number of decks must be an integer.")
            settings['ruleset'].numDecksSet(v)
        elif o == '--coplayleft' or o == '--coplayright':
            try:
                v = int(a)
            except ValueError:
                raise BJException(
                    "The number of coplayers must be an integer.")
            if v < 0:
                raise BJException(
                    "The number of coplayers must be non-negative.")
            if o == '--coplayleft':
                settings['coplayleft'] = v
            else:
                settings['coplayright'] = v
        elif o == '--dealerdelay':
            try:
                v = int(a)
            except ValueError:
                raise BJException("The dealer delay must be an integer.")
            if v < 0:
                raise BJException("The dealer delay must be non-negative.")
            settings['dealerdelay'] = v
        elif o == '--tablemin':
            try:
                v = int(a)
            except ValueError:
                raise BJException("The table minimum must be an integer.")
            settings['ruleset'].tableMinSet(v)
        elif o == '--tablemax':
            try:
                v = int(a)
            except ValueError:
                raise BJException("The table maximum must be an integer.")
            settings['ruleset'].tableMaxSet(v)
        elif o == '--shufflethresh':
            try:
                v = int(a)
            except ValueError:
                raise BJException("The shuffle threshold must be an integer.")
            settings['ruleset'].reshuffleThresholdSet(v)
        elif o == '--bjpayout':
            colon = a.find(':')
            if colon is None:
                raise BJException(
                    "The Blackjack payout is malformed; must be '#:#'.")
            try:
                numer = int(a[:colon])
                denom = int(a[colon + 1:])
            except ValueError:
                raise BJException(
                    "The Blackjack payout is malformed; must be '#:#'.")
            settings['ruleset'].blackjackPayoutSet({'n': numer, 'd': denom})
        elif o == '--initbalance':
            try:
                v = int(a)
            except ValueError:
                raise BJException("The initial balance must be an integer.")
            if v <= 0:
                raise BJException("The initial balance must be positive.")
            settings['initbalance'] = v
        elif o == '--countstrategy':
            if not isStrategyKnown(a):
                raise BJException(
                    "The name of the card-counting strategy is unrecognized.")
            settings['cardcount'] = BJCardCount(a)
        elif o == '--countquiz':
            try:
                v = int(a)
            except ValueError:
                raise BJException(
                    "The card-count quiz interval must be an integer.")
            if v < 0:
                raise BJException(
                    "The card-count quiz interval must be non-negative.")
            settings['cardcount-quiz'] = v
            settings['cardcount-quizObj'] = BJCardCountQuiz()
        elif o == '--coloring':
            if not coloringSupported():
                raise BJException(
                    "Terminal colors are not supported in your environment.")
            try:
                coloringSet(a)
            except PeafowltermError as e:
                raise BJException("Coloring setting rejected: %s" % e)
        elif o == '--benevolent':
            settings['benevolent'] = True
        elif o == '--disallow-double':
            settings['ruleset'].allowDoubleSet(False)
        elif o == '--disallow-surrender':
            settings['ruleset'].allowSurrenderSet(False)
        elif o == '--allow-das':
            settings['ruleset'].allowDASSet(True)
        elif o == '--allow-hitsa':
            settings['ruleset'].allowHitSplitAcesSet(True)
        elif o == '--soft17hit':
            settings['ruleset'].hitSoft17Set(True)
        elif o == '--enable-5cc':
            settings['ruleset'].fiveCardCharlieSet(True)
        elif o == '--show-cardcount':
            settings['cardcount-show'] = True

    # Check for consistency
    if settings['ruleset'].tableMin > settings['ruleset'].tableMax:
        raise BJException(
            "The table minimum must not be greater than the table maximum.")
    if settings['cardcount-show'] and not settings['cardcount'].isEnabled():
        raise BJException(
            "To show card count, a card-counting strategy must be selected.")
    if settings['cardcount-quiz'] and not settings['cardcount'].isEnabled():
        raise BJException(
            "To quiz about the card count, a card-counting strategy must be selected."
        )
    if settings['coplayleft'] + settings['coplayright'] > 6:
        raise BJException(
            "The maximum number of players, including yourself, at a table is 7."
        )

    return settings
예제 #9
0
 def tableMaxSet(self, v):
     if v < 0:
         raise BJException("The table maximum must be non-negative.")
     self.tableMax = v
예제 #10
0
 def tableMinSet(self, v):
     if v < 0:
         raise BJException("The table minimum must be non-negative.")
     self.tableMin = v