def check_hand_ranking_method(method: str): """ Helper function to check the is_{ranking} methods. """ for row in TEST_HANDS.itertuples(): # Remove the "is_" from the beginning of the method name to # compare with the ranking from the file. If is the same # ranking that the method is testing, the result must be # true, otherwise must be false. if row.ranking == method[3:]: assert getattr(bluff.Hand(row.hand), method)() else: assert not getattr(bluff.Hand(row.hand), method)()
def test_repr(): """ Test class' __repr__. """ for row in TEST_HANDS.itertuples(): # Since the repr from Hand sorts the hand. Comparing the # repr to the hand string in the test_hands.csv would fail. # The way I decided to check if it was working was to # construct another Hand from the repr. If the repr is # working correctly, it must construct a Hand instance with # the same repr as the hand instance from the hand created # by with the test_hand.csv. assert repr(bluff.Hand(repr(bluff.Hand(row.hand)))) == repr( bluff.Hand(row.hand) )
def test_winner(): """ Test the method winner. """ test_hands = ( bluff.Hand("As Ah 4d Tc Js"), bluff.Hand("3s 4h 5d 6c 7s"), bluff.Hand("Qs Ah 4d Tc Js"), ) winner = 1 n_starting_cards = 5 rnd = bluff.Round(players=players, n_starting_cards=n_starting_cards) # Access the players hand to force they have the test hands. for player, test_hand in zip(rnd.players, test_hands): player.hand = test_hand assert rnd.winner() == winner
def test_create_empty_hand(): """ Test the creation of an empty hand. """ hand = bluff.Hand() assert hand.ranks == [] assert hand.suits == [] assert hand.numerical_ranks == [] assert hand.hex_ranks == [] assert hand.value == 0
def eval_combinations(hand, board): """ Evaluate every possible combination of hole cards and board. Args: hand: Hole cards. board: Board cards. Returns: Highest hand value. """ cards = np.concatenate([[hand[:2], hand[2:]], board]) combos = itertools.combinations(cards, r=5) return np.max([bluff.Hand("".join(combo)).value for combo in combos])
def eval_directly(hand, board): """ Evaluate all cards at the same time. Please notice that this function fails at evaluating ties. Args: hand: Hole cards. board: Board cards. Returns: Highest hand value. """ cards = np.concatenate([[hand[:2], hand[2:]], board]) return bluff.Hand("".join(cards)).value
def test_string_arguments(): """" Test creation of a hand by string arguments. """ reference = bluff.Hand( bluff.Card("Ad"), bluff.Card("Ks"), bluff.Card("Tc"), bluff.Card("6c"), bluff.Card("2h"), ) alternatives = [ bluff.Hand("Ad", "Ks", "Tc", "6c", "2h"), bluff.Hand("AdKsTc6c2h"), bluff.Hand("Ad Ks Tc 6c 2h"), bluff.Hand("Ad, Ks, Tc, 6c, 2h"), bluff.Hand("Ad,Ks,Tc,6c,2h"), ] for alt in alternatives: assert reference.value == alt.value
def test_hand_ranking_name(): """ Test name property from Hand class. """ for row in TEST_HANDS.itertuples(): assert bluff.Hand(row.hand).name == row.ranking
def test_value(): """ Test the hand value. """ for row in TEST_HANDS.itertuples(): assert bluff.Hand(row.hand).value == int(row.value, 16)