def dealer_stats_for_given_hand(games: int = 1000000, decks: int = 6) -> tuple: dealer_stats = {} for i in range(1, 11): dealer_stats[str(i)] = { '17': 0, '18': 0, '19': 0, '20': 0, '21': 0, 'F': 0 } deck = BjDeck(decks) for _ in range(games): deck.start_game() start_hand = deck.deal() dealer = [start_hand] d_cards = dealer_hand(dealer, deck) count = count_hand(d_cards) if count > 21: count = 'F' dealer_stats[str(start_hand)][str( count)] = dealer_stats[str(start_hand)][str(count)] + 1. raw_count = pd.DataFrame(dealer_stats) raw_count.index.name = 'end count' raw_count.columns.name = 'start hand' total = raw_count / raw_count.sum(axis=0) return raw_count, total
def rewards(final_hands: list, d_cards: list) -> list: try: ret = [] d_hand = count_hand(d_cards) for hand in final_hands: # player has over 21 or # player has less than the dealer or # dealer has 21 in two cards and the player don't have 21 in two cards if (hand.count > 21 or (hand.count < d_hand and d_hand < 22)) or (d_hand == 21 and len(d_cards) == 2 and len(hand.cards) > 2): ret.append(-hand.bet) hand.reward = -hand.bet # player has blackjack in two cards and dealer don't have 21 in two cards elif hand.count == 21 and len( hand.cards) == 2 and (d_hand != 21 or len(d_cards) > 2): ret.append(hand.bet * 1.5) hand.reward = hand.bet * 1.5 # dealer has more than 21 or the player has more than the dealer elif d_hand > 21 or hand.count > d_hand: ret.append(hand.bet) hand.reward = hand.bet # same hand for dealer and player elif hand.count == d_hand: ret.append(0) hand.reward = 0 else: print(1) return ret except BaseException: print(1)
def __init__(self, cards: list, bet: float, dealer: int, action: str): self.bet = bet self.cards = cards.copy() self.count = count_hand(cards) self.actions = [{ 'hand': self.cards.copy(), 'dealer': dealer, 'action': action }] self.reward = 0
def convert_hand_to_index(cards: list) -> str: ''' possible outcomes: 'Y' = split 'D' = double 'S' = Stand 'H' = Hit ''' if len(cards) == 2: if cards[0] == cards[1]: if cards[0] == 1: index = 'A,A' else: index = f'{cards[0]},{cards[1]}' elif cards[0] == 1: index = f'A,{cards[1]}' elif cards[1] == 1: index = f'A,{cards[0]}' else: count = count_hand(cards) index = str(count) else: count = count_hand(cards) index = str(count) return index
def test_counts(): assert count_hand([1, 1, 1, 1, 10]) == 14 assert count_hand([1, 10, 1, 10]) == 22 assert count_hand([8, 1]) == 19 assert count_hand([1, 10]) == 21 assert count_hand([1, 1]) == 12
def dealer_hand(cards: list, deck: BjDeck) -> list: count = count_hand(cards) while count < 17: cards.append(deck.deal()) count = count_hand(cards) return cards
def hit(self, deck: BjDeck): self.cards.append(deck.deal()) self.count = count_hand(self.cards)