def test_is_pair_when_full_house_exist(self):
        hand = [{
            'value': 12,
            'suit': 'Clubs',
            'face': 'Q',
            'color': 'black'
        }, {
            'value': 12,
            'suit': 'Spades',
            'face': 'Q',
            'color': 'black'
        }, {
            'value': 8,
            'suit': 'Spades',
            'face': '8',
            'color': 'black'
        }, {
            'value': 12,
            'suit': 'Hearts',
            'face': 'Q',
            'color': 'red'
        }, {
            'value': 8,
            'suit': 'Clubs',
            'face': '8',
            'color': 'red'
        }]

        self.assertEqual(card_tests.is_pair(hand), False)
    def test_is_pair_when_3_pair_exists(self):
        hand = [{
            'value': 2,
            'suit': 'Clubs',
            'face': '2',
            'color': 'black'
        }, {
            'value': 2,
            'suit': 'Hearts',
            'face': '2',
            'color': 'black'
        }, {
            'value': 4,
            'suit': 'Clubs',
            'face': '4',
            'color': 'black'
        }, {
            'value': 4,
            'suit': 'Diamonds',
            'face': '4',
            'color': 'red'
        }, {
            'value': 4,
            'suit': 'Clubs',
            'face': '4',
            'color': 'red'
        }]

        self.assertEqual(card_tests.is_pair(hand), False)
    def test_is_pair_when_pair_exist(self):
        hand = [{
            'value': 2,
            'suit': 'Clubs',
            'face': '2',
            'color': 'black'
        }, {
            'value': 2,
            'suit': 'Hearts',
            'face': '2',
            'color': 'black'
        }, {
            'value': 4,
            'suit': 'Clubs',
            'face': '4',
            'color': 'black'
        }, {
            'value': 5,
            'suit': 'Clubs',
            'face': '5',
            'color': 'red'
        }, {
            'value': 6,
            'suit': 'Clubs',
            'face': '6',
            'color': 'red'
        }]

        self.assertEqual(card_tests.is_pair(hand), True)
def main():
    deck = build_deck()
    shuffle(deck)

    hand = deal_hand(deck)

    print("\nHand dealt:\n")
    for card in hand:
        print("{} of {}".format(card['face'], card['suit']))

    print("\nAnalysis:\n\nHand contains:")
    """hand = [{'value': 2, 'suit': 'Clubs', 'face': '2', 'color': 'black'},
            {'value': 2, 'suit': 'Clubs', 'face': '2', 'color': 'black'},
            {'value': 2, 'suit': 'Clubs', 'face': '2', 'color': 'black'},
            {'value': 4, 'suit': 'Clubs', 'face': '4', 'color': 'red'},
            {'value': 4, 'suit': 'Clubs', 'face': '4', 'color': 'red'}]
    """
    check_high_card = card_tests.is_high_card(hand)
    check_pair = card_tests.is_pair(hand)
    check_2_pair = card_tests.is_2_pair(hand)
    check_3_of_a_kind = card_tests.is_3_of_a_kind(hand)
    check_4_of_a_kind = card_tests.is_4_of_a_kind(hand)
    check_full_house = card_tests.is_full_house(hand)
    check_flush = card_tests.is_flush(hand)
    check_straight = card_tests.is_straight(hand)
    check_straight_flush = card_tests.is_straight_flush(hand)

    if check_straight_flush:
        print("Straight flush")

    elif check_high_card:
        print("High card")

    elif check_full_house:
        print("Full house")

    elif check_pair:
        print("A pair")

    elif check_2_pair:
        print("Two pair")

    elif check_3_of_a_kind:
        print("3 of a kind")

    elif check_4_of_a_kind:
        print("4 of a kind")

    elif check_flush:
        print("A flush")

    elif check_straight:
        print("A straight")
def main():
    times_to_test = get_number_of_poker_hands_to_test()

    file_to_open = input('name a file to write the results to:\n')
    file_to_open = file_to_open + '.csv'

    deck = build_deck()
    #  shuffle(deck)

    straight_flush_count = 0
    high_card_count = 0
    full_house_count = 0
    pair_count = 0
    two_pair_count = 0
    three_of_a_kind_count = 0
    four_of_a_kind_count = 0
    flush_count = 0
    straight_count = 0
    count = 0

    while count < times_to_test:

        shuffle(deck)
        hand = deal_hand(deck)

        #  Nested if statements to reduce the number of functions that run to test the poker hand each iteration
        check_high_card = card_tests.is_high_card(hand)
        if check_high_card:
            high_card_count += 1
        elif not check_high_card:
            check_pair = card_tests.is_pair(hand)
            if check_pair:
                pair_count += 1
            elif not check_pair:
                check_2_pair = card_tests.is_2_pair(hand)
                if check_2_pair:
                    two_pair_count += 1
                elif not check_2_pair:
                    check_3_of_a_kind = card_tests.is_3_of_a_kind(hand)
                    if check_3_of_a_kind:
                        three_of_a_kind_count += 1
                    elif not check_3_of_a_kind:
                        check_straight = card_tests.is_straight(hand)
                        if check_straight:
                            straight_count += 1
                        elif not check_straight:
                            check_flush = card_tests.is_flush(hand)
                            if check_flush:
                                flush_count += 1
                            elif not check_flush:
                                check_full_house = card_tests.is_full_house(
                                    hand)
                                if check_full_house:
                                    full_house_count += 1
                                elif not check_full_house:
                                    check_4_of_a_kind = card_tests.is_4_of_a_kind(
                                        hand)
                                    if check_4_of_a_kind:
                                        four_of_a_kind_count += 1
                                    elif not check_4_of_a_kind:
                                        check_straight_flush = card_tests.is_straight_flush(
                                            hand)
                                        if check_straight_flush:
                                            straight_flush_count += 1

        count += 1

    total_poker_hands = 2598960

    prob_of_hand_appearing = {
        'straight_flush': 40 / total_poker_hands,
        'high_card': 1302540 / total_poker_hands,
        'full_house': 3744 / total_poker_hands,
        'pair': 1098240 / total_poker_hands,
        'two_pair': 123552 / total_poker_hands,
        'three_of_a_kind': 54912 / total_poker_hands,
        'four_of_a_kind': 624 / total_poker_hands,
        'flush': 5108 / total_poker_hands,
        'straight': 10200 / total_poker_hands
    }

    count_of_hand_appearing = {
        'straight_flush': straight_flush_count,
        'high_card': high_card_count,
        'full_house': full_house_count,
        'pair': pair_count,
        'two_pair': two_pair_count,
        'three_of_a_kind': three_of_a_kind_count,
        'four_of_a_kind': four_of_a_kind_count,
        'flush': flush_count,
        'straight': straight_count
    }

    #  try catch for opening and writing to file
    try:
        csv_file = open(file_to_open, 'w', newline='\n')
        file_headings = [
            'hand', 'occurrences', 'percent', 'expected', 'difference'
        ]
        csv_writer = csv.DictWriter(csv_file, fieldnames=file_headings)
        csv_writer.writeheader()

        print('\n{:20}{:26}{:26}{:30}{:34}'.format('hand', '\toccurrences',
                                                   'percent', 'expected',
                                                   'difference'))

        for key in prob_of_hand_appearing:
            print('{:20}{:15}{:21}{:27}{:32}'.format(
                key, count_of_hand_appearing[key],
                round(count_of_hand_appearing[key] / times_to_test, 6),
                round(prob_of_hand_appearing[key], 6),
                round(((count_of_hand_appearing[key] / times_to_test) -
                       prob_of_hand_appearing[key]), 6)))

            line_to_write = {
                'hand':
                key,
                'occurrences':
                count_of_hand_appearing[key],
                'percent':
                round(count_of_hand_appearing[key] / times_to_test, 6),
                'expected':
                round(prob_of_hand_appearing[key], 6),
                'difference':
                round(((count_of_hand_appearing[key] / times_to_test) -
                       prob_of_hand_appearing[key]), 6),
            }

            csv_writer.writerow(line_to_write)

        csv_file.close()

    except PermissionError:
        print('Cannot open file for writing')
        print('File might be open in another program.')
예제 #6
0
def main():
    deck = build_deck()
    shuffle(deck)

    hand_1, hand_2 = deal_hand(deck)

    print("\nHands dealt:\nHand 1:")
    for card in hand_1:
        print("{} of {}".format(card['face'], card['suit']))

    print("\nHand 2:")
    for card_2 in hand_2:
        print("{} of {}".format(card_2['face'], card_2['suit']))

    print("\nAnalysis:\n\nHands Contain:")

    #  Below passes Hand 1 to the given functions to find what it is
    check_high_card = card_tests.is_high_card(hand_1)
    check_pair = card_tests.is_pair(hand_1)
    check_2_pair = card_tests.is_2_pair(hand_1)
    check_3_of_a_kind = card_tests.is_3_of_a_kind(hand_1)
    check_4_of_a_kind = card_tests.is_4_of_a_kind(hand_1)
    check_full_house = card_tests.is_full_house(hand_1)
    check_flush = card_tests.is_flush(hand_1)
    check_straight = card_tests.is_straight(hand_1)
    check_straight_flush = card_tests.is_straight_flush(hand_1)

    #  Below passes Hand 2 to the given functions to find what it is
    check_high_card_hand_2 = card_tests.is_high_card(hand_2)
    check_pair_hand_2 = card_tests.is_pair(hand_2)
    check_2_pair_hand_2 = card_tests.is_2_pair(hand_2)
    check_3_of_a_kind_hand_2 = card_tests.is_3_of_a_kind(hand_2)
    check_4_of_a_kind_hand_2 = card_tests.is_4_of_a_kind(hand_2)
    check_full_house_hand_2 = card_tests.is_full_house(hand_2)
    check_flush_hand_2 = card_tests.is_flush(hand_2)
    check_straight_hand_2 = card_tests.is_straight(hand_2)
    check_straight_flush_hand_2 = card_tests.is_straight_flush(hand_2)

    check_hand_1 = 0

    if check_straight_flush:
        print("Hand 1: Straight flush")
        check_hand_1 = 1

    elif check_high_card:
        print("Hand 1: High card")
        check_hand_1 = 9

    elif check_full_house:
        print("Hand 1: Full house")
        check_hand_1 = 3

    elif check_pair:
        print("Hand 1: A pair")
        check_hand_1 = 8

    elif check_2_pair:
        print("Hand 1: Two pair")
        check_hand_1 = 7

    elif check_3_of_a_kind:
        print("Hand 1: 3 of a kind")
        check_hand_1 = 6

    elif check_4_of_a_kind:
        print("Hand 1: 4 of a kind")
        check_hand_1 = 2

    elif check_flush:
        print("Hand 1: A flush")
        check_hand_1 = 4

    elif check_straight:
        print("Hand 1: A straight")
        check_hand_1 = 5

    check_hand_2 = 0

    if check_straight_flush_hand_2:
        print("Hand 2: Straight flush")
        check_hand_2 = 1

    elif check_high_card_hand_2:
        print("Hand 2: High card")
        check_hand_2 = 9

    elif check_full_house_hand_2:
        print("Hand 2: Full house")
        check_hand_2 = 3

    elif check_pair_hand_2:
        print("Hand 2: A pair")
        check_hand_2 = 8

    elif check_2_pair_hand_2:
        print("Hand 2: Two pair")
        check_hand_2 = 7

    elif check_3_of_a_kind_hand_2:
        print("Hand 2: 3 of a kind")
        check_hand_2 = 6

    elif check_4_of_a_kind_hand_2:
        print("Hand 2: 4 of a kind")
        check_hand_2 = 2

    elif check_flush_hand_2:
        print("Hand 2: A flush")
        check_hand_2 = 4

    elif check_straight_hand_2:
        print("Hand 2: A straight")
        check_hand_2 = 5

    print("\nWINNER IS:")
    #  Checks who wins
    if check_hand_1 < check_hand_2:
        print("Hand 1!!!!!")

    elif check_hand_1 > check_hand_2:
        print("Hand 2!!!!!")

    elif check_hand_1 == check_hand_2:
        print("NOOO IT'S A DRAW")