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
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 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
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
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]))
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
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")
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
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)
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}")
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))
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('')
def createShuffledDeck(): deck = Deck() deck.shuffle() return deck
def testDeckRemove(self): deck = Deck() card23 = Card(2, 3) deck.remove_card(card23)
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()