Пример #1
0
class Dealer():
    ''' Dealer class

    Dealer class, implements the main methods a dealer would use in playing the game.

    Args:
        name (str) : Dealer name, defaults to dealer.
        cash_balance (int) : Dealer's cash balance, defaults to 100
        number_of_decks (int) : Number of decks to be created.
    Attributes:
        name (str) : Dealer name, defaults to dealer.
        cash_balance (int) : Dealer's cash balance, defaults to 100
        number_of_decks (int) : Number of decks to be created.
        deck (Deck) : Deck object
        hand (Hand) : Hand object
    '''

    def __init__(self, name='dealer', cash_balance=100, number_of_decks=1):
        self.name = name
        self.cash_balance = cash_balance
        self.number_of_decks = number_of_decks
        self.deck = Deck(self.number_of_decks)
        self.hand = Hand()

    def show_balance(self):
        '''Show balance method

        Show balance methods prints the dealer's name and cash balance.
        '''
        print(f'The {self.name} has {self.cash_balance} $ in the bank.')

    def show_dealer_card_values(self):
        '''Show player card values

        Show player card values method
        '''
        print('Dealer cards: ')
        print(self.hand)
        print(f'Dealer hand value is {self.hand.get_card_value()}.')

    def match_bet(self, bet_amount):
        if self.cash_balance >= abs(bet_amount):
            self.cash_balance -= abs(bet_amount)
            return abs(bet_amount)
        else:
            print('The bank went bust.')
            sys.exit()

    def is_soft_17(self):
        '''Is soft 17

        Is soft 17 method checks if the dealer's hand is a soft 17 or no.

        Returns:
            (bool) : True if dealer cards value is 17, False if otherwise
        '''
        if (self.hand.get_card_value() < 17) and (len(self.hand) <= 3):
            return True
        else:
            return False

    @card_object_check
    def return_cards_to_deck(self, discard_pot):
        '''Return cards to deck

        Method that returns collected round cards to deck.

        Args:
            discard_card (list) : List of card objects made of the player's and the dealer's respective hands.
        '''
        if type(discard_pot) == type([]):
            self.deck.all_cards.extend(discard_pot)
        else:
            self.deck.all_cards.append(discard_pot)

    def add_to_cash_balance(self, bet_amount):
        '''Add to cash balance

        Add to cash balance method add bet winnings to the Dealer's cash balance.

        Args:
            bet_amount (int) : winning bet amount
        '''
        self.cash_balance += abs(bet_amount)
Пример #2
0
class TestHand(unittest.TestCase):

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

        (
            self.card_1, self.card_2, self.card_3, self.card_4, self.card_5,
            self.card_6, self.card_7
        ) = (
            Card('Diamonds', 'Two'), Card('Spades', 'King'),
            Card('Clubs', 'Ace'), Card('Spades',
                                       'Queen'), 'this is not a card', 10, 9.5
        )

    def tearDown(self):
        del self.hand
        del self.card_1
        del self.card_2
        del self.card_3
        del self.card_4
        del self.card_5
        del self.card_6
        del self.card_7

    def test_hand_add_new_card_adds_one_card(self):
        self.hand.add_card(self.card_1)
        self.assertEqual(
            len(self.hand), 1,
            'Add card is not working properly. It should contain one element.'
        )

    def test_hand_add_new_card_adds_two_or_more_cards(self):
        self.hand.add_card([self.card_1, self.card_2, self.card_3])
        self.assertGreaterEqual(
            len(self.hand), 2,
            'Add card is not working properly. It should contain two or more elements.'
        )

    def test_hand_one_card_added_is_list(self):
        self.hand.add_card(self.card_1)
        self.assertEqual(
            type(self.hand.hand), list,
            'Hand is not composed of list of cards. Check if the add_card method instantiate an empty list and that proper appending methods are implemented.'
        )

    def test_hand_two_or_more_card_hand_is_list(self):
        self.hand.add_card([self.card_1, self.card_2, self.card_3])
        self.assertEqual(
            type(self.hand.hand), list,
            'Hand is not composed of list of cards. Check if the add_card method instantiate an empty list and that proper appending methods are implemented.'
        )

    def test_hand_card_added_is_card_instance(self):
        self.hand.add_card([self.card_1, self.card_2, self.card_3])
        for elem in self.hand.hand:
            self.assertIsInstance(
                elem, Card,
                'Hand does not contain Card objects. Check if you are adding card objects'
            )

    def test_hand_card_added_decorator_type_error_int(self):
        self.assertRaises(TypeError, self.hand.add_card, self.card_5)

    def test_hand_card_added_decorator_type_error_int(self):
        self.assertRaises(TypeError, self.hand.add_card, self.card_6)

    def test_hand_card_added_decorator_type_error_int(self):
        self.assertRaises(TypeError, self.hand.add_card, self.card_7)

    def test_hand_card_added_decorator_type_error_list_mixed_card_object_and_other_type(
        self
    ):
        self.assertRaises(
            TypeError, self.hand.add_card,
            [self.card_4, self.card_5, self.card_6]
        )

    def test_hand_card_added_decorator_type_error_list_other_type(self):
        self.assertRaises(
            TypeError, self.hand.add_card,
            [self.card_5, self.card_6, self.card_7]
        )

    def test_hand_get_card_value_value_less_than_twenty_one(self):
        self.hand.add_card([self.card_1, self.card_2])
        self.assertLess(
            self.hand.get_card_value(), 21,
            'Hand value not less than 21. Get card value method is not calculating the hand value right.'
        )

    def test_hand_get_card_value_has_ace(self):
        self.hand.add_card([self.card_2, self.card_3])
        self.assertEqual(
            self.hand.get_card_value(), 21,
            'Hand value not 21. Get card value method is not calculating right.'
        )

    def test_hand_get_card_value_value_more_than_twenty_one(self):
        self.hand.add_card([self.card_1, self.card_2, self.card_4])
        self.assertGreater(
            self.hand.get_card_value(), 21,
            'Hand value is not greater than 21. Get card calue not calculating hand value right.'
        )