Пример #1
0
 def instantiate_full_houses(self):
     full_house_index = 167
     for three_card in reversed(ranks):
         for two_card in reversed(ranks):
             if two_card != three_card:
                 hand = Hand([Card(three_card, 's')] * 3 +
                             [Card(two_card, 'd')] * 2)
                 self.prime_table[hand.p_value()] = full_house_index
                 full_house_index += 1
Пример #2
0
 def instantiate_four_of_a_kinds(self):
     four_of_a_kind_index = 11
     for four_card in reversed(ranks):
         for single_card in reversed(ranks):
             if single_card != four_card:
                 hand = Hand([Card(four_card, 's')] * 4 +
                             [Card(single_card, 'd')])
                 self.prime_table[hand.p_value()] = four_of_a_kind_index
                 four_of_a_kind_index += 1
Пример #3
0
 def instantiate_two_pair(self):
     two_pair_index = 2468
     for two_card_1, two_card_2 in combinations(reversed(ranks), 2):
         for single_card in reversed(
             [c for c in ranks if c != two_card_1 and c != two_card_2]):
             hand = Hand([Card(two_card_1, 'd')] * 2 +
                         [Card(two_card_2, 'c')] * 2 +
                         [Card(single_card, 'c')])
             self.prime_table[hand.p_value()] = two_pair_index
             two_pair_index += 1
Пример #4
0
    def setUp(self):
        # get a deck with no cards to test scoring
        self.score_deck = Deck()
        self.score_deck.cards = []

        # get a regular deck
        self.deck = Deck()

        # get a Hand
        self.hand = Hand(self.deck)
Пример #5
0
    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
        )
Пример #6
0
 def instantiate_three_of_a_kind(self):
     three_of_a_kind_index = 1610
     for three_card in reversed(ranks):
         for single_card_1, single_card_2 in combinations(
                 reversed([c for c in ranks if c != three_card]), 2):
             hand = Hand(
                 [Card(three_card, 's')] * 3 +
                 [Card(single_card_1, 'd'),
                  Card(single_card_2, 'd')])
             self.prime_table[hand.p_value()] = three_of_a_kind_index
             three_of_a_kind_index += 1
Пример #7
0
 def instantiate_one_pair(self):
     one_pair_index = 3326
     for two_card in reversed(ranks):
         for single_card_1, single_card_2, single_card_3 in combinations(
                 reversed([c for c in ranks if c != two_card]), 3):
             hand = Hand([Card(two_card, 's')] * 2 + [
                 Card(single_card_1, 'd'),
                 Card(single_card_2, 'd'),
                 Card(single_card_3, 'd')
             ])
             self.prime_table[hand.p_value()] = one_pair_index
             one_pair_index += 1
Пример #8
0
 def instantiate_lookup_table(self, table, straight_offset, normal_offset):
     straight_index = straight_offset
     normal_index = normal_offset
     for c1, c2, c3, c4, c5 in combinations(reversed(ranks), 5):
         hand = Hand([
             Card(c1, 's'),
             Card(c2, 's'),
             Card(c3, 's'),
             Card(c4, 's'),
             Card(c5, 's')
         ])
         if (rank_dict[c1] - rank_dict[c5] == 4):
             table[hand.q_value()] = straight_index
             straight_index += 1
         elif (c1, c2, c3, c4, c5) == ('A', '5', '4', '3', '2'):
             table[hand.q_value()] = straight_offset + 9
         else:
             table[hand.q_value()] = normal_index
             normal_index += 1
Пример #9
0
 def __init__(self, name='player', cash_balance=100):
     self.name = name
     self.cash_balance = cash_balance
     self.maximum_bet = cash_balance - 10
     self.minimum_bet = 10
     self.hand = Hand()
Пример #10
0
 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()
Пример #11
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)
Пример #12
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.'
        )
Пример #13
0
from evaluator import HandEvaluator
from card import Card, Hand

hand_evaluator = HandEvaluator()

cards = [
    Card('7', 's'),
    Card('5', 's'),
    Card('4', 's'),
    Card('3', 's'),
    Card('2', 's')
]

hand_1 = Hand(cards, hand_evaluator)

cards = [
    Card('8', 'd'),
    Card('8', 's'),
    Card('8', 'c'),
    Card('3', 's'),
    Card('3', 'c')
]

hand_2 = Hand(cards, hand_evaluator)

print(hand_2 > hand_1)

cards = [
    Card('6', 's'),
    Card('5', 's'),
    Card('4', 's'),
Пример #14
0
        print('Game exited')
        exit()

    # initialize chips amount
    player = chips()
    print("You have 100 chips")

    while player.amount > 0:
        new_deck = Deck()
        new_deck.shuffle()

        # First turn, place your bets
        bet = player_bet(player.amount)

        # initialize NPC and player Hand
        computer_hand = Hand()
        computer_hand.add_card(new_deck.deal())
        computer_hand.add_card(new_deck.deal())

        player_hand = Hand()
        player_hand.add_card(new_deck.deal())
        player_hand.add_card(new_deck.deal())

        print_hidden_card(player_hand, computer_hand)

        # player turn
        while player_hand.points <= 21:
            chose = input('Hit or stand?\n')
            if chose.lower() == 'hit':
                player_hand.add_card(new_deck.deal())
                player_hand.adjust_for_ace()
Пример #15
0
class TestHand(unittest.TestCase):
    def setUp(self):
        # get a deck with no cards to test scoring
        self.score_deck = Deck()
        self.score_deck.cards = []

        # get a regular deck
        self.deck = Deck()

        # get a Hand
        self.hand = Hand(self.deck)

    def score_hand(self, score_string):
        """Test helper for scoring hands."""
        self.hand.cards = self.score_deck.cards
        score_dict = self.hand.score(self.score_deck)

        # test if score_strings are equivalent
        self.assertEqual(score_dict["score_string"], score_string)
        # make sure the copied hand has 5 cards
        self.assertEqual(len(score_dict["cards"]), 5)

    def test_items_are_constructed(self):
        self.assertIsInstance(self.deck, Deck)
        self.assertIsInstance(self.score_deck, Deck)
        self.assertIsInstance(self.hand, Hand)

        # make sure score deck has no cards
        self.assertEqual(len(self.score_deck.cards), 0)

        # make sure regular deck has all cards. Deck should be short from hand draw
        self.assertEqual(len(self.deck.cards), 52 - 5)

    def test_one_card_dicard_success(self):
        card = self.hand.cards[1]
        self.hand.discard([1], self.deck)

        # make sure card has been disarded and the new card is in the hand.
        self.assertEqual(card, self.deck.discard_pile[0])
        self.assertNotEqual(card, self.hand.cards[1])

    def test_two_card_dicard_success(self):
        indices = [0, 1]
        cards = self.hand.cards[:2]
        self.hand.discard(indices, self.deck)


        # make sure cards have been disarded and the new cards are in the hand.
        for card in cards:
            self.assertTrue(card in self.deck.discard_pile)
            self.assertFalse(card in self.hand.cards)

    def test_two_card_dicard_success(self):
        indices = [0, 1, 2, 3]
        cards = self.hand.cards[:4]
        self.hand.discard(indices, self.deck)

        # make sure cards have been disarded and the new cards are in the hand.
        for card in cards:
            self.assertTrue(card in self.deck.discard_pile)
            self.assertFalse(card in self.hand.cards)

    def test_no_card_dicard_success(self):
        indices = []
        cards = self.hand.cards[:5]
        self.hand.discard(indices, self.deck)

        # make sure cards have been disarded and the new cards are in the hand.
        for card in cards:
            self.assertFalse(card in self.deck.discard_pile)
            self.assertTrue(card in self.hand.cards)

    def test_five_card_discard_fail(self):
        indices = [0,1,2,3,4]
        self.assertEqual(self.hand.discard(indices, self.deck), "Invalid Index")

    def test_bad_index_discard_fail(self):
        indices = [7]
        self.assertEqual(self.hand.discard(indices, self.deck), "Invalid Index")

    def test_negative_index_discard_fail(self):
        indices = [-2]
        self.assertEqual(self.hand.discard(indices, self.deck), "Invalid Index")

    def test_historgram_exists(self):
        self.assertTrue(self.hand.get_histogram())

    def test_flush(self):
        self.score_deck.cards = [Card(6, 0), Card(2, 0), Card(12, 0), Card(13, 0), Card(1,0)]
        self.hand.cards = self.score_deck.cards
        self.assertTrue(self.hand.is_flush())

    def test_hand_pair(self):
        self.score_deck.cards = [Card(14, 0), Card(14, 1), Card(12, 1), Card(13, 1), Card(1,2)]
        self.hand.cards = self.score_deck.cards
        self.score_hand("One Pair")

    def test_hand_two_pair(self):
        self.score_deck.cards = [Card(14, 0), Card(14, 1), Card(12, 1), Card(12, 2), Card(1,2)]
        self.score_hand("Two Pair")

    def test_three_of_a_kind(self):
        self.score_deck.cards = [Card(14, 0), Card(14, 1), Card(14, 2), Card(12, 2), Card(1,2)]
        self.score_hand("Three of a Kind")

    def test_four_of_a_kind(self):
        self.score_deck.cards = [Card(14, 0), Card(14, 1), Card(14, 2), Card(14, 3), Card(1,2)]
        self.score_hand("Four of a Kind")

    def test_full_house(self):
        self.score_deck.cards = [Card(14, 0), Card(14, 1), Card(14, 2), Card(12, 3), Card(12,2)]
        self.score_hand("Full House")

    def test_royal_flush(self):
        self.score_deck.cards = [Card(10, 0), Card(11, 0), Card(12, 0), Card(13, 0), Card(14,0)]
        self.score_hand("Royal Flush")

    def test_straight(self):
        self.score_deck.cards = [Card(1, 0), Card(2, 1), Card(3, 2), Card(4, 3), Card(5,2)]
        self.score_hand("Straight")

    def test_straight_five_at_bottom(self):
        self.score_deck.cards = [Card(2, 0), Card(3, 1), Card(4, 2), Card(5, 3), Card(14,2)]
        self.score_hand("Straight")

    def test_straight_flush(self):
        self.score_deck.cards = [Card(1, 0), Card(2, 0), Card(3, 0), Card(4, 0), Card(5,0)]
        self.score_hand("Straight Flush")

    def test_ace_high(self):
        self.score_deck.cards = [Card(14, 1), Card(12, 2), Card(5, 4), Card(7, 0), Card(10,0)]
        self.score_hand("Ace High")

    def test_high_card(self):
        self.score_deck.cards = [Card(11, 1), Card(12, 2), Card(5, 3), Card(7, 0), Card(10,0)]
        self.score_hand(f"High Card:Q\u2666")
Пример #16
0
    while answer != 'y' and answer != 'n':
        answer = input(question)
    return answer


if __name__ == '__main__':

    while True:
        # Print an opening statement
        print('Welcome to play Black Jack! Get as close to 21 as you can without going over!\n\
        Dealer hits until she reaches 17. Aces count as 1 or 11.')

        # Create & shuffle the deck, deal two cards to each player
        deck = Deck()
        deck.shuffle()
        player = Hand()
        player.add_card(deck.deal())
        player.add_card(deck.deal())
        dealer = Hand()
        dealer.add_card(deck.deal())
        dealer.add_card(deck.deal())

        # Set up the Player's chips
        player_chips = player_chips if 'player_chips' in globals() else Chips()

        # Prompt the Player for their bet
        take_bet(player_chips)  # will modify player_chips.bet

        # Show cards (but keep one dealer card hidden)
        show_some(player, dealer)