示例#1
0
 def __init__(self):
     Deck.__init__(self)
     Hand.__init__(self)
     self.playerTop5Cards = {}
     self.game = {}
     self.sortOrder = ['A', 'K', 'Q', 'J', '0', '9', '8', '7', '6', '5', '4', '3', '2']
     self.suits = ['S', 'H', 'D', 'C']
示例#2
0
def check_deck():
    main_deck = Deck()

    assert len(main_deck.cards) == 52
    print("Shuffling main deck")
    main_deck.shuffle()
    #print(main_deck)
    popped_card = main_deck.pop_card()
    print(popped_card)
    print("Sorting main deck")
    main_deck.sort()
    main_deck.add_card(popped_card)
    #print(main_deck)
    print("Deck testing successful")
示例#3
0
def shell():
    deck = Deck()
    deck.shuffle()
    while True:
        if deck.count_cards() > 15:
            play_hand(deck)
        else:
            deck = Deck()
            deck.shuffle()
            print("Reshuffling deck")
        print("Press enter to play another hand or type 'Quit' to exit")
        print(">>> ", end='')
        ans = input()
        if ans.lower() == 'quit':
            break
def check_deck():
    main_deck = Deck()
    
    assert len(main_deck.cards) == 52
    print("Shuffling main deck")
    main_deck.shuffle()
    #print(main_deck)
    popped_card = main_deck.pop_card()
    print(popped_card)
    print("Sorting main deck")
    main_deck.sort()
    main_deck.add_card(popped_card)
    #print(main_deck)
    print("Deck testing successful")
示例#5
0
def test(n, n_hands=1, n_cards=7):
    """test shuffles a deck of cards, divides it into hands, classifies the
    hands, and counts the number of times various classifications appear.
    n is total number of trials
    n_hands is number of hands dealt per trial
    n_cards is number of cards per hand
    """
    hist = {
        "High Card": 0,
        "Pair": 0,
        "Two Pair": 0,
        "Three of a Kind": 0,
        "Straight": 0,
        "Flush": 0,
        "Full House": 0,
        "Four of a Kind": 0,
        "Straight Flush": 0,
        "Royal Flush": 0
    }

    for i in range(n):
        deck = Deck()
        deck.shuffle()
        for j in range(n_hands):
            hand = PokerHand()
            deck.move_cards(hand, 7)
            hist[hand.classify()] += 1
    ratios = {}
    check = 0
    for h in hist.keys():
        ratios[h] = hist[h] / (n * n_hands)
        check += hist[h]
    if check != n * n_hands:
        return ("Error with number of hands recorded: %g" % check)
    return ratios
示例#6
0
    def probability_of_combinations(deck):

        num_of_hands = 0
        dic_of_combinations = {
            "Straight flush": 0,
            "Four of a kind": 0,
            "Full house": 0,
            "Flush": 0,
            "Straight": 0,
            "Three of a kind": 0,
            "Two pairs": 0,
            "Pair": 0,
            "No combinations": 0
        }
        for _ in range(60000):
            deck = Deck()
            deck.shuffle()

            for i in range(10):
                hand = PokerHand()
                num_of_hands += 1
                deck.move_cards(hand, 5)
                hand.sort()
                """print(hand)
                print(hand.classify())
                print(" ")"""
                dic_of_combinations[hand.classify()] += 1

        for key in dic_of_combinations:
            print(key, float(dic_of_combinations[key] / num_of_hands * 100))
        return None
示例#7
0
 def poker_stats(n):
     dico = dict()
     for i in range(n):
         deck = Deck()
         deck.shuffle()
         for i in range(7):
             hand = PokerHand()
             deck.move_cards(hand, 7)
             hand.classify()
             dico[hand.label]=dico.get(hand.label, 0)+(1/(7*n)*100)
     print(sorted(dico.items(), key=lambda x: -x[1]))
示例#8
0
def single_run(hands={}):
    deck = Deck()
    deck.shuffle()

    # deal the cards and classify the hands
    for i in range(7):
        hand = PokerHand()
        deck.move_cards(hand, 7)
        hand.sort()
        classification = str(hand.classify())
        hands[classification] = hands.get(classification, 0) + 1

    return hands
示例#9
0
def game(handSize, players):
    deck = Deck()
    deck.shuffle()
    hand_labels = []
    for i in range(players):
        hand = PokerHand()
        deck.move_cards(hand, handSize)
        hand.sort()
        hand.classify()
        hand_labels.append(hand.label)
    label_dict = dict()
    for label in hand_labels:
        label_dict[label] = label_dict.get(label, 0) + 1
    return (label_dict)
示例#10
0
def get_classDict():
    deck = Deck()
    deck.shuffle()
    handList = []
    for _ in range(7):
        hand = PokerHand()
        deck.move_cards(hand, 7)
        handList.append(hand)

    classDict = {}
    for hand in handList:
        hand.classify()
        classDict[hand.label] = classDict.get(hand.label, 0) + 1
    return classDict
示例#11
0
def main():
    n = 100000
    freqs = {}

    for i in range(n):
        deck = Deck()
        deck.shuffle()
        hand = PokerHand()
        deck.draw(hand, 5)
        hand.classify()
        freqs[hand.label] = freqs.get(hand.label, 0) + 1

    print("Estimated probabilities:")
    for key in PokerHand.all_labels:
        print(f"{key} \t\t {(freqs.get(key, 0) / n):.10f}")
示例#12
0
def statistics():
    hands = {}
    iterations = 500
    number_of_hands = 7
    for j in range(iterations):
        deck = Deck()
        deck.shuffle()
        for i in range(number_of_hands):
            hand = PokerHand()
            deck.move_cards(hand, 7)
            #print(hand)
            #print('')
            hand.classify()
            hands[hand.label] = hands.get(hand.label, 0) + 1
    for key in sorted(hands):
        print("Probability of a %s is %.3f%%" % (key, hands[key] / (iterations * number_of_hands) * 100))
示例#13
0
    def classify(self):
        '''
		straight: five cards with ranks in sequence
		
		flush: five cards with the same suit
		'''
        label = [
            'pair', 'two pair', 'three of a kind', 'striaght', 'flush',
            'full house', 'four of a kind', 'straight flush'
        ]


if __name__ == '__main__':
    # make a deck
    deck = Deck()
    deck.shuffle()

    # deal the cards and classify the hands
    h = PokerHand()
    deck.move_cards(h, 10)
    h.suit_hist()
    print(h)
    '''
	for i in range(7):
		hand = PokerHand()
		deck.move_cards(hand, 7)
		hand.suit_hist
		hand.sort()
		print(hand)
		print(hand.has_flush())
示例#14
0
 def testDeckRemove(self):
     deck = Deck()
     card23 = Card(2, 3)
     deck.remove_card(card23)
示例#15
0
 def testDeckRemove(self):
     deck = Deck()
     card23 = Card(2, 3)
     deck.remove_card(card23)
示例#16
0
文件: 2nd_exp.py 项目: tscott8/cs306
def generate_card_lists():
    lists = []
    deck1 = Deck()
    deck2 = Deck()
    deck3 = Deck()
    deck2.shuffle()
    deck3.reverse()
    lists = [(deck1.get_cards(), "ordered"), (deck2.get_cards(), "random"),
             (deck3.get_cards(), "reversed")]
    return lists
示例#17
0
    popped_card = main_deck.pop_card()
    print(popped_card)
    print("Sorting main deck")
    main_deck.sort()
    main_deck.add_card(popped_card)
    #print(main_deck)
    print("Deck testing successful")


def deal_hand(deck):
    """ Veneer function for dealing hands """
    return deck.deal_hands(4, 13)


def print_hands(hand_list):
    """ Pretty prints all the cards in each hand """
    if hand_list == None or len(hand_list) == 0:
        print("No hands in list")
    else:
        for hand in hand_list:
            print(hand.label + "\n" + str(hand) + "\n")


main_deck = Deck()
main_deck.shuffle()
hands = deal_hand(main_deck)
print_hands(hands)

#check_comparisons()
#check_deck()
    #print(main_deck)
    popped_card = main_deck.pop_card()
    print(popped_card)
    print("Sorting main deck")
    main_deck.sort()
    main_deck.add_card(popped_card)
    #print(main_deck)
    print("Deck testing successful")


def deal_hand(deck):
    """ Veneer function for dealing hands """
    return deck.deal_hands(4,13)

def print_hands(hand_list):
    """ Pretty prints all the cards in each hand """
    if hand_list == None or len(hand_list) == 0:
        print("No hands in list")
    else:
        for hand in hand_list:
            print(hand.label + "\n" + str(hand) + "\n")

main_deck = Deck()
main_deck.shuffle()            
hands = deal_hand(main_deck)
print_hands(hands)

#check_comparisons()
#check_deck()

示例#19
0
def createShuffledDeck():
    deck = Deck()
    deck.shuffle()
    return deck
示例#20
0
    return hands


def print_probabilities(hands):

    total = 0

    for val in hands:
        total += hands[val]

    print '**********************************************'
    print 'Poker Hand Probabilities'
    print '**********************************************'

    for val in hands:
        hands[val] = (hands[val] / float(total)) * 100

    for key in sorted(hands, key=hands.get, reverse=False):
        print '%s: %.2f %%' % (key, hands[key])

if __name__ == '__main__':
    # make a deck
    deck = Deck()
    deck.shuffle()

    # deal the cards and classify the hands
    for i in range(20000):
        hands = single_run()

    print_probabilities(hands)
示例#21
0
        if self.has_straight_flush():
            classification.append({"Straight Flush":8})

        for hands in classification:
            hand.label = max(hands, key=hands.get)

        return hand

if __name__ == '__main__':
    num_decks = int(input('How Many Decks to Shuffle?')) # Shuffle 1,000+ decks for highest accuracy
    num_hands = int(input('How Many Hands to Deal?')) # usually deal 7 hands
    total_hands = num_decks * num_hands
    prob_hist = {}

    for i in range(num_decks):
        deck = Deck()
        deck.shuffle()

    # deal the cards and classify the hands
        for i in range(num_hands):
            hand = PokerHand()
            deck.move_cards(hand, num_hands)
            hand.sort()
            hand.classify()
            # print(hand)
            # print(hand.label+'\n')
            # Creat histogram of each best hand
            prob_hist[hand.label] = prob_hist.get(hand.label, 0) + 1/total_hands*100
    # create dataframe of histogram
    table = pd.DataFrame.from_dict(prob_hist,orient='index', columns=['Best Hand Probability(%)'])
    print(table)
示例#22
0
        else:
            return False

    def has_three_of_a_kind(self):
        self.rank_hist()
        i = 0
        for val in self.ranks.values():
            if val == 3:
                i += 1
        if i == 1:
            return True
        else:
            return False


if __name__ == '__main__':
    # make a deck
    deck = Deck()
    deck.shuffle()

    # deal the cards and classify the hands
    for i in range(7):
        hand = PokerHand()
        deck.move_cards(hand, 5)
        hand.sort()
        print(hand)
        print("Has flush:", hand.has_flush())
        print("Has a pair:", hand.has_pair())
        print("Has two pairs:", hand.has_pair(num=2))
        print('')
示例#23
0
        self.suits = {}
        for card in self.cards:
            self.suits[card.suit] = self.suits.get(card.suit, 0) + 1

    def has_flush(self):
        """Returns True if the hand has a flush, False otherwise.
      
        Note that this works correctly for hands with more than 5 cards.
        """
        self.suit_hist()
        for val in self.suits.values():
            if val >= 5:
                return True
        return False


if __name__ == '__main__':
    # make a deck
    deck = Deck()
    deck.shuffle()

    # deal the cards and classify the hands
    for i in range(7):
        hand = PokerHand()
        deck.move_cards(hand, 7)
        hand.sort()
        print(hand)
        print(hand.has_flush())
        print('')