예제 #1
0
def test_if_splits():
    # Test if we can split each duplicate hand
    for card in cards:
        temp = Hand()
        temp.add(Card(card))
        temp.add(Card(card))
        assert temp.can_be_split()
예제 #2
0
 def __init__(self, balance, name=None, strategy=no_strategy):
     self.hand = Hand()
     self.starting_balance = balance
     self.balance = balance
     self.name = name
     self.strategy = strategy
     self.score = 0
     self.dealer_card = None
예제 #3
0
 def __init__(self,
              starting_chips=1000,
              number_decks=6,
              percent_to_use=0.8):
     self.player_chips = starting_chips
     self.shoe = Shoe(number_decks=number_decks,
                      percent_to_use=percent_to_use)
     self.player_hand = Hand()
     self.dealer_hand = Hand()
예제 #4
0
 def deal(self, bet):
     if bet > self.player_chips:
         raise Exception(f'Insufficient chips to place bet of {bet} chips.')
     self.player_hand = Hand(bet=bet)
     self.dealer_hand = Hand()
     self.player_hand.add_card(self.shoe.deal_card())
     self.dealer_hand.add_card(self.shoe.deal_card())
     self.player_hand.add_card(self.shoe.deal_card())
     self.dealer_hand.add_card(self.shoe.deal_card())
예제 #5
0
def deal_start_cards(game_deck):
    """
    Deals the starting cards for the game.
    Sets the global HAND list for both dealer and player.
    Dealer's HAND has a hidden card at the start.

    :param game_deck: the current game deck
    :type game_deck: a list of Card objects
    """
    global HAND

    HAND = Hand([game_deck.pop(), game_deck.pop()], hidden=True)

    player.HAND = Hand([game_deck.pop(), game_deck.pop()])
예제 #6
0
def reveal_cards():
    """
    Recreates the dealer's cards so that the first is no longer hidden.
    """
    global HAND

    HAND = Hand(HAND.cards)
예제 #7
0
def test_soft_hand():
    hand = Hand([
        Card('A', 'H'),
        Card('5', 'D'),
    ])
    assert_equal(hand.value, 16)
    assert_true(hand.soft)
    assert_false(hand.blackjack)
예제 #8
0
def test_blackjack():
    hand = Hand([
        Card('A', 'H'),
        Card('Q', 'D'),
    ])
    assert_equal(hand.value, 21)
    assert_true(hand.soft)
    assert_true(hand.blackjack)
예제 #9
0
def test_hard_hand():
    hand = Hand([
        Card('7', 'H'),
        Card('2', 'D'),
        Card('K', 'S'),
    ])
    assert_equal(hand.value, 19)
    assert_false(hand.soft)
    assert_false(hand.blackjack)
예제 #10
0
    def test_hand_manipulation_methods(self):
        p = Player("Ivan")
        self.assertEqual(p.hand, None)

        hand = Hand()
        p.give_cards(hand)
        self.assertEqual(p.hand, hand)

        p.game_over()
        self.assertEqual(p.hand, None)
예제 #11
0
    def test_values_return_possible_hand_values(self):
        h = Hand()
        h.add((10, Deck.CLUBS))
        h.add((Deck.ACE, Deck.CLUBS))

        values = h.values()
        values.sort()

        self.assertEqual(values, [11, 21])
예제 #12
0
    def test_bet_strategy_streak_mixed_streak(self):
        streak_rates = {-2: 2, -1: 1, 0: 1, 1: 2, 2: 3}

        settings = {
            'game_settings': BetStrategyTest.game_settings,
            'strategy_type': BetStrategyType.STREAK,
            'streak_rates': streak_rates
        }

        strategy = BetStrategy(settings)

        strategy.last_hand = Hand()
        strategy.last_hand.result = HandResult.LOSE

        streak_count = 2

        for _ in range(streak_count):
            strategy.last_hand = Hand()
            strategy.last_hand.result = HandResult.WIN

        self.assertEqual(BetStrategyTest.game_settings.min_bet * streak_rates[streak_count], strategy.bet())
예제 #13
0
    def test_hand_string_representation(self):
        h = Hand()

        h.add((2, Deck.CLUBS))
        h.add((Deck.ACE, Deck.CLUBS))

        self.assertEqual(f"A{Deck.CLUBS} 2{Deck.CLUBS}", str(h))
예제 #14
0
    def test_bet_strategy_series_reset_on_lose(self):
        settings = {
            'game_settings': BetStrategyTest.game_settings,
            'strategy_type': BetStrategyType.SERIES,
            'series': [1, 2, 3]
        }

        strategy = BetStrategy(settings)

        strategy.last_hand = Hand()
        strategy.last_hand.result = HandResult.LOSE

        self.assertEqual(BetStrategyTest.game_settings.min_bet, strategy.bet())
예제 #15
0
    def test_bet_strategy_parlay(self):
        settings = {
            'game_settings': BetStrategyTest.game_settings,
            'strategy_type': BetStrategyType.PARLAY
        }

        strategy = BetStrategy(settings)
        self.assertEqual(BetStrategyTest.game_settings.min_bet, strategy.bet())

        strategy.last_hand = Hand()
        strategy.last_hand.result = HandResult.WIN
        strategy.last_hand.winnings = BetStrategyTest.game_settings.min_bet
        self.assertEqual(BetStrategyTest.game_settings.min_bet * 2, strategy.bet())
예제 #16
0
class Player(metaclass=abc.ABCMeta):
    """
    Base class for all players
    """
    default_bet = 1

    def __init__(self, balance, name=None, strategy=no_strategy):
        self.hand = Hand()
        self.starting_balance = balance
        self.balance = balance
        self.name = name
        self.strategy = strategy
        self.score = 0
        self.dealer_card = None

    def __str__(self):
        if self.name:
            return self.name
        return self.__class__.__name__

    @abc.abstractmethod
    def will_hit(self):
        pass

    def on_card_dealt(self, card, is_dealer):
        if is_dealer:
            self.dealer_card = card
        self.score += self.strategy.get_count(card)

    def on_shuffle(self):
        self.score = 0

    def get_bet(self):
        return self.strategy.get_bet(self.score)

    def has_natural(self):
        return self.hand.get_score() == 21

    def pay(self, player, bet):
        logger.info('[BET] {0} gave {1} chips to {2}.'.format(
            self, bet, player))
        self.balance -= bet
        player.balance += bet

    def reset(self):
        self.hand = Hand()

    def reset_balance(self):
        self.balance = self.starting_balance
예제 #17
0
    def test_bet_strategy_martingale(self):
        settings = {
            'game_settings': BetStrategyTest.game_settings,
            'strategy_type': BetStrategyType.MARTINGALE
        }

        strategy = BetStrategy(settings)
        self.assertEqual(BetStrategyTest.game_settings.min_bet, strategy.bet())

        strategy.last_hand = Hand()
        strategy.last_hand.result = HandResult.PUSH
        self.assertEqual(BetStrategyTest.game_settings.min_bet, strategy.bet())

        strategy.last_hand.result = HandResult.LOSE
        self.assertEqual(BetStrategyTest.game_settings.min_bet * 2, strategy.bet())
예제 #18
0
파일: player.py 프로젝트: pstreck/blackjack
    def new_hand(self, bet=None):
        hand_number = len(self.hands) + 1

        if self.bankroll < self.game_settings.min_bet:
            return None

        if not self.dealer:
            if not bet:
                bet = self.bet_strategy.bet()

            self.bankroll -= bet

        hand = Hand(bet=bet, number=hand_number)
        self.hands.append(hand)

        return hand
예제 #19
0
    def test_bet_strategy_series(self):
        settings = {
            'game_settings': BetStrategyTest.game_settings,
            'strategy_type': BetStrategyType.SERIES
        }

        strategy = BetStrategy(settings)
        with self.assertRaises(ValueError):
            strategy.bet()

        settings['series'] = [1, 2, 3]
        strategy = BetStrategy(settings)

        for multiplier in settings['series']:
            self.assertEqual(BetStrategyTest.game_settings.min_bet * multiplier, strategy.bet())

        strategy.last_hand = Hand()
        strategy.last_hand.result = HandResult.WIN

        self.assertEqual(BetStrategyTest.game_settings.min_bet, strategy.bet())
예제 #20
0
    def start(self, deck=None):
        if self.winner != None:
            raise RuntimeError("Game is over")

        enough_money = True
        for p in self.players:
            enough_money = p.has_enough_money(self.stake)
        if not enough_money:
            raise RuntimeError("One of the players does not have enought money to make a stake!")

        for p in self.players:
            p.make_stake(self.stake)
            p.give_cards(Hand())
            self.bank += self.stake

        if deck == None:
            self.deck = Deck()
            self.deck.shuffle()
        else:
            self.deck = deck

        for _ in range(0, 2):
            for p in self.players:
                p.hand.add(self.deck.pop())
예제 #21
0
def test_empty_hand():
    temp = Hand()
    assert temp.list() == "[]"
    assert temp.value() == 0
예제 #22
0
def build_hand(hand_type):
    hand = Hand()
    for card in HANDS[hand_type]['cards']:
        hand.deal(card)

    return hand
예제 #23
0
def play_blackjack(chips=Account('Chris', 500)):
    """
    Play BlackJack
    :param chips: object
    :return:
    """

    # request a name be entered if one isn't present
    while not len(chips.name):
        try:
            named = input('What\'s your name? ')

            if named.isalpha():
                chips.name = named
                break
            else:
                print(
                    f"{named} is not a valid name, please enter a valid name")
                continue

        except:
            print(
                'Sorry something went wrong, please try and input your name again'
            )
            continue

    # Add fund to account if none is present
    if not chips.has_funds():
        chips.add_funds()
    # Add bet if none has been placed
    while not chips.has_bet():
        chips.make_bet()

    # Build deck and shuffle
    deck = Deck()
    deck.shuffle()

    deal_count = 1
    player = Hand()
    dealer = Hand()
    player.cards = []
    dealer.cards = []
    # Start first phase of the dealing
    while deal_count <= 4:
        deal_count += 1
        if deal_count % 2 == 0:
            if deal_count == 4:
                the_fourth_card = deck.deal()
                the_fourth_card.hide_card()
                dealer.add_card(the_fourth_card)
            else:
                dealer.add_card(deck.deal())

        else:
            player.add_card(deck.deal())

    # Second phase of the dealing
    while True:
        # display cards
        clear()
        print(chips.name + ': ')
        print(player)
        print('Dealers: ')
        print(dealer)

        if player.bust():
            break
        if player.next_move():
            player.add_card(deck.deal())
            continue
        else:
            break

    # change view permissions
    for card in dealer.cards:
        card.show_card()

    # Once player finishes turn continue to deal dealers hand
    while True:
        # display cards
        clear()
        print(chips.name + ': ')
        print(player)
        print('Dealers: ')
        print(dealer)

        if dealer.bust():
            break
        if player.bust():
            break
        if dealer.total() > player.total():
            break
        else:
            dealer.add_card(deck.deal())
            continue

    # display results and finalise transaction
    if player.bust():
        clear()
        chips.subtract()
        print(
            f"{chips.name} you lost this one\n\nYour new Balance {chips.balance()}\n"
            f"{chips.name}: {player}"
            f"Dealers: {dealer}"
            f"\nYour Score {player.total()}\nDealers Score {dealer.total()}")

    elif player.total() < dealer.total() and not dealer.bust():
        clear()
        chips.subtract()
        print(
            f"{chips.name} the dealer beat your score\n\nYour new Balance {chips.balance()}\n"
            f"{chips.name}: {player}"
            f"Dealers: {dealer}"
            f"\nYour Score {player.total()}\nDealers Score {dealer.total()}")

    else:
        clear()
        chips.add()
        print(f"{chips.name} you won!\n\nYour new Balance {chips.balance()}\n"
              f"{chips.name}: {player}"
              f"Dealers: {dealer}"
              f"\nYour Score {player.total()}\nDealers Score {dealer.total()}")

    # Do they want to replay
    while True:
        try:
            replay = input('Do you want to play again? ').upper()

            if replay == 'YES':
                play_blackjack()
                break
            elif replay == 'NO':
                print(
                    f'{chips.name} thank you for playing and we look forward to seeing you again'
                )
                break
            else:
                print(
                    'We didn\'t understand your request, a YES or NO is desired'
                )
                continue
        except ValueError:
            print(
                'Invalid Input: We didn\'t recognise your request please try again YES or No'
            )
            continue
예제 #24
0
 def test_add_card(self):
     h = Hand()
     card = (10, Deck.CLUBS)
     h.add(card)
     self.assertEqual(h.cards, [card])
예제 #25
0
    def test_best_value_returns_best_blackjack_hand_value(self):
        h = Hand()

        h.add((2, Deck.CLUBS))
        self.assertEqual(2, h.best_value())

        h.add((Deck.ACE, Deck.CLUBS))
        self.assertEqual(13, h.best_value())

        h.add((Deck.ACE, Deck.HEARTS))
        self.assertEqual(14, h.best_value())
예제 #26
0
class State:
    def __init__(self,
                 starting_chips=1000,
                 number_decks=6,
                 percent_to_use=0.8):
        self.player_chips = starting_chips
        self.shoe = Shoe(number_decks=number_decks,
                         percent_to_use=percent_to_use)
        self.player_hand = Hand()
        self.dealer_hand = Hand()

    def deal(self, bet):
        if bet > self.player_chips:
            raise Exception(f'Insufficient chips to place bet of {bet} chips.')
        self.player_hand = Hand(bet=bet)
        self.dealer_hand = Hand()
        self.player_hand.add_card(self.shoe.deal_card())
        self.dealer_hand.add_card(self.shoe.deal_card())
        self.player_hand.add_card(self.shoe.deal_card())
        self.dealer_hand.add_card(self.shoe.deal_card())

    def inspect_dealers_hand(self):
        return self.dealer_hand.inspect_card()

    def hit(self):
        self.player_hand.add_card(self.shoe.deal_card())

    def dealer_play(self):
        print(f'Dealer shows {self.dealer_hand.get_cards()}')
        while self.dealer_hand.get_value() <= 17:
            # Dealer hits soft 17
            if self.dealer_hand.get_value() == 17 \
                    and len(self.dealer_hand.get_cards()) == 2 \
                    and 'A' in self.dealer_hand.get_cards():
                break
            self.dealer_hand.add_card(self.shoe.deal_card())
        print(
            f'Dealer ends up with {self.dealer_hand.get_cards()}. (Total={self.dealer_hand.get_value()})'
        )

    def play(self, bet):
        if not self.shoe.is_shoe_open():
            self.shoe.reset_shoe()
            print('Shuffling shoe.')
        self.deal(bet)
        dealer_card = self.inspect_dealers_hand()
        action = 'bj' if self.player_hand.get_value() == 21 else None
        while action not in ['stand', 'bust', 'bj']:
            print(f'Dealer is showing: {dealer_card}.')
            player_cards = self.player_hand.get_cards()
            print(
                f'You have: {player_cards}. (Total={self.player_hand.get_value()})'
            )
            action = input('Hit or Stand?')
            if action == 'hit':
                self.hit()
            if self.player_hand.get_value() > 21:
                action = 'bust'
        if action != 'bust':
            self.dealer_play()

        # Check result
        if action == 'bust':
            print('You busted!')
            chip_diff = self.player_hand.bet * -1
        elif action == 'bj':
            print('Blackjack!')
            chip_diff = self.player_hand.bet * 1.5
        else:
            result = self.player_hand.check_hand(self.dealer_hand)
            if result > 0:
                print('You won!')
                chip_diff = self.player_hand.bet
            elif result == 0:
                print('Push.')
                chip_diff = 0
            else:
                print('You lost.')
                chip_diff = self.player_hand.bet * -1

        self.player_chips += chip_diff
        return chip_diff
예제 #27
0
def test_splitting():
    # Test splitting each hand into two separate ones
    for card in cards:
        temp = Hand()
        temp.add(Card(card))
        temp.add(Card(card))

        first = Hand()
        second = Hand()
        first.add(temp.cards.pop())
        second.add(temp.cards.pop())
        assert first.value() == second.value()
        # Make sure split hand is empty
        assert len(temp.cards) == 0
예제 #28
0
def test_for_soft_values():
    temp = Hand()
    temp.add(Card("4"))
    temp.add(Card("A"))
    assert temp.is_soft_value()
    temp.add(Card("A"))
    assert temp.is_soft_value()
    temp.add(Card("4"))
    assert temp.is_soft_value()
    temp.add(Card("5"))
    assert not temp.is_soft_value()
예제 #29
0
def test_ace_calculation():
    temp = Hand()
    temp.add(Card("A"))
    assert temp.has_ace()
    assert temp.value() == 11
    temp.add(Card("A"))
    assert temp.value() == 12
    temp = Hand()
    temp.add(Card("9"))
    temp.add(Card("A"))
    assert temp.value() == 20
    temp.add(Card("A"))
    assert temp.value() == 21
    temp.add(Card("A"))
    assert temp.value() == 12
예제 #30
0
def test_blackjack():
    temp = Hand()
    temp.add(Card("A"))
    temp.add(Card("K"))
    assert temp.is_blackjack()