def test(): "Test cases for the functions in poker program" sf = "6C 7C 8C 9C TC".split() # Straight Flush fk = "9D 9H 9S 9C 7D".split() # Four of a Kind fh = "TD TC TH 7C 7D".split() # Full House tp = "5S 5D AC AS KS".split() # Two Pair s1 = "AC 2S 3C 4D 5D".split() # A-5 Straight s2 = "2S 3C 4D 5D 6S".split() # 2-6 Straight ah = "AC 2S 9C 4D 6D".split() # A High sh = "7C 2S 6C 3D 5D".split() # 7 High assert poker([s1, s2, ah, sh]) == [s2] assert poker([s1, ah, sh]) == [s1] fkranks = card_ranks(fk) tpranks = card_ranks(tp) assert kind(4, fkranks) == 9 assert kind(3, fkranks) is None assert kind(2, fkranks) is None assert kind(1, fkranks) == 7 assert two_pair(fkranks) is None assert two_pair(tpranks) == (14, 5) assert poker([sf, fk, fh]) == [sf] assert poker([fk, fh]) == [fk] assert poker([fh, fh]) == [fh, fh] assert poker([sf]) == [sf] assert poker([sf] + 99*[fh]) == [sf] assert hand_rank(sf) == (8, 10) assert hand_rank(fk) == (7, 9, 7) assert hand_rank(fh) == (6, 10, 7) return 'tests pass'
def poker_winner(): size=0; announce = "*** WINNER ***\n-------------\n"; msg = ""; for key in playerNames.keys(): hand = playerHands.get(key); player = playerNames.get(key); result = poker.hand_rank(hand); size = len(result); if size == 2: if result[0] == 0: #player has nothing ( 0 ) msg = player + "has nothing.\n "; elif result[0] == 4: msg = player +" has Straigt! " + str(result[1])+"\n"; elif size == 3: if result[0] == 1: #One pairs msg = player + " has One pair of " + str(result[1])+"\n"; elif result[0] == 2: #Two pairs msg = player + " has Two pairs of" + str(result[1])+"\n"; elif result[0] == 3: #Three of a kind msg = player + " has Three of a kind! " + str(result[1])+"\n"; #Take away players cards del playerHands[key]; announce = announce+msg; #append message #send message broadcast(announce);
def test_hand_rank_tester_14(self): '''testcase hand_rank normal high_card hc = ['4S', '3H', '9D', '8C', 'TS'] ''' hc = ['4S', '3H', '9D', '8C', 'TS'] actual = poker.hand_rank(hc)[0] expected = 0 self.assertEqual (actual,expected)
def test_hand_rank_tester_13(self): '''testcase hand_rank normal one_pair op = ['5S', '3H', '9D', '8C', '8S'] ''' op = ['5S', '3H', '9D', '8C', '8S'] actual = poker.hand_rank(op)[0] expected = 1 self.assertEqual (actual,expected)
def test_hand_rank_tester_12(self): '''testcase hand_rank normal two_pair tp = ['5S', '5H', '9D', '8C', '8S'] ''' tp = ['5S', '5H', '9D', '8C', '8S'] actual = poker.hand_rank(tp)[0] expected = 2 self.assertEqual (actual,expected)
def test_hand_rank_tester_10(self): '''testcase hand_rank normal straight st = ['JC', 'TC', '9C', '8S', '7C'] ''' st = ['JC', 'TC', '9C', '8S', '7C'] actual = poker.hand_rank(st)[0] expected = 4 self.assertEqual (actual,expected)
def test_hand_rank_tester_9(self): '''testcase hand_rank normal flush fl = ['JC', '5C', '9C', '8C', '7C'] ''' fl = ['JC', '5C', '9C', '8C', '7C'] actual = poker.hand_rank(fl)[0] expected = 5 self.assertEqual (actual,expected)
def test_hand_rank_tester_8(self): '''testcase hand_rank highest full_house fh = ['AS', 'AC', 'AD', 'KH', 'KS'] ''' fh = ['AS', 'AC', 'AD', 'KH', 'KS'] actual = poker.hand_rank(fh)[0] expected = 6 self.assertEqual (actual,expected)
def test_hand_rank_tester_7(self): '''testcase hand_rank normal full_house fh = ['5S', '5H', '5D', '8C', '8S'] ''' fh = ['5S', '5H', '5D', '8C', '8S'] actual = poker.hand_rank(fh)[0] expected = 6 self.assertEqual (actual,expected)
def test_hand_rank_tester_6(self): '''testcase hand_rank lowest four_of_a_kind fk = ['2S','2C','2D','2H','3S'] ''' fk = ['2S','2C','2D','2H','3S'] actual = poker.hand_rank(fk)[0] expected = 7 self.assertEqual (actual,expected)
def test_hand_rank_tester_5(self): '''testcase hand_rank highest four_of_a_kind fk = ['AS','AC','AD','AH','KS'] ''' fk = ['AS','AC','AD','AH','KS'] actual = poker.hand_rank(fk)[0] expected = 7 self.assertEqual (actual,expected)
def test_hand_rank_tester_11(self): '''testcase hand_rank normal three_of_a_kind tk = ['5S', '7H', '8D', '8C', '8S'] ''' tk = ['5S', '7H', '8D', '8C', '8S'] actual = poker.hand_rank(tk)[0] expected = 3 self.assertEqual (actual,expected)
def test_hand_rank_tester_4(self): '''testcase hand_rank normal four_of_a_kind fk = ['5S', '5H', '5D', '5C', 'KS'] ''' fk = ['5S', '5H', '5D', '5C', 'KS'] actual = poker.hand_rank(fk)[0] expected = 7 self.assertEqual (actual,expected)
def test_hand_rank_tester_1(self): '''testcase hand_rank high straight_flush sf = ['JS', 'TS', '9S', '8S', '7S'] ''' sf = ['JS', 'TS', '9S', '8S', '7S'] actual = poker.hand_rank(sf)[0] expected = 8 self.assertEqual (actual,expected)
def test_hand_rank_tester_3(self): '''testcase hand_rank low straight_flush sf = ['2C','3C','4C','5C','6C'] ''' sf = ['2C','3C','4C','5C','6C'] actual = poker.hand_rank(sf)[0] expected = 8 self.assertEqual (actual,expected)
def hand_percentages(n=700*1000): "Sample n random hands and print a table of frequencies for each type of hand" counts = [0] * 9 for i in range(n/10): for hand in deal(10): ranking = hand_rank(hand)[0] counts[ranking] += 1 for i in reversed(range(9)): print "%14s: %6.3f %%" % (hand_names[i], 100.*counts[i]/n)
def test_hand_rank(self): """Test hand_rank with straight flush hand ['JC', 'TC', '9C', '8C', '7C'] """ sf = ['JC', 'TC', '9C', '8C', '7C'] actual = poker.hand_rank(sf) expected = 8,10 self.assertEqual(actual, expected)
def hand_percentages(n=1000000): counts = [0] * 9 for i in range(n / 10): for hand in deal(10): ranking = hand_rank(hand)[0] counts[ranking] += 1 for i in reversed(range(9)): print "%14s: %6.3f %%" % (hand_names[i], 100. * counts[i] / n)
def hand_percentages(n=700*1000): "Sample n random hands and print a table of percentages for each hand." counts = [0] * 9 for i in range(n/10): for hand in deal(10): ranking = pkr.hand_rank(hand)[0] counts[ranking] += 1 for i in reversed(range(9)): print("%14s: %6.3f %%" % (hand_names[i], 100.*counts[i]/n))
def percentages(n=700 * 1000): """Sample n random hands and print a table of percentages for each type of hand""" counts = [0] * 10 for _ in range(n // 10): for hand in deal(10): ranking = hand_rank(hand)[0] counts[ranking] += 1 for i in reversed(range(10)): print("%d: %6.3f" % (i, 100. * counts[i] / n))
def hand_percentages(n=1000000): """ sample n random hands and print percentages for each hand """ counts = [0]*9 for i in range(n/10): hands = deal(10) for hand in hands: rank = hand_rank(hand)[0] counts[rank] += 1 for i in reversed(range(9)): print "%20s: %6.3f %%" % (hand_names[i], (100.*counts[i]/n))
def test_hand_rank_2(self): """ Test hand_rank with four of a kind hand ['6H', '6S', '6D', '6C', 'KS'] """ fk = ['6H', '6S', '6D', '6C', 'KS'] actual = poker.hand_rank(fk) expected = 7,6 self.assertEqual(actual, expected)
def test_check_winner2(self): '''testcase check type card of winner in One pairs Pl1=['TH', '2H', '5H', 'QC', '2S'] Pl2=['3D', '9S', 'AH', '6C', '9C'] Winner is who has card : ['3D', '9S', 'AH', '6C', '9C'] "Win by One pairs" ''' Pl1=['TH', '2H', '5H', 'QC', '2S'] Pl2=['3D', '9S', 'AH', '6C', '9C'] actual = (poker.hand_rank(poker.poker([Pl1,Pl2])[0])[0]==3) expected = False self.assertEqual (actual,expected)
def test_check_winner1(self): '''testcase check type card of winner in Three of a kinds Pl1=['TH', '2H', '5H', 'QC', '2S'] Pl2=['3D', '9S', '9H', '6C', '9C'] Winner is who has card : ['3D', '9S', '9H', '6C', '9C'] "Win by Three of a kinds" ''' Pl1=['TH', '2H', '5H', 'QC', '2S'] Pl2=['3D', '9S', '9H', '6C', '9C'] actual = (poker.hand_rank(poker.poker([Pl1,Pl2])[0])[0]==3) expected = True self.assertEqual (actual,expected)
def hand_percenteges(n=700 * 1000): ''' Sample n random hands and print a table of percentages for each type of hand. ''' hand_names = ('High Card', 'Pair', '2 Pair', '3 Kind', 'Straight', 'Flush', 'Full House', '4 Kind', 'Straight Flush') counts = [0] * 9 for i in range(n // 10): for poker.hand in deal.deal(10): counts[poker.hand_rank(poker.hand)[0]] += 1 for j in reversed(range(9)): print('{:14} {:6.3%}'.format(hand_names[j], counts[j] / n))
def hand_percentage(n=700*1000): """Sample n random hands and print a table of percentages for each type of hand. Args: n: int indicates total number of hands to be dealt. """ counts = [0]*9 for i in range(n/10): for hand in deal(10): ranking = hand_rank(hand)[0] counts[ranking] += 1 for i in reversed(range(9)): print "%14s: %6.3f %%" % (hand_names[i], 100.*counts[i]/n)
def test_hand_rank(): sf = "6C 7C 8C 9C TC".split() fk = "9D 9H 9S 9C 7D".split() fh = "TD TC TH 7C 7D".split() flush = "TH 8H 7H 5H 3H".split() straight = "JD TS 9C 8D 7H".split() three_kind = "7C 7D 7H 5H 2S".split() two_pair = "JC JD 3H 3S KS".split() pair = "2S 2C JC 6S 3H".split() high_card = "7H 5S 4D 3C 2H".split() assert hand_rank(sf) == (9, (10, 9, 8, 7, 6)) assert hand_rank(fk) == (7, (9, 7)) assert hand_rank(fh) == (6, (10, 7)) assert hand_rank(flush) == (5, (10, 8, 7, 5, 3)) assert hand_rank(straight) == (4, (11, 10, 9, 8, 7)) assert hand_rank(three_kind) == (3, (7, 5, 2)) assert hand_rank(two_pair) == (2, (11, 3, 13)) assert hand_rank(pair) == (1, (2, 11, 6, 3)) assert hand_rank(high_card) == (0, (7, 5, 4, 3, 2))
def sangriento(n, texas_holdem=True): if texas_holdem: hands = texas_holdem_deal(n) else: hands = deal(n) for hand in hands: print hand winner = poker(hands) ranking = hand_rank(winner[0])[0] print "Winner hand(s):" print hand_names[ranking] return winner
def hand_percentages(n=700 * 1000): """ Sample n random hands and print a table of percentages for each hand. """ counts = [0] * 9 for i in range(n // 10): for hand in deal(10): ranking = hand_rank(hand)[0] counts[ranking] += 1 hand_names = [ "Straight Flush", "4 Kind", "Full House", "Flush", "Straight", "3 Kind", "2 Pair", "Pair", "High Card", ] for i in reversed(range(9)): print(f"{hand_names[i]}: {round(counts[i]/n * 100, 2)}%")
def test_hand_rank_flush(self): cards = [('hearts', '3'),('hearts', '8'),('hearts', '9'),('hearts', '10'),('hearts', '2')] hand_rank_1 = hand_rank(cards) self.assertEqual(hand_rank_1, 'flush')
def test_hand_rank(arg, expected): assert hand_rank(arg) == expected
hands_per_hour = 30 hours_per_session = 6 # hand_to_beat = ["9c", "8c", "7c", "6c", "Tc"] hand_to_beat = ["9c", "9h", "9s", "As", "Ac"] hothands_per_session = [] for s in range(sessions): hothand = [] for i in range(hours_per_session): for h in range(hands_per_hour): hothands_hour = [] pockets, community = poker.holdem_deal(num_players) tablehands = [] for p in pockets: # compute each player's best hand from hand_pool playershands = [p+list(c) for c in itertools.combinations(community, 3)] playersbest = max(playershands, key=poker.hand_rank) tablehands.append(playersbest) tablebest = max(tablehands, key=poker.hand_rank) if poker.hand_rank(tablebest) > poker.hand_rank(hand_to_beat): hothands_hour.append(tablebest) hothand.append(hothands_hour) has_hothand = [0 if not h else 1 for h in hothand] num_hothands = sum(has_hothand) hothands_per_session.append(num_hothands) with(open("hothands.csv", "w+")) as f: writer = csv.writer(f) for h in hothands_per_session: writer.writerow([h])
def test_hand_rank_straight(self): cards = [('spades', '7'),('hearts', '8'),('hearts', '9'),('hearts', '10'),('clubs', 'J')] hand_rank_1 = hand_rank(cards) self.assertEqual(hand_rank_1, 'straight')
def test_hand_rank_straight_flush(self): cards = [('hearts', '7'),('hearts', '8'),('hearts', '9'),('hearts', '10'),('hearts', 'J')] hand_rank_1 = hand_rank(cards) self.assertEqual(hand_rank_1, 'straight-flush')
def test_poker_example_15(self): fh = ["5S", "5H", "5D", "6C", "6S"] actual = poker.hand_rank(fh) expected = 6.03 self.assertEqual(actual, expected)
def test_poker_example_13(self): fk = ["5S", "5H", "5D", "5C", "KS"] actual = poker.hand_rank(fk) expected = 7.03 self.assertEqual(actual, expected)
def test_poker_example_6(self): sf = ["JC", "TC", "9C", "8C", "QC"] actual = poker.hand_rank(sf) expected = 8.10 self.assertEqual(actual, expected)