コード例 #1
0
def test_when_describe_hand_and_bad_hand_type_then_raise_error():
    with raises(
            ValueError,
            match=
            "Hand type provided 'WINNING_HAND_TYPE' is not an acceptable value"
    ):
        describe_hand("Texas Holdem", "WINNING_HAND_TYPE")
コード例 #2
0
def test_when_describe_hand_and_too_many_kwargs_passed_then_raise_error(
        game_type, hand_type, kwargs):
    with raises(
            ValueError,
            match=
            f"Kwargs object '{kwargs}' contains additional keys from what is expected"
    ):
        describe_hand(game_type, hand_type, **kwargs)
コード例 #3
0
def test_when_describe_hand_and_incorrect_kwargs_passed_then_raise_error(
        game_type, hand_type, kwargs):
    with raises(
            ValueError,
            match=
            f"Kwargs object '{kwargs}' does not contain all keys required for "
            f"this method"):
        describe_hand(game_type, hand_type, **kwargs)
コード例 #4
0
    def rank_hands(self, players_hands: Dict[str, List[Card]]):
        """
        Public method that will rank players texas hold'em hands against each other

        :param players_hands: Dict in format of
        {
            "PLAYER_NAME": [LIST_OF_CARDS]
        }

        :return: dict in the following format:
        {
            <RANK>: {
                "players": ["LIST OF PLAYER NAMES"],
                "hand_description": "HAND_DESCRIPTION"
            }
        }
        """

        player_hands_by_rank = self._group_player_hands_by_rank(players_hands)
        ranks = sorted(list(player_hands_by_rank.keys()))

        current_rank = 0
        ranked_player_hands = dict()

        for rank in ranks:
            hands = list(player_hands_by_rank[rank].values())
            subranked_hands = rank_hand_type(
                GAME_TYPE_TEXAS_HOLDEM,
                self._hand_rankings[rank - 1][HAND_TITLE],
                hands=hands,
            )
            subranked_players = self._link_subranked_hands_to_players(
                subranked_hands, players_hands
            )

            for players in subranked_players.values():
                current_rank += 1
                hand_desc = describe_hand(
                    GAME_TYPE_TEXAS_HOLDEM,
                    self._hand_rankings[rank - 1][HAND_TITLE],
                    hand=players_hands[players[0]],
                )
                ranked_player_hands[current_rank] = {
                    "players": players,
                    "hand_description": hand_desc,
                }

        return ranked_player_hands
コード例 #5
0
    def find_best_hand(self, hole_cards: List[Card], board_cards: List[Card]):
        """
        Abstract method to implement to find a players best hand.

        :param hole_cards: List of card objects, representing the players hole cards
        :param board_cards: List of card objects, representing the board cards available to use.
        :return: dictionary of player hand information. including at least the following keys:
        {
            "best_hand": List of card objects representing the players best hand
            "hand_title": Str the english title of the best hand type the player has (Straight, Flush, Two Pair, etc)
            "hand_rank": Int ranking of the hand type, with 1 signifying the best type of hand (e.g. straight flush
                         would have a ranking of 1 in texas holdem)
        }
        child classes implementing this abstract method can choose to expand the dictionary where
        appropriate but must at minimum have the above keys
        """

        hand_size = (
            5
            if len(hole_cards) + len(board_cards) >= 5
            else len(hole_cards) + len(board_cards)
        )
        all_hands = get_all_combinations(hole_cards, board_cards, hand_size)
        for hand_ranking in self._hand_rankings:
            matched_hands = [
                hand
                for hand in all_hands
                if hand_test(
                    GAME_TYPE_TEXAS_HOLDEM, hand_ranking[HAND_TITLE], hand=hand
                )
            ]

            if matched_hands:
                best_hand = rank_hand_type(
                    GAME_TYPE_TEXAS_HOLDEM,
                    hand_ranking[HAND_TITLE],
                    hands=matched_hands,
                )[1][0]
                return {
                    BEST_HAND: best_hand,
                    HAND_TITLE: hand_ranking[HAND_TITLE],
                    HAND_RANK: hand_ranking[HAND_RANK],
                    HAND_DESCRIPTION: describe_hand(
                        GAME_TYPE_TEXAS_HOLDEM, hand_ranking[HAND_TITLE], hand=best_hand
                    ),
                }
コード例 #6
0
def test_when_describe_hand_and_bad_game_type_then_raise_error():
    with raises(
            ValueError,
            match=
            "Game type provided 'BAD_GAME_TYPE' is not an acceptable value"):
        describe_hand("BAD_GAME_TYPE", "Straight Flush")
コード例 #7
0
def test_when_describe_hand_and_texas_holdem_and_high_card_then_correct_description_returned(
        hand, expected):
    hand = get_hand(hand)
    actual = describe_hand("Texas Holdem", "High Card", hand=hand)
    assert actual == expected
コード例 #8
0
def test_when_describe_hand_and_texas_holdem_and_two_pair_then_correct_description_returned(
        hand, expected):
    hand = get_hand(hand)
    actual = describe_hand("Texas Holdem", "Two Pair", hand=hand)
    assert actual == expected
コード例 #9
0
def test_when_describe_hand_and_texas_holdem_and_straight_then_correct_description_returned(
        hand, expected):
    hand = get_hand(hand)
    actual = describe_hand("Texas Holdem", "Straight", hand=hand)
    assert actual == expected
コード例 #10
0
def test_when_describe_hand_and_texas_holdem_and_full_house_then_correct_description_returned(
        hand, expected):
    hand = get_hand(hand)
    actual = describe_hand("Texas Holdem", "Full House", hand=hand)
    assert actual == expected