def setUp(self): self.rank_hands = { poker.Hand.HIGH_CARD: poker.Hand(['5D', '8C', '9S', 'JS', 'AC']), poker.Hand.ONE_PAIR: poker.Hand(['5H', '5C', '6S', '7S', 'KD']), poker.Hand.TWO_PAIRS: poker.Hand(['2D', '3C', '2H', '7S', '7H']), poker.Hand.THREE_OF_A_KIND: poker.Hand(['AS', '2D', 'TH', 'AH', 'AC']), poker.Hand.STRAIGHT: poker.Hand(['9D', '5S', '7H', '8S', '6S']), poker.Hand.FLUSH: poker.Hand(['7S', 'TS', 'KS', '3S', 'JS']), poker.Hand.FULL_HOUSE: poker.Hand(['6S', '2D', '2H', '6D', '6H']), poker.Hand.FOUR_OF_A_KIND: poker.Hand(['7S', '7H', '7D', '2H', '7C']), poker.Hand.STRAIGHT_FLUSH: poker.Hand(['JS', '8S', 'QS', 'TS', '9S']), poker.Hand.ROYAL_FLUSH: poker.Hand(['QH', 'TH', 'JH', 'KH', 'AH']), }
def test_get_hand_type(self): for i in range(int(len(self.deck) / 5)): hand = poker.Hand(self.deck) if i in self.royal_flush: self.assertEqual(hand.get_hand_type(), "royal_flush", msg=hand) if i in self.straight_flush: self.assertEqual(hand.get_hand_type(), "straight_flush", msg=hand) if i in self.four_of_a_kind: self.assertEqual(hand.get_hand_type(), "four_of_a_kind", msg=hand) if i in self.full_house: self.assertEqual(hand.get_hand_type(), "full_house", msg=hand) if i in self.flush: self.assertEqual(hand.get_hand_type(), "flush", msg=hand) if i in self.straight: self.assertEqual(hand.get_hand_type(), "straight", msg=hand) if i in self.three_of_a_kind: self.assertEqual(hand.get_hand_type(), "three_of_a_kind", msg=hand) if i in self.two_pair: self.assertEqual(hand.get_hand_type(), "two_pair", msg=hand) if i in self.pair: self.assertEqual(hand.get_hand_type(), "pair", msg=hand) if i == 0: self.assertEqual(hand.get_hand_type(), "high_card", msg=hand)
def calculate_pokerval(_cards): """ Calculate/retrieve a pokerval from a set of 5 or more cards. Also return the 'index' used for db storage. """ global pokerval_cache, pokerval_cachehits, pokerval_cachemisses cards = poker.normalize_cards(_cards) try: index = poker.make_stringindex(cards) try: pokerval = pokerval_cache[index] pokerval_cachehits += 1 return index, pokerval except KeyError: pokerval_cachemisses += 1 pass pokerval = 0 if len(cards) == 5: pokerval = poker.CalculatingHand(cards).getpokerval() elif len(cards) > 5: for fivecards in xuniqueCombinations(cards, 5): hand = poker.Hand(fivecards) pokerval = max(pokerval, hand.getpokerval()) else: raise ValueError("Not enough cards!") pokerval_cache[index] = pokerval except KeyError: errstr = "Hand not in database: %s %s, <%s>, %s" % (format_cards( _cards), format_cards(cards), index, reverse_stringindex(index)) raise KeyError(errstr) except: raise return index, pokerval
def __init__(self, user, stack, place=0): self.still_in = True self.chips_in = 0 self.side_potential = 0 self.user = user self.name = user.display_name self.place = place self.stack = stack self.hand = poker.Hand()
def t(self, has, hand_indexes): """ has: method name of the type of hand to test hand_indexes: list of hands that contain the type of hand """ hands = [] for i in range(int(len(self.deck) / 5)): hand = poker.Hand(self.deck) if getattr(hand, has)(): hands.append(hand) if i not in hand_indexes: assert False, str(hand) + "\n" + str(i) self.assertEqual(len(hands), len(hand_indexes))
def main(): global iterations global results start = time() for i in range(iterations): deck = poker.Deck() deck.shuffle() results[poker.Hand(deck).get_hand_type()] += 1 total_results = sum(results.values()) for hand_type in poker.Hand.get_hand_types(): print( str(hand_type).ljust(15) + "\t" + "%.5f%%" % (results[hand_type] / total_results * 100, ) + "\t" + str(results[hand_type])) print("\nTotal Hands:\t\t" + str(total_results)) print("Script completed in", time() - start, "seconds.") with open("results.json", "w") as f: json.dump(results, f)
import poker as p deck = p.Deck.standard_52_card_deck(False) cards = [deck["5C"], deck["6C"], deck["7C"], deck["8C"], deck["9C"]] hand = p.Hand(cards) print(hand, "(should be: 5C 6C 7C 8C 9C, Straight flush, strength=80900000000)") cards = [deck["TC"], deck["JC"], deck["AC"], deck["KC"], deck["QC"]] hand = p.Hand(cards) print(hand, "(should be: Royal Straight flush, strength=81400000000)") cards = [deck["5C"], deck["2C"], deck["3C"], deck["AC"], deck["4C"]] hand = p.Hand(cards) print(hand, "(should be: Straight flush, strength=80500000000)") cards = [deck["5C"], deck["5D"], deck["5H"], deck["5S"], deck["AC"]] hand1 = p.Hand(cards) print(hand1, "(should be: 5C 5D 5H 5S AC, Four of a kind, strength=70514000000)") cards = [deck["TC"], deck["TD"], deck["TH"], deck["JH"], deck["JC"]] hand2 = p.Hand(cards) print(hand2, "(should be: TC TD TH JH JC, Full house, strength=61011000000)") cards = [deck["TS"], deck["TC"], deck["TD"], deck["JS"], deck["JH"]] hand3 = p.Hand(cards) print(hand3, "(should be: TS TC TD JS JH, Full house, strength=61011000000)") print(hand3 > hand2)
] discardButtonShadowPositions = [ (75, 355, DISCARD_BUTTON_SHADOW_WIDTH, DISCARD_BUTTON_SHADOW_HEIGHT), (225, 355, DISCARD_BUTTON_SHADOW_WIDTH, DISCARD_BUTTON_SHADOW_HEIGHT), (375, 355, DISCARD_BUTTON_SHADOW_WIDTH, DISCARD_BUTTON_SHADOW_HEIGHT), (525, 355, DISCARD_BUTTON_SHADOW_WIDTH, DISCARD_BUTTON_SHADOW_HEIGHT), (675, 355, DISCARD_BUTTON_SHADOW_WIDTH, DISCARD_BUTTON_SHADOW_HEIGHT) ] newHandButtonRect = (50, 400, 100, 25) drawButtonRect = (160, 400, 77, 27) discardAllButtonRect = (325, 400, 75, 25) holdAllButtonRect = (245, 400, 75, 25) deck = poker.newDeck() playerHand = poker.Hand(poker.drawHand(deck)) discards = set() drawNewHandButtonEnabled = True holdButtonsEnabled = False drawButtonEnabled = False discardAllButtonEnabled = False holdAllButtonEnabled = False def renderCard(position, card): suit = suitUnicodeMap[card.suit] if card.rank > 10: rank = faceCardRankMap[str(card.rank)] else:
def test_compare_two_pair(self): low = poker.Hand(['7S', '9D', 'JH', '7D', 'JS']) high = poker.Hand(['AS', 'AD', '5C', '2D', '2H']) self.assertTrue(low < high)
def test_compare_ace_low_straight(self): low = poker.Hand(['AH', '2S', '3C', '4S', '5S']) high = poker.Hand(['2S', '3C', '4S', '5S', '6S']) self.assertTrue(low < high)
def test_ace_low_straight(self): hand = poker.Hand(['AH', '2S', '3C', '4S', '5S']) self.assertEqual([hand.rank(), hand.values()], [poker.Hand.STRAIGHT, [5, 4, 3, 2, 1]])
def test_ace_high_straight(self): hand = poker.Hand(['AH', 'KS', 'QC', 'JS', 'TS']) self.assertEqual([hand.rank(), hand.values()], [poker.Hand.STRAIGHT, [14, 13, 12, 11, 10]])
import poker as p d = p.Deck.standard_52_card_deck() print(d) for c in d: print(repr(c), c) h = p.Hand([d['2c']]) print(h._strength) print(h.strength) # h = p.Hand.best_from_cards(d.cards) # print(h)