Exemple #1
0
    def test_name(self, name, expected):
        """
        Tests the name parameter of the __init__ method.

        """
        if type(expected) == type and issubclass(expected, Exception):
            with pytest.raises(TypeError):
                Player(
                    name=name,
                    rules=HouseRules(
                        shoe_size=4,
                        bet_limits=[10, 500]
                    ),
                    bankroll=100,
                    min_bet=10
                )

        else:
            p = Player(
                    name=name,
                    rules=HouseRules(
                        shoe_size=4,
                        bet_limits=[10, 500]
                    ),
                    bankroll=100,
                    min_bet=10
                )
            assert p.name == expected
Exemple #2
0
    def test_bankroll(self, bankroll, expected):
        """
        Tests the bankroll parameter of the __init__ method.

        """
        if type(expected) == type and issubclass(expected, Exception):
            with pytest.raises(ValueError):
                Player(
                    name='Player 1',
                    rules=HouseRules(
                        shoe_size=4,
                        bet_limits=[10, 500]
                    ),
                    bankroll=bankroll,
                    min_bet=10
                )

        else:
            p = Player(
                    name='Player 1',
                    rules=HouseRules(
                        shoe_size=4,
                        bet_limits=[10, 500]
                    ),
                    bankroll=bankroll,
                    min_bet=10
                )
            assert p.bankroll == expected
Exemple #3
0
    def test_shoe_size(self, shoe_size, expected):
        """
        Tests the shoe_size parameter of the __init__ method.

        """
        if type(expected) == type and issubclass(expected, Exception):
            with pytest.raises(ValueError):
                HouseRules(shoe_size=shoe_size, bet_limits=[10, 500])

        else:
            r = HouseRules(shoe_size=shoe_size, bet_limits=[10, 500])
            assert r.shoe_size == expected
Exemple #4
0
    def test_bet_limits(self, bet_limits, expected):
        """
        Tests the bet_limits parameter of the __init__ method.

        """
        if type(expected) == type and issubclass(expected, Exception):
            with pytest.raises(tuple([TypeError, ValueError])):
                HouseRules(shoe_size=4, bet_limits=bet_limits)

        else:
            rules = HouseRules(shoe_size=4, bet_limits=bet_limits)
            assert rules.min_bet == expected[0]
            assert rules.max_bet == expected[1]
Exemple #5
0
    def test_bet_count_amount(self, bet_count_amount, expected):
        """
        Tests the bet_count_amount of the __init__ method.

        """
        r = HouseRules(
            shoe_size=4,
            bet_limits=[10, 500]
        )

        if type(expected) == type and issubclass(expected, Exception):
            with pytest.raises(ValueError):
                Player(
                    name='Player 1',
                    rules=r,
                    bankroll=100,
                    min_bet=10,
                    bet_count_amount=bet_count_amount,
                    bet_spread=10,
                    bet_strategy='Spread',
                    count_strategy='Hi-Lo'
                )

        else:
            p = Player(
                    name='Player 1',
                    rules=r,
                    bankroll=100,
                    min_bet=10,
                    bet_count_amount=bet_count_amount,
                    bet_spread=10,
                    bet_strategy='Spread',
                    count_strategy='Hi-Lo'
            )
            assert p.bet_ramp == expected
Exemple #6
0
def setup_cards():
    cards_list = []
    for shoe_size in [4, 6, 8]:
        r = HouseRules(shoe_size=shoe_size, bet_limits=[10, 500])
        cards_list.append(Cards(rules=r))

    return cards_list
Exemple #7
0
def test_dealer_plays_hand(s17, dealer_hand, expected):
    """
    Tests the dealer_plays_hand function when a dealer hits or stands
    on soft 17.

    """
    r = HouseRules(
        shoe_size=4,
        bet_limits=[10, 500],
        s17=s17
    )
    c = Cards(rules=r)
    t = Table()
    p = Player(
        name='Player 1',
        rules=r,
        bankroll=100,
        min_bet=10
    )
    t.add_player(player=p)
    p.stats.create_count_key(count_key=0)

    dealer_hand = dealer_plays_hand(
        rules=r,
        cards=c,
        dealer_hole_card=dealer_hand[1],
        dealer_hand=dealer_hand
    )

    assert dealer_hand == expected
Exemple #8
0
def test_players_play_hands_double_down(double_down, expected):
    """
    Tests the double down option within the players_play_hands function.

    """
    r = HouseRules(
        shoe_size=4,
        bet_limits=[10, 500],
        double_down=double_down
    )
    c = Cards(rules=r)
    t = Table()
    p = Player(
        name='Player 1',
        rules=r,
        bankroll=100,
        min_bet=10
    )
    t.add_player(player=p)
    p.stats.create_count_key(count_key=0)

    p.set_hand()
    p.hit(key=1, new_card=6)
    p.hit(key=1, new_card=4)

    players_play_hands(
        table=t,
        rules=r,
        cards=c,
        dealer_hand=[11, 2],
        dealer_up_card=2
    )

    assert p.get_double_down(key=1) == expected
Exemple #9
0
    def test_min_bet(self, min_bet, expected):
        """
        Tests the min_bet parameter of the __init__ method.

        """
        r = HouseRules(
            shoe_size=4,
            bet_limits=[10, 500]
        )

        if type(expected) == type and issubclass(expected, Exception):
            with pytest.raises(ValueError):
                Player(
                    name='Player 1',
                    rules=r,
                    bankroll=100,
                    min_bet=min_bet
                )

        else:
            p = Player(
                    name='Player 1',
                    rules=r,
                    bankroll=100,
                    min_bet=min_bet
                )
            assert p.min_bet == expected
Exemple #10
0
def setup_counting_strategy():
    r = HouseRules(shoe_size=4, bet_limits=[10, 500])
    c = Cards(rules=r)
    c.burn_card()
    c.add_to_seen_cards(card=1)
    cs = CountingStrategy(rules=r, cards=c)
    return c, cs
Exemple #11
0
    def test_blackjack_payout(self, blackjack_payout, expected):
        """
        Tests the blackjack_payout parameter of the __init__ method.

        """
        if type(expected) == type and issubclass(expected, Exception):
            with pytest.raises(ValueError):
                HouseRules(shoe_size=4,
                           bet_limits=[10, 500],
                           blackjack_payout=blackjack_payout)

        else:
            rules = HouseRules(shoe_size=4,
                               bet_limits=[10, 500],
                               blackjack_payout=blackjack_payout)
            assert rules.blackjack_payout == expected
Exemple #12
0
def setup_table():
    """
    Fixture that sets up a table with a single player.

    """
    r = HouseRules(
        shoe_size=4,
        bet_limits=[10, 500],
        s17=True,
        blackjack_payout=1.5,
        max_hands=4,
        double_down=True,
        split_unlike_tens=True,
        double_after_split=True,
        resplit_aces=False,
        insurance=True,
        late_surrender=True,
        dealer_shows_hole_card=True
    )
    c = Cards(rules=r)
    t = Table()
    p = Player(
        name='Player 1',
        rules=r,
        bankroll=100,
        min_bet=10
    )
    t.add_player(player=p)
    p.set_hand()
    p.stats.create_count_key(count_key=0)
    return c, t, r, p
Exemple #13
0
    def test_hard(self, s17, expected):
        """
        Tests the hard method.

        """
        r = HouseRules(shoe_size=4, bet_limits=[10, 500], s17=s17)
        ps = PlayingStrategy(rules=r, strategy='Basic')
        assert ps.hard() == expected
Exemple #14
0
    def test_max_hands(self, resplit_aces, max_hands, expected):
        """
        Tests the max_hands parameter of the __init__ method.

        """
        if type(expected) == type and issubclass(expected, Exception):
            with pytest.raises(ValueError):
                HouseRules(shoe_size=4,
                           bet_limits=[10, 500],
                           resplit_aces=resplit_aces,
                           max_hands=max_hands)

        else:
            rules = HouseRules(shoe_size=4,
                               bet_limits=[10, 500],
                               resplit_aces=resplit_aces,
                               max_hands=max_hands)
            assert rules.max_hands == expected
Exemple #15
0
def test_splittable(split_unlike_tens, hand, num_hands, expected):
    """
    Tests the splittable function.

    """
    rules = HouseRules(shoe_size=4,
                       bet_limits=[10, 500],
                       split_unlike_tens=split_unlike_tens,
                       max_hands=4)
    assert splittable(rules=rules, hand=hand, num_hands=num_hands) is expected
Exemple #16
0
def setup_player():
    r = HouseRules(
        shoe_size=4,
        bet_limits=[10, 500]
    )
    p = Player(
            name='Player 1',
            rules=r,
            bankroll=100,
            min_bet=10
    )
    return p
Exemple #17
0
    def test_strategy(self, strategy, expected):
        """
        Tests the strategy parameter of the __init__ method.

        """
        r = HouseRules(shoe_size=4, bet_limits=[10, 500])

        if type(expected) == type and issubclass(expected, Exception):
            with pytest.raises(ValueError):
                PlayingStrategy(rules=r, strategy=strategy)

        else:
            ps = PlayingStrategy(rules=r, strategy=strategy)
            assert ps.strategy == strategy
Exemple #18
0
    def test_remaining_decks(self):
        """
        Tests the remaining_decks method.

        """
        for shoe_size in [4, 6, 8]:
            r = HouseRules(shoe_size=shoe_size, bet_limits=[10, 500])
            c = Cards(rules=r)
            # burn within 1 card of changing remaining decks amount (rounded to the nearest integer)
            for i in range(0, 26):  # half a deck of cards
                c.burn_card()
                assert c.remaining_decks() == r.shoe_size
            c.burn_card()
            assert c.remaining_decks() == r.shoe_size - 1
Exemple #19
0
    def test_add_player(self, size_limit, player, expected):
        """
        Tests the add_player method.

        """
        t = Table(size_limit=size_limit)
        r = HouseRules(shoe_size=4, bet_limits=[10, 500])
        p = Player(name='Player 1', rules=r, bankroll=100, min_bet=10)

        assert len(t.players) == 0
        t.add_player(player=p)
        assert len(t.players) == 1

        if type(expected) == type and issubclass(expected, Exception):
            with pytest.raises(tuple([TypeError, ValueError])):
                t.add_player(player=player)
Exemple #20
0
def test_deal_hands():
    """
    Tests the deal_hands function.

    """
    r = HouseRules(
        shoe_size=4,
        bet_limits=[10, 500]
    )
    c = Cards(rules=r)
    t = Table()
    p = [
        Player(
            name='First to act',
            rules=r,
            bankroll=100,
            min_bet=10
        ),
        Player(
            name='Second to act',
            rules=r,
            bankroll=100,
            min_bet=10
        ),
        Player(
            name='Third to act',
            rules=r,
            bankroll=100,
            min_bet=10
        )
    ]

    for player in p:
        t.add_player(player=player)

    dealer_hand = deal_hands(table=t, cards=c)

    for player in p:
        if player.name == 'First to act':
            assert player.get_hand(key=1) == [1, 10]
        elif player.name == 'Second to act':
            assert player.get_hand(key=1) == [13, 9]
        else:
            assert player.get_hand(key=1) == [12, 8]

    assert dealer_hand == [11, 7]
Exemple #21
0
    def test_remove_player(self, player, expected):
        """
        Tests the remove_player method.

        """
        t = Table()
        r = HouseRules(shoe_size=4, bet_limits=[10, 500])
        p = Player(name='Player 1', rules=r, bankroll=100, min_bet=10)
        t.add_player(player=p)
        assert len(t.players) == 1

        if type(expected) == type and issubclass(expected, Exception):
            with pytest.raises(ValueError):
                t.remove_player(player=player)

        else:
            t.remove_player(player=p)
            assert len(t.get_players()) == 0
Exemple #22
0
def test_players_play_hands_split(
        resplit_aces, max_hands, player_cards, fixed_deck, expected_split, expected_hands
):
    """
    Tests the players_play_hands function when a player has the option to
    split or re-split their hand.

    """
    r = HouseRules(
        shoe_size=4,
        bet_limits=[10, 500],
        max_hands=max_hands,
        resplit_aces=resplit_aces
    )
    c = Cards(rules=r)
    t = Table()
    p = Player(
        name='Player 1',
        rules=r,
        bankroll=100,
        min_bet=10
    )
    t.add_player(player=p)
    p.stats.create_count_key(count_key=0)

    if fixed_deck:
        random.seed(27418)  # 4 aces dealt in a row, would be 6 hands if no max hands limit
        c.shuffle()

    p.set_hand()
    p.hit(key=1, new_card=player_cards[0])
    p.hit(key=1, new_card=player_cards[1])

    players_play_hands(
        table=t,
        rules=r,
        cards=c,
        dealer_hand=[11, 4],
        dealer_up_card=4
    )

    for key in p.hands_dict:
        assert p.get_split(key=key) is expected_split
        assert p.get_hand(key=key) == expected_hands[key - 1]
Exemple #23
0
    def test_back_counting_entry_exit(
            self, count_strategy, insurance, back_counting, back_counting_entry_exit, expected
    ):
        """
        Tests the back_counting of the __init__ method.

        """
        r = HouseRules(
            shoe_size=4,
            bet_limits=[10, 500]
        )

        if type(expected) == type and issubclass(expected, Exception):
            with pytest.raises(ValueError):
                Player(
                    name='Player 1',
                    rules=r,
                    bankroll=100,
                    min_bet=10,
                    bet_count_amount=[(1, 10), (2, 50)],
                    bet_spread=10,
                    bet_strategy='Spread',
                    count_strategy=count_strategy,
                    insurance=insurance,
                    back_counting=back_counting,
                    back_counting_entry_exit=back_counting_entry_exit
                )

        else:
            p = Player(
                    name='Player 1',
                    rules=r,
                    bankroll=100,
                    min_bet=10,
                    bet_count_amount=[(1, 10), (2, 50)],
                    bet_spread=10,
                    bet_strategy='Spread',
                    count_strategy=count_strategy,
                    insurance=insurance,
                    back_counting=back_counting,
                    back_counting_entry_exit=back_counting_entry_exit
            )
            assert p.back_counting_entry == expected[0]
            assert p.back_counting_exit == expected[1]
Exemple #24
0
    def test_cut_card_reached(self, penetration, expected):
        """
        Tests the cut_card_reached method.

        """
        for shoe_size in [4, 6, 8]:
            r = HouseRules(shoe_size=shoe_size, bet_limits=[10, 500])
            c = Cards(rules=r)
            if penetration < 0.5 or penetration > 0.9:
                with pytest.raises(ValueError):
                    c.cut_card_reached(penetration=penetration)
            else:
                # burn within 1 card of reaching desired penetration
                for i in range(0, int(r.shoe_size * penetration * 52) - 1):
                    c.burn_card()
                assert c.cut_card_reached(
                    penetration=penetration) is not expected

                # burn enough cards to reach desired penetration
                c.burn_card()
                assert c.cut_card_reached(penetration=penetration) is expected
Exemple #25
0
def test_players_play_hands_insurance(
        insurance, player_count, pre_insurance_count, expected
):
    """
    Tests the insurance option within the players_play_hands function.

    """
    r = HouseRules(
        shoe_size=4,
        bet_limits=[10, 500],
        insurance=insurance
    )
    c = Cards(rules=r)
    t = Table()
    p = Player(
        name='Player 1',
        rules=r,
        bankroll=100,
        min_bet=10,
        insurance=player_count
    )
    t.add_player(player=p)
    p.pre_insurance_count = pre_insurance_count
    p.stats.create_count_key(count_key=0)  # count before betting
    p.stats.create_count_key(count_key=pre_insurance_count)  # count before insurance bet

    p.set_hand()
    p.hit(key=1, new_card=2)
    p.hit(key=1, new_card=2)

    players_play_hands(
        table=t,
        rules=r,
        cards=c,
        dealer_hand=[11, 1],
        dealer_up_card=1
    )

    assert p.get_insurance() is expected
Exemple #26
0
def test_players_play_hands_double_after_split(
        double_after_split, expected_split, expected_hands
):
    """
    Tests the double after splitting option within the players_play_hands function.

    """
    r = HouseRules(
        shoe_size=4,
        bet_limits=[10, 500],
        double_after_split=double_after_split
    )
    c = Cards(rules=r)
    t = Table()
    p = Player(
        name='Player 1',
        rules=r,
        bankroll=100,
        min_bet=10
    )
    t.add_player(player=p)
    p.stats.create_count_key(count_key=0)

    p.set_hand()
    p.hit(key=1, new_card=2)
    p.hit(key=1, new_card=2)

    players_play_hands(
        table=t,
        rules=r,
        cards=c,
        dealer_hand=[11, 2],
        dealer_up_card=2
    )

    for key in p.hands_dict:
        assert p.get_split(key=key) is expected_split
        assert p.get_hand(key=key) == expected_hands[key - 1]
Exemple #27
0
def test_players_play_hands_surrender(
        late_surrender, expected_surrender, expected_hand
):
    """
    Tests the late surrender option within the players_play_hands function.

    """
    r = HouseRules(
        shoe_size=4,
        bet_limits=[10, 500],
        late_surrender=late_surrender
    )
    c = Cards(rules=r)
    t = Table()
    p = Player(
        name='Player 1',
        rules=r,
        bankroll=100,
        min_bet=10
    )
    t.add_player(player=p)
    p.stats.create_count_key(count_key=0)

    p.set_hand()
    p.hit(key=1, new_card=10)
    p.hit(key=1, new_card=6)

    players_play_hands(
        table=t,
        rules=r,
        cards=c,
        dealer_hand=[11, 11],
        dealer_up_card=11
    )

    assert p.get_surrender() is expected_surrender
    assert p.get_hand(key=1) == expected_hand
Exemple #28
0
from house_rules import HouseRules
from player import Player
from play_shoe import PlayShoe

if __name__ == "__main__":

    # set table rules
    r = HouseRules(shoe_size=6,
                   bet_limits=[10, 500],
                   s17=True,
                   blackjack_payout=1.5,
                   max_hands=4,
                   double_down=True,
                   split_unlike_tens=True,
                   double_after_split=True,
                   resplit_aces=False,
                   insurance=True,
                   late_surrender=True,
                   dealer_shows_hole_card=True)

    # players that will be added to table
    p = [
        Player(name='Card Counter',
               rules=r,
               bankroll=12000,
               min_bet=10,
               bet_spread=10,
               bet_count_amount=[(1, 10), (3, 50), (7, 75)],
               play_strategy='Basic',
               bet_strategy='Spread',
               count_strategy='Halves',
Exemple #29
0
class TestTable(object):
    @pytest.mark.parametrize(
        'size_limit, expected',
        [
            (0, ValueError),  # size limit < 1
            (8, ValueError),  # size limit > 7
            (7, 7)
        ])
    def test_size_limit(self, size_limit, expected):
        """
        Tests the size_limit parameter of the __init__ method.

        """
        if type(expected) == type and issubclass(expected, Exception):
            with pytest.raises(ValueError):
                Table(size_limit=size_limit)

        else:
            t = Table(size_limit=size_limit)
            assert t.size_limit == expected

    @pytest.mark.parametrize(
        'size_limit, player, expected',
        [
            # players name exists at table
            (2,
             Player(name='Player 1',
                    rules=HouseRules(shoe_size=4, bet_limits=[10, 500]),
                    bankroll=100,
                    min_bet=10), ValueError),

            # incorrect type
            (2, [
                Player(name='Player 1',
                       rules=HouseRules(shoe_size=4, bet_limits=[10, 500]),
                       bankroll=100,
                       min_bet=10)
            ], TypeError),

            # table at capacity
            (1,
             Player(name='Player 2',
                    rules=HouseRules(shoe_size=4, bet_limits=[10, 500]),
                    bankroll=100,
                    min_bet=10), ValueError)
        ])
    def test_add_player(self, size_limit, player, expected):
        """
        Tests the add_player method.

        """
        t = Table(size_limit=size_limit)
        r = HouseRules(shoe_size=4, bet_limits=[10, 500])
        p = Player(name='Player 1', rules=r, bankroll=100, min_bet=10)

        assert len(t.players) == 0
        t.add_player(player=p)
        assert len(t.players) == 1

        if type(expected) == type and issubclass(expected, Exception):
            with pytest.raises(tuple([TypeError, ValueError])):
                t.add_player(player=player)

    @pytest.mark.parametrize(
        'player, expected',
        [
            # remove player not at table
            (Player(name='Player 2',
                    rules=HouseRules(shoe_size=4, bet_limits=[10, 500]),
                    bankroll=100,
                    min_bet=10), ValueError)
        ])
    def test_remove_player(self, player, expected):
        """
        Tests the remove_player method.

        """
        t = Table()
        r = HouseRules(shoe_size=4, bet_limits=[10, 500])
        p = Player(name='Player 1', rules=r, bankroll=100, min_bet=10)
        t.add_player(player=p)
        assert len(t.players) == 1

        if type(expected) == type and issubclass(expected, Exception):
            with pytest.raises(ValueError):
                t.remove_player(player=player)

        else:
            t.remove_player(player=p)
            assert len(t.get_players()) == 0