def receive_result(sck, idx): global hands_stored, play_hand, play_hand_count pickled_hand = sck.recv(4096) hand = pickle.loads(pickled_hand) hands_stored[idx] = hand play_hand[idx] = True play_hand_count += 1 this_hand = PokerHand() points_stored[idx] = this_hand.get_six_hands_points(hands_stored[idx]) sd = ShowDownPoints(points_stored, play_hand) for i in range(6): if play_hand[i] == True: for cardlist in hands_stored[i]: window.title("Player Hands") y_list = [ 0, 5 * Y_GAP, 4 * Y_GAP, 3 * Y_GAP, 2 * Y_GAP, Y_GAP, 0 ] for j in [6, 5, 4, 3, 2, 1]: x = X_OFFSET x1 = x y = y_list[j] + 8 overlap_factor = 1 for card in cardlist[j]: if len(cardlist[i]) > 5: overlap_factor = 5 / (len(cardlist[j]) + 1) r.create_image(x, y, image=image_dict[card[0:3]], anchor="nw", tag="best") x += X_GAP * overlap_factor x1 = x1 + 5 * X_GAP x = x1 print("Server Received", idx, hand)
def bestHand(cards): best = PokerHand('2S 3C 4H 5C 7H') # Weakest Poker Hand cards = cards.split(' ') for one in range(len(cards)): for two in range(one + 1, len(cards)): for three in range(two + 1, len(cards)): for four in range(three + 1, len(cards)): for five in range(four + 1, len(cards)): test = PokerHand(cards[one] + ' ' + cards[two] + ' ' + cards[three] + ' ' + cards[four] + ' ' + cards[five]) if test > best or test == best: best = test return best
def three_2s_AK(self): return PokerHand([ PlayingCard(Suit.Spades, FaceValue.Tre), PlayingCard(Suit.Diamonds, FaceValue.Tre), PlayingCard(Suit.Clubs, FaceValue.Tre), PlayingCard(Suit.Spades, FaceValue.Ace), PlayingCard(Suit.Hearts, FaceValue.King) ])
def flush_s_A7532(self): return PokerHand([ PlayingCard(Suit.Spades, FaceValue.Ace), PlayingCard(Suit.Spades, FaceValue.Seven), PlayingCard(Suit.Spades, FaceValue.Five), PlayingCard(Suit.Spades, FaceValue.Tre), PlayingCard(Suit.Spades, FaceValue.Deuce) ])
def flush_h_KQJ98(self): return PokerHand([ PlayingCard(Suit.Hearts, FaceValue.King), PlayingCard(Suit.Hearts, FaceValue.Queen), PlayingCard(Suit.Hearts, FaceValue.Jack), PlayingCard(Suit.Hearts, FaceValue.Nine), PlayingCard(Suit.Hearts, FaceValue.Eight) ])
def six_high_straight2(self): return PokerHand([ PlayingCard(Suit.Clubs, FaceValue.Six), PlayingCard(Suit.Hearts, FaceValue.Five), PlayingCard(Suit.Spades, FaceValue.Four), PlayingCard(Suit.Hearts, FaceValue.Tre), PlayingCard(Suit.Hearts, FaceValue.Deuce) ])
def ace_high_straight(self): return PokerHand([ PlayingCard(Suit.Diamonds, FaceValue.Ace), PlayingCard(Suit.Hearts, FaceValue.King), PlayingCard(Suit.Hearts, FaceValue.Queen), PlayingCard(Suit.Hearts, FaceValue.Jack), PlayingCard(Suit.Hearts, FaceValue.Ten) ])
def eights_full_of_2s(self): return PokerHand([ PlayingCard(Suit.Spades, FaceValue.Eight), PlayingCard(Suit.Diamonds, FaceValue.Eight), PlayingCard(Suit.Clubs, FaceValue.Eight), PlayingCard(Suit.Spades, FaceValue.Deuce), PlayingCard(Suit.Diamonds, FaceValue.Deuce) ])
def sevens_full_of_aces(self): return PokerHand([ PlayingCard(Suit.Spades, FaceValue.Seven), PlayingCard(Suit.Diamonds, FaceValue.Seven), PlayingCard(Suit.Clubs, FaceValue.Seven), PlayingCard(Suit.Clubs, FaceValue.Ace), PlayingCard(Suit.Diamonds, FaceValue.Ace) ])
def four_4s_9(self): return PokerHand([ PlayingCard(Suit.Spades, FaceValue.Four), PlayingCard(Suit.Diamonds, FaceValue.Four), PlayingCard(Suit.Clubs, FaceValue.Four), PlayingCard(Suit.Hearts, FaceValue.Four), PlayingCard(Suit.Spades, FaceValue.Nine) ])
def four_8s_3(self): return PokerHand([ PlayingCard(Suit.Spades, FaceValue.Eight), PlayingCard(Suit.Diamonds, FaceValue.Eight), PlayingCard(Suit.Clubs, FaceValue.Eight), PlayingCard(Suit.Hearts, FaceValue.Eight), PlayingCard(Suit.Diamonds, FaceValue.Tre) ])
def king_high_straight_flush(self): return PokerHand([ PlayingCard(Suit.Diamonds, FaceValue.King), PlayingCard(Suit.Diamonds, FaceValue.Queen), PlayingCard(Suit.Diamonds, FaceValue.Jack), PlayingCard(Suit.Diamonds, FaceValue.Ten), PlayingCard(Suit.Diamonds, FaceValue.Nine) ])
def aces_2s_K(self): return PokerHand([ PlayingCard(Suit.Spades, FaceValue.Ace), PlayingCard(Suit.Diamonds, FaceValue.Ace), PlayingCard(Suit.Spades, FaceValue.Deuce), PlayingCard(Suit.Diamonds, FaceValue.Deuce), PlayingCard(Suit.Spades, FaceValue.King) ])
def Ks_Qs_J(self): return PokerHand([ PlayingCard(Suit.Diamonds, FaceValue.King), PlayingCard(Suit.Clubs, FaceValue.King), PlayingCard(Suit.Spades, FaceValue.Queen), PlayingCard(Suit.Diamonds, FaceValue.Queen), PlayingCard(Suit.Spades, FaceValue.Jack) ])
def royal_flush_hearts(self): return PokerHand([ PlayingCard(Suit.Hearts, FaceValue.Ace), PlayingCard(Suit.Hearts, FaceValue.King), PlayingCard(Suit.Hearts, FaceValue.Queen), PlayingCard(Suit.Hearts, FaceValue.Jack), PlayingCard(Suit.Hearts, FaceValue.Ten) ])
def seven_5_4_3_2(self): return PokerHand([ PlayingCard(Suit.Spades, FaceValue.Seven), PlayingCard(Suit.Spades, FaceValue.Five), PlayingCard(Suit.Diamonds, FaceValue.Four), PlayingCard(Suit.Spades, FaceValue.Tre), PlayingCard(Suit.Spades, FaceValue.Deuce) ])
def tens_9s_8v2(self): return PokerHand([ PlayingCard(Suit.Clubs, FaceValue.Ten), PlayingCard(Suit.Hearts, FaceValue.Ten), PlayingCard(Suit.Clubs, FaceValue.Nine), PlayingCard(Suit.Hearts, FaceValue.Nine), PlayingCard(Suit.Diamonds, FaceValue.Eight) ])
def ace_K_Q_J_8(self): return PokerHand([ PlayingCard(Suit.Diamonds, FaceValue.Ace), PlayingCard(Suit.Spades, FaceValue.King), PlayingCard(Suit.Diamonds, FaceValue.Queen), PlayingCard(Suit.Diamonds, FaceValue.Jack), PlayingCard(Suit.Spades, FaceValue.Eight) ])
def ace_K_Q_J_8v2(self): return PokerHand([ PlayingCard(Suit.Clubs, FaceValue.Ace), PlayingCard(Suit.Hearts, FaceValue.King), PlayingCard(Suit.Clubs, FaceValue.Queen), PlayingCard(Suit.Clubs, FaceValue.Jack), PlayingCard(Suit.Clubs, FaceValue.Eight) ])
def twos_K_Q_J(self): return PokerHand([ PlayingCard(Suit.Clubs, FaceValue.Deuce), PlayingCard(Suit.Hearts, FaceValue.Deuce), PlayingCard(Suit.Clubs, FaceValue.King), PlayingCard(Suit.Diamonds, FaceValue.Queen), PlayingCard(Suit.Hearts, FaceValue.Jack) ])
def ace_k_q_j_9(self): return PokerHand([ PlayingCard(Suit.Spades, FaceValue.Ace), PlayingCard(Suit.Diamonds, FaceValue.King), PlayingCard(Suit.Spades, FaceValue.Queen), PlayingCard(Suit.Spades, FaceValue.Jack), PlayingCard(Suit.Spades, FaceValue.Nine) ])
def fives_J_9_6v2(self): return PokerHand([ PlayingCard(Suit.Clubs, FaceValue.Five), PlayingCard(Suit.Hearts, FaceValue.Five), PlayingCard(Suit.Clubs, FaceValue.Jack), PlayingCard(Suit.Diamonds, FaceValue.Nine), PlayingCard(Suit.Clubs, FaceValue.Six) ])
def fives_J_9_6(self): return PokerHand([ PlayingCard(Suit.Spades, FaceValue.Five), PlayingCard(Suit.Diamonds, FaceValue.Five), PlayingCard(Suit.Diamonds, FaceValue.Jack), PlayingCard(Suit.Spades, FaceValue.Nine), PlayingCard(Suit.Diamonds, FaceValue.Six) ])
def tens_K_3_2(self): return PokerHand([ PlayingCard(Suit.Spades, FaceValue.Ten), PlayingCard(Suit.Diamonds, FaceValue.Ten), PlayingCard(Suit.Diamonds, FaceValue.King), PlayingCard(Suit.Diamonds, FaceValue.Tre), PlayingCard(Suit.Diamonds, FaceValue.Deuce) ])
def tens_8_7_6(self): return PokerHand([ PlayingCard(Suit.Clubs, FaceValue.Ten), PlayingCard(Suit.Hearts, FaceValue.Ten), PlayingCard(Suit.Spades, FaceValue.Eight), PlayingCard(Suit.Spades, FaceValue.Seven), PlayingCard(Suit.Spades, FaceValue.Six) ])
def aces_4_3_2(self): return PokerHand([ PlayingCard(Suit.Clubs, FaceValue.Ace), PlayingCard(Suit.Hearts, FaceValue.Ace), PlayingCard(Suit.Spades, FaceValue.Four), PlayingCard(Suit.Spades, FaceValue.Tre), PlayingCard(Suit.Spades, FaceValue.Deuce) ])
def fours_3s_K(self): return PokerHand([ PlayingCard(Suit.Spades, FaceValue.Four), PlayingCard(Suit.Diamonds, FaceValue.Four), PlayingCard(Suit.Diamonds, FaceValue.Tre), PlayingCard(Suit.Clubs, FaceValue.Tre), PlayingCard(Suit.Hearts, FaceValue.King) ])
def tens_9s_8(self): return PokerHand([ PlayingCard(Suit.Spades, FaceValue.Ten), PlayingCard(Suit.Diamonds, FaceValue.Ten), PlayingCard(Suit.Spades, FaceValue.Nine), PlayingCard(Suit.Diamonds, FaceValue.Nine), PlayingCard(Suit.Spades, FaceValue.Eight) ])
def GetAnswerPE054(self): count = 0 pokerHand = PokerHand() pokerInputFile = open('d:\poker.txt', 'r') play = pokerInputFile.readline() playIndex = 0 while play is not '': playIndex += 1 cardsInPlay = play.split(' ') cards1 = [] cards2 = [] for index in range(5): cards1.append(Card(cardsInPlay[index][0], cardsInPlay[index][1])) hand1 = Hand(cards1) for index in range(5, 10): cards2.append(Card(cardsInPlay[index][0], cardsInPlay[index][1])) hand2 = Hand(cards2) if(pokerHand.compareHand(hand1, hand2) > 0): count = count + 1 play = pokerInputFile.readline() print "the answer is " + str(count)
def best_hand(self): if len(self.table.table_cards) + len(self._cards) < 5: return None hands = [PokerHand(list(card_set)) for card_set in itertools.combinations( self._cards + self.table.table_cards, 5)] # only look at hand_type, not card values hands.sort(key=lambda h: h.hand_type.value, reverse=True) # throw out hands of lower valued hand_type contenders = [] for hand in hands: if hand.hand_type == hands[0].hand_type: contenders.append(hand) else: break # Sort hands by face_value and return best contenders.sort() return contenders[-1]
def __init__(self, rounds=1000, *choices): # Variable declarations for counting self.flush = 0 self.fullHouse = 0 self.threeOfAKind = 0 self.fourOfAKind = 0 self.onePair = 0 self.twoPairs = 0 self.rounds = rounds self.choices = choices # Check for empty values if len(self.choices) == 0: sys.exit("Empty poker type not allowed") # Run game 10000 times for i in range(self.rounds): # Instantiate card and decks self.deck = Deck() self.deck.shuffle() # Play 10 rounds for j in range(10): self.poker = PokerHand() # Pick up 5 cards for k in range(5): self.poker.add(self.deck.deal()) # Check if cards are cetain types of pokers if self.poker.isFlush(): self.flush += 1 elif self.poker.hasFullHouse(): self.fullHouse += 1 elif self.poker.hasThreeOfAKind(): self.threeOfAKind += 1 elif self.poker.hasFourOfAKind(): self.fourOfAKind += 1 elif self.poker.hasOnePair(): self.onePair += 1 elif self.poker.hasTwoPairs(): self.twoPairs += 1
class Probability: def __init__(self, rounds=1000, *choices): # Variable declarations for counting self.flush = 0 self.fullHouse = 0 self.threeOfAKind = 0 self.fourOfAKind = 0 self.onePair = 0 self.twoPairs = 0 self.rounds = rounds self.choices = choices # Check for empty values if len(self.choices) == 0: sys.exit("Empty poker type not allowed") # Run game 10000 times for i in range(self.rounds): # Instantiate card and decks self.deck = Deck() self.deck.shuffle() # Play 10 rounds for j in range(10): self.poker = PokerHand() # Pick up 5 cards for k in range(5): self.poker.add(self.deck.deal()) # Check if cards are cetain types of pokers if self.poker.isFlush(): self.flush += 1 elif self.poker.hasFullHouse(): self.fullHouse += 1 elif self.poker.hasThreeOfAKind(): self.threeOfAKind += 1 elif self.poker.hasFourOfAKind(): self.fourOfAKind += 1 elif self.poker.hasOnePair(): self.onePair += 1 elif self.poker.hasTwoPairs(): self.twoPairs += 1 def show(self): # Results self.head = "%s hands dealt\n%s\n" self.head = self.head % (self.rounds, (len(self.head) - 1) * "=") print(self.head) for choice in self.choices: choice = choice.lower().replace(" ", "") if choice == "f" or choice == "flush": print("Flush occurred %s times" % self.flush) print("Estimated P(Flush) is: %.3f\n" % ((self.flush * 100)/self.rounds)) elif choice == "fh" or choice == "fullhouse": print("Full House occurred %s times" % self.fullHouse) print("Estimated P(Full House) is: %.3f\n" % ((self.fullHouse * 100)/self.rounds)) elif choice == "toak" or choice == "threeofakind": print("Three of a kind %s times" % self.threeOfAKind) print("Estimated P(Three of a kind) is: %.3f\n" % ((self.threeOfAKind * 100)/self.rounds)) elif choice == "foak" or choice == "fourofakind": print("Four of a kind %s times" % self.fourOfAKind) print("Estimated P(Four of a kind) is: %.3f\n" % ((self.fourOfAKind * 100)/self.rounds)) elif choice == "op" or choice == "onepair": print("One Pair %s times" % self.onePair) print("Estimated P(One Pair) is: %.3f\n" % ((self.onePair * 100)/self.rounds)) elif choice == "tp" or choice == "twopairs": print("Two Pairs %s times" % self.twoPairs) print("Estimated P(Two Pairs) is: %.3f\n" % ((self.twoPairs * 100)/self.rounds)) def plot(self): pokerlists = [] header = [] for choice in self.choices: choice = choice.lower().replace(" ", "") if choice == "f" or choice == "flush": pokerlists.append(self.flush) header.append("Flush") elif choice == "fh" or choice == "fullhouse": pokerlists.append(self.fullHouse) header.append("Full House") elif choice == "toak" or choice == "threeofakind": pokerlists.append(self.threeOfAKind) header.append("Three of a kind") elif choice == "foak" or choice == "fourofakind": pokerlists.append(self.fourOfAKind) header.append("Four of a kind") elif choice == "op" or choice == "onepair": pokerlists.append(self.onePair) header.append("One Pair") elif choice == "tp" or choice == "twopairs": pokerlists.append(self.twoPairs) header.append("Two Pairs") try: n_groups = len(pokerlists) fig, ax = plt.subplots() index = np.arange(n_groups) bar_width = 0.5 opacity = 0.4 rects1 = plt.bar(index, pokerlists, bar_width, alpha=opacity, color='b', label='Trials: %s' % self.rounds) fig.canvas.set_window_title('Poker Hand') plt.xlabel('Poker Hands') plt.ylabel('Total number of Occurrences') plt.title('Poker Hand') plt.xticks(index + bar_width, header) plt.legend() plt.tight_layout() plt.show() except: return "Error: Can't plot graph"
class Employee: def __init__(self, name, age): self.name = name self.age = age def __repr__(self): return '[{} ({} years old)]'.format(self.name, self.age) def __lt__(self, other): return self.age < other.age # # Exercise 6 (Poker) # NUM_TRIALS = 1000000 counts = defaultdict(int) for _ in xrange(NUM_TRIALS): deck = Deck() deck.shuffle() hand = PokerHand() deck.move_cards(hand, 7) counts[hand.classify()] += 1 print print 'Poker hands:' for h in counts: print ' {}:{}{}'.format(h, ' ' * (20 - len(h)), float(counts[h]) / NUM_TRIALS)
def test_poker_hand(self): self.assertTrue( PokerHand("TC TH 5C 5H KH").compare_with( PokerHand("9C 9H 5C 5H AC")) == Result.WIN) self.assertTrue( PokerHand("TS TD KC JC 7C").compare_with( PokerHand("JS JC AS KC TD")) == Result.LOSS) self.assertTrue( PokerHand("7H 7C QC JS TS").compare_with( PokerHand("7D 7C JS TS 6D")) == Result.WIN) self.assertTrue( PokerHand("5S 5D 8C 7S 6H").compare_with( PokerHand("7D 7S 5S 5D JS")) == Result.LOSS) self.assertTrue( PokerHand("AS AD KD 7C 3D").compare_with( PokerHand("AD AH KD 7C 4S")) == Result.LOSS) self.assertTrue( PokerHand("TS JS QS KS AS").compare_with( PokerHand("AC AH AS AS KS")) == Result.WIN) self.assertTrue( PokerHand("TS JS QS KS AS").compare_with( PokerHand("TC JS QC KS AC")) == Result.WIN) self.assertTrue( PokerHand("TS JS QS KS AS").compare_with( PokerHand("QH QS QC AS 8H")) == Result.WIN) self.assertTrue( PokerHand("AC AH AS AS KS").compare_with( PokerHand("TC JS QC KS AC")) == Result.WIN) self.assertTrue( PokerHand("AC AH AS AS KS").compare_with( PokerHand("QH QS QC AS 8H")) == Result.WIN) self.assertTrue( PokerHand("TC JS QC KS AC").compare_with( PokerHand("QH QS QC AS 8H")) == Result.WIN) self.assertTrue( PokerHand("7H 8H 9H TH JH").compare_with( PokerHand("JH JC JS JD TH")) == Result.WIN) self.assertTrue( PokerHand("7H 8H 9H TH JH").compare_with( PokerHand("4H 5H 9H TH JH")) == Result.WIN) self.assertTrue( PokerHand("7H 8H 9H TH JH").compare_with( PokerHand("7C 8S 9H TH JH")) == Result.WIN) self.assertTrue( PokerHand("7H 8H 9H TH JH").compare_with( PokerHand("TS TH TD JH JD")) == Result.WIN) self.assertTrue( PokerHand("7H 8H 9H TH JH").compare_with( PokerHand("JH JD TH TC 4C")) == Result.WIN) self.assertTrue( PokerHand("JH JC JS JD TH").compare_with( PokerHand("4H 5H 9H TH JH")) == Result.WIN) self.assertTrue( PokerHand("JH JC JS JD TH").compare_with( PokerHand("7C 8S 9H TH JH")) == Result.WIN) self.assertTrue( PokerHand("JH JC JS JD TH").compare_with( PokerHand("TS TH TD JH JD")) == Result.WIN) self.assertTrue( PokerHand("JH JC JS JD TH").compare_with( PokerHand("JH JD TH TC 4C")) == Result.WIN) self.assertTrue( PokerHand("4H 5H 9H TH JH").compare_with( PokerHand("7C 8S 9H TH JH")) == Result.WIN) self.assertTrue( PokerHand("4H 5H 9H TH JH").compare_with( PokerHand("TS TH TD JH JD")) == Result.LOSS) self.assertTrue( PokerHand("4H 5H 9H TH JH").compare_with( PokerHand("JH JD TH TC 4C")) == Result.WIN) self.assertTrue( PokerHand("7C 8S 9H TH JH").compare_with( PokerHand("TS TH TD JH JD")) == Result.LOSS) self.assertTrue( PokerHand("7C 8S 9H TH JH").compare_with( PokerHand("JH JD TH TC 4C")) == Result.WIN) self.assertTrue( PokerHand("TS TH TD JH JD").compare_with( PokerHand("JH JD TH TC 4C")) == Result.WIN)