def _render_config() -> render.viewer.RenderConfig: return { "action": 0, "active": [True, True], "all_in": [False, False], "community_cards": [], "button": 0, "done": False, "hole_cards": [[poker.Card("Ah"), poker.Card("Ad")], []], "pot": 10, "payouts": [0, 0], "prev_action": (1, 10, False), "street_commits": [10, 20], "stacks": [200, 200], }
def test_invalid_init() -> None: with pytest.raises(error.InvalidRankError): poker.Card("1s") with pytest.raises(error.InvalidRankError): poker.Card("1t") with pytest.raises(error.InvalidRankError): poker.Deck(0, 0) with pytest.raises(error.InvalidRankError): poker.Deck(2, 14) with pytest.raises(error.InvalidSuitError): poker.Card("At") with pytest.raises(error.InvalidSuitError): poker.Deck(0, 1) with pytest.raises(error.InvalidSuitError): poker.Deck(5, 1)
def test_base() -> None: config: render.viewer.RenderConfig = { "action": 0, "active": [True, True], "all_in": [False, False], "community_cards": [], "button": 0, "done": False, "hole_cards": [[poker.Card("Ah")], [poker.Card("Ac")]], "pot": 10, "payouts": [0, 0], "prev_action": (1, 10, False), "street_commits": [10, 20], "stacks": [100, 100], } viewer = render.PokerViewer(0, 0, 0) with pytest.raises(NotImplementedError): viewer.render(config, sleep=5)
def test_ops() -> None: card = poker.Card("Ac") assert card & card assert card & card._int assert card | card assert card | card._int assert card << 0 == card._int assert card._int << 0 == card._int assert card >> 0 == card._int assert card._int >> 0 == card._int with pytest.raises(NotImplementedError): assert card == 0
def test_short_deck() -> None: order = ["sf", "fk", "fl", "fh", "st", "tk", "tp", "pa", "hc"] evaluator = poker.Evaluator(4, 9, 5, 0, order=order) # flush > full house hand1 = [poker.Card("8h"), poker.Card("7h")] hand2 = [poker.Card("Jd"), poker.Card("As")] comm_cards = [ poker.Card("Jh"), poker.Card("9h"), poker.Card("Ah"), poker.Card("Ac"), ] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards)
def test_trick() -> None: random.seed(42) deck = poker.Deck(4, 13).trick([poker.Card("Ah"), poker.Card("2s")]) deck.shuffle() cards = deck.draw(2) assert cards[0] == poker.Card("Ah") assert cards[1] == poker.Card("2s") deck = deck.shuffle() cards = deck.draw(2) assert cards[0] == poker.Card("Ah") assert cards[1] == poker.Card("2s") deck = deck.untrick().shuffle() cards = deck.draw(2) assert cards[0] != poker.Card("Ah") assert cards[1] != poker.Card("2s")
def test_2_card() -> None: # 1 suit evaluator = poker.Evaluator(1, 3, 2) hand1 = [poker.Card("Ks")] hand2 = [poker.Card("Qs")] comm_cards = [poker.Card("As")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # 2 suits # pair > high card evaluator = poker.Evaluator(2, 3, 2) hand1 = [poker.Card("Qs")] hand2 = [poker.Card("Ks")] comm_cards = [poker.Card("Qh")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # high card > low card hand1 = [poker.Card("Ah")] hand2 = [poker.Card("Ks")] comm_cards = [poker.Card("Qs")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards)
def test_str_repr() -> None: card = poker.Card("Ac") assert repr(card) == f"Card ({id(card)}): {card}" deck = poker.Deck(4, 13) assert repr(deck) == f"Deck ({id(deck)}): {str(deck)}"
def test_mandatory_hole_cards() -> None: evaluator = poker.Evaluator(4, 13, 5, 2) # mandatory hole cards hand1 = [ poker.Card("Th"), poker.Card("Jc"), poker.Card("2c"), poker.Card("5c") ] hand2 = [ poker.Card("Ah"), poker.Card("Qc"), poker.Card("2h"), poker.Card("5s") ] comm_cards = [ poker.Card("9s"), poker.Card("8c"), poker.Card("7d"), poker.Card("6c"), poker.Card("5d"), ] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards)
def test_1_card() -> None: # no community cards evaluator = poker.Evaluator(1, 3, 1) hand1 = [poker.Card("As")] hand2 = [poker.Card("Ks")] assert evaluator.evaluate(hand1, []) < evaluator.evaluate(hand2, []) # 1 community card hand1 = [poker.Card("As")] hand2 = [poker.Card("Ks")] comm_cards = [poker.Card("Qs")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) hand1 = [poker.Card("Qs")] hand2 = [poker.Card("Ks")] comm_cards = [poker.Card("As")] assert evaluator.evaluate(hand1, comm_cards) == evaluator.evaluate( hand2, comm_cards) # mandatory hole card evaluator = poker.Evaluator(1, 3, 1, 1) hand1 = [poker.Card("Ks")] hand2 = [poker.Card("Qs")] comm_cards = [poker.Card("As")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # 2 suits # 1 card for hand, no community cards evaluator = poker.Evaluator(2, 3, 1) hand1 = [poker.Card("Ah")] hand2 = [poker.Card("As")] assert evaluator.evaluate(hand1, []) == evaluator.evaluate(hand2, [])
def test_5_card() -> None: # 1 suit # straight > high card evaluator = poker.Evaluator(1, 13, 5) hand1 = [poker.Card("Js"), poker.Card("Ts")] hand2 = [poker.Card("Qs"), poker.Card("3s")] comm_cards = [poker.Card("7s"), poker.Card("8s"), poker.Card("9s")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # high card > low card hand1 = [poker.Card("Ks"), poker.Card("2s")] hand2 = [poker.Card("Qs"), poker.Card("3s")] comm_cards = [poker.Card("4s"), poker.Card("5s"), poker.Card("Ts")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # ace high straight > ace low straight hand = [poker.Card("As")] comm_cards1 = [ poker.Card("Ts"), poker.Card("Js"), poker.Card("Qs"), poker.Card("Ks"), ] comm_cards2 = [ poker.Card("2s"), poker.Card("3s"), poker.Card("4s"), poker.Card("5s"), ] assert evaluator.evaluate(hand, comm_cards1) < evaluator.evaluate( hand, comm_cards2) # 2 suits # straight flush > straight evaluator = poker.Evaluator(2, 13, 5) hand1 = [poker.Card("Js"), poker.Card("Ts")] hand2 = [poker.Card("Jh"), poker.Card("Th")] comm_cards = [poker.Card("7s"), poker.Card("8s"), poker.Card("9s")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # straight > two pair hand1 = [poker.Card("Js"), poker.Card("Ts")] hand2 = [poker.Card("7h"), poker.Card("8h")] comm_cards = [poker.Card("7s"), poker.Card("8s"), poker.Card("9s")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # two pair > flush hand1 = [poker.Card("7h"), poker.Card("8h")] hand2 = [poker.Card("Js"), poker.Card("Ts")] comm_cards = [poker.Card("7s"), poker.Card("8s"), poker.Card("2s")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # flush > pair hand1 = [poker.Card("8s"), poker.Card("7s")] hand2 = [poker.Card("Th"), poker.Card("2s")] comm_cards = [poker.Card("9s"), poker.Card("Ts"), poker.Card("As")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # pair > high card hand1 = [poker.Card("8s"), poker.Card("7h")] hand2 = [poker.Card("Th"), poker.Card("2s")] comm_cards = [poker.Card("8h"), poker.Card("9h"), poker.Card("As")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # 3 suits # straight flush > four of a kind evaluator = poker.Evaluator(3, 13, 5) hand1 = [poker.Card("Jh"), poker.Card("Qh")] hand2 = [poker.Card("Kc"), poker.Card("Kd")] comm_cards = [ poker.Card("Th"), poker.Card("Kh"), poker.Card("Ah"), poker.Card("Ad"), ] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # full house > three of a kind hand1 = [poker.Card("Kc"), poker.Card("Kd")] hand2 = [poker.Card("Ac"), poker.Card("Qh")] comm_cards = [ poker.Card("Th"), poker.Card("Kh"), poker.Card("Ah"), poker.Card("Ad"), ] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # three of a kind > straight hand1 = [poker.Card("Ac"), poker.Card("Qh")] hand2 = [poker.Card("Qc"), poker.Card("Jd")] comm_cards = [ poker.Card("Th"), poker.Card("Kh"), poker.Card("Ah"), poker.Card("Ad"), ] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # straight > two pair hand1 = [poker.Card("Jd"), poker.Card("Td")] hand2 = [poker.Card("Qd"), poker.Card("Kd")] comm_cards = [poker.Card("Qh"), poker.Card("Kh"), poker.Card("Ah")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # two pair > pair hand1 = [poker.Card("Qd"), poker.Card("Kd")] hand2 = [poker.Card("Qc"), poker.Card("Td")] comm_cards = [poker.Card("Qh"), poker.Card("Kh"), poker.Card("Ah")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # pair > high card hand1 = [poker.Card("8s"), poker.Card("7h")] hand2 = [poker.Card("Ah"), poker.Card("2s")] comm_cards = [poker.Card("8h"), poker.Card("9h"), poker.Card("Ts")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # 4 suits # straight flush > four of a kind evaluator = poker.Evaluator(4, 13, 5) hand1 = [poker.Card("Jh"), poker.Card("Qh")] hand2 = [poker.Card("As"), poker.Card("Ac")] comm_cards = [ poker.Card("Th"), poker.Card("Kh"), poker.Card("Ah"), poker.Card("Ad"), ] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # four of a kind > full house hand1 = [poker.Card("As"), poker.Card("Ac")] hand2 = [poker.Card("Kc"), poker.Card("Kd")] comm_cards = [poker.Card("Kh"), poker.Card("Ah"), poker.Card("Ad")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # full house > flush hand1 = [poker.Card("Kc"), poker.Card("Kd")] hand2 = [poker.Card("Th"), poker.Card("5h")] comm_cards = [ poker.Card("Kh"), poker.Card("Ah"), poker.Card("Ad"), poker.Card("2h"), ] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # flush > straight hand1 = [poker.Card("Th"), poker.Card("5h")] hand2 = [poker.Card("Jd"), poker.Card("Td")] comm_cards = [poker.Card("Qh"), poker.Card("Kh"), poker.Card("Ah")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # straight > three of a kind hand1 = [poker.Card("Jd"), poker.Card("Td")] hand2 = [poker.Card("Qd"), poker.Card("Qc")] comm_cards = [poker.Card("Qh"), poker.Card("Kh"), poker.Card("Ah")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # three of a kind > two pair hand1 = [poker.Card("Qd"), poker.Card("Qc")] hand2 = [poker.Card("Kd"), poker.Card("Ad")] comm_cards = [poker.Card("Qh"), poker.Card("Kh"), poker.Card("Ah")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # two pair > pair hand1 = [poker.Card("9h"), poker.Card("Qh")] hand2 = [poker.Card("8s"), poker.Card("7s")] comm_cards = [poker.Card("9s"), poker.Card("Qs"), poker.Card("8d")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # pair > high card hand1 = [poker.Card("8s"), poker.Card("7h")] hand2 = [poker.Card("Ah"), poker.Card("2s")] comm_cards = [poker.Card("8h"), poker.Card("9h"), poker.Card("Ts")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards)
def test_3_card() -> None: # 1 suit # straight > high card evaluator = poker.Evaluator(1, 13, 3) hand1 = [poker.Card("Js")] hand2 = [poker.Card("Qs")] comm_cards = [poker.Card("9s"), poker.Card("Ts")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # high card > low card hand1 = [poker.Card("Ks")] hand2 = [poker.Card("Qs")] comm_cards = [poker.Card("5s"), poker.Card("Ts")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # ace high straight > ace low straight hand = [poker.Card("As")] comm_cards1 = [poker.Card("Qs"), poker.Card("Ks")] comm_cards2 = [poker.Card("2s"), poker.Card("3s")] assert evaluator.evaluate(hand, comm_cards1) < evaluator.evaluate( hand, comm_cards2) # 2 suits # straight flush > straight evaluator = poker.Evaluator(2, 13, 3) hand1 = [poker.Card("Js")] hand2 = [poker.Card("Jc")] comm_cards = [poker.Card("9s"), poker.Card("Ts")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # straight > pair hand1 = [poker.Card("Jc")] hand2 = [poker.Card("9c")] comm_cards = [poker.Card("9s"), poker.Card("Ts")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # pair > flush hand1 = [poker.Card("7c")] hand2 = [poker.Card("As")] comm_cards = [poker.Card("7s"), poker.Card("Ts")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # flush > high card hand1 = [poker.Card("9s")] hand2 = [poker.Card("Ac")] comm_cards = [poker.Card("7s"), poker.Card("Ts")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # 4 suits # straight flush > straight evaluator = poker.Evaluator(4, 13, 3) hand1 = [poker.Card("Js")] hand2 = [poker.Card("Jc")] comm_cards = [poker.Card("9s"), poker.Card("Ts")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # straight > pair hand1 = [poker.Card("Jc")] hand2 = [poker.Card("9c")] comm_cards = [poker.Card("9s"), poker.Card("Ts")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # flush > pair hand1 = [poker.Card("As")] hand2 = [poker.Card("7c")] comm_cards = [poker.Card("7s"), poker.Card("Ts")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards) # pair > high card hand1 = [poker.Card("7c")] hand2 = [poker.Card("Ac")] comm_cards = [poker.Card("7s"), poker.Card("Ts")] assert evaluator.evaluate(hand1, comm_cards) < evaluator.evaluate( hand2, comm_cards)
def test_render() -> None: viewer = render.ASCIIViewer(2, 2, 5) config: render.viewer.RenderConfig = { "action": 0, "active": [True, True], "all_in": [False, False], "community_cards": [], "button": 0, "done": False, "hole_cards": [ [poker.Card("Ah"), poker.Card("Ad")], [poker.Card("Kh"), poker.Card("Kd")], ], "pot": 10, "payouts": [0, 0], "prev_action": (1, 10, False), "street_commits": [10, 20], "stacks": [100, 100], } stdout = io.StringIO() with redirect_stdout(stdout): viewer.render(config) string = stdout.getvalue() sub_strings = [f"A{chr(9829)}", f"A{chr(9830)}", "??", "20"] not_sub_strings = [f"K{chr(9829)}", f"K{chr(9830)}"] assert all(sub_string in string for sub_string in sub_strings) assert all(sub_string not in string for sub_string in not_sub_strings) config["active"] = [True, False] stdout = io.StringIO() with redirect_stdout(stdout): viewer.render(config) string = stdout.getvalue() sub_strings = [f"A{chr(9829)}", f"A{chr(9830)}", "20"] not_sub_strings = ["??"] assert all(sub_string in string for sub_string in sub_strings) assert all(sub_string not in string for sub_string in not_sub_strings) config["active"] = [True, True] config["all_in"] = [False, True] stdout = io.StringIO() with redirect_stdout(stdout): viewer.render(config) string = stdout.getvalue() sub_strings = [f"A{chr(9829)}", f"A{chr(9830)}", "20", "A"] not_sub_strings = [f"K{chr(9829)}", f"K{chr(9830)}"] assert all(sub_string in string for sub_string in sub_strings) assert all(sub_string not in string for sub_string in not_sub_strings) config["all_in"] = [False, False] config["prev_action"] = (1, 200, False) stdout = io.StringIO() with redirect_stdout(stdout): viewer.render(config) string = stdout.getvalue() action_string = "Player 2 bet 200 Action on Player 1" sub_strings = [f"A{chr(9829)}", f"A{chr(9830)}", "20", action_string] not_sub_strings = [f"K{chr(9829)}", f"K{chr(9830)}"] assert all(sub_string in string for sub_string in sub_strings) assert all(sub_string not in string for sub_string in not_sub_strings) config["prev_action"] = (1, 0, True) stdout = io.StringIO() with redirect_stdout(stdout): viewer.render(config) string = stdout.getvalue() action_string = "Player 2 folded Action on Player 1" sub_strings = [f"A{chr(9829)}", f"A{chr(9830)}", "20", action_string] not_sub_strings = [f"K{chr(9829)}", f"K{chr(9830)}"] assert all(sub_string in string for sub_string in sub_strings) assert all(sub_string not in string for sub_string in not_sub_strings) config["prev_action"] = (1, 0, False) stdout = io.StringIO() with redirect_stdout(stdout): viewer.render(config) string = stdout.getvalue() action_string = "Player 2 checked Action on Player 1" sub_strings = [f"A{chr(9829)}", f"A{chr(9830)}", "20", action_string] not_sub_strings = [f"K{chr(9829)}", f"K{chr(9830)}"] assert all(sub_string in string for sub_string in sub_strings) assert all(sub_string not in string for sub_string in not_sub_strings) config["prev_action"] = None config["payouts"] = [2, 2] config["done"] = True stdout = io.StringIO() with redirect_stdout(stdout): viewer.render(config) string = stdout.getvalue() action_string = "Players 1, 2 won 2, 2 respectively" sub_strings = [ f"A{chr(9829)}", f"A{chr(9830)}", f"K{chr(9829)}", f"K{chr(9830)}", action_string, ] not_sub_strings = [] assert all(sub_string in string for sub_string in sub_strings) assert all(sub_string not in string for sub_string in not_sub_strings) config["payouts"] = [10, 0] config["done"] = True stdout = io.StringIO() with redirect_stdout(stdout): viewer.render(config) string = stdout.getvalue() action_string = "Player 1 won 10" sub_strings = [ f"A{chr(9829)}", f"A{chr(9830)}", f"K{chr(9829)}", f"K{chr(9830)}", action_string, ] not_sub_strings = [] assert all(sub_string in string for sub_string in sub_strings) assert all(sub_string not in string for sub_string in not_sub_strings)