Example #1
0
def queryOpenAction(_hand, _minimumPotAfterOpen, _playersCurrentBet,
                    _playersRemainingChips):
    poker_hand = []

    ########### EVALUATE HAND ###########
    print '=== EVALUATION ==='

    for n in _hand:
        poker_hand.append(Card.new(n))

    Card.print_pretty_cards(poker_hand)

    score = DEUCES_EVALUATOR.evaluate([], poker_hand)
    rank = DEUCES_EVALUATOR.class_to_string(
        DEUCES_EVALUATOR.get_rank_class(score))
    print "Rank = " + rank
    ########### / EVALUATE HAND ###########

    ########### ESTIMATE WIN CHANCE ###########
    chance = round(
        (float(1) -
         Evaluator.get_five_card_rank_percentage(DEUCES_EVALUATOR, score)), 2)
    print "Chance to win =", chance * 100, "%"
    ########### / ESTIMATE WIN CHANCE ###########

    ########### ACTION DEPENDS ON WIN CHANCE ###########

    print("======= OPENING ACTION =======")

    # chance to win < 22%: Bet 1 coin up to 2% of stack size
    if (chance < 0.22):
        action = ClientBase.BettingAnswer.ACTION_OPEN, \
                 int(random.randint(0, round(0.05 * _playersRemainingChips) + 1) + _minimumPotAfterOpen)
    # chance to win < 33%: Bet 1 coin up to 10% of stack size
    elif (chance < 0.33):
        action = ClientBase.BettingAnswer.ACTION_OPEN, \
                 int(random.randint(0, round(0.1 * _playersRemainingChips) + 1) + _minimumPotAfterOpen)
    # chance to win < 66%: Bet 15% of stack size up to 20% of stack size
    elif (chance < 0.66):
        action = ClientBase.BettingAnswer.ACTION_OPEN, \
                 int(random.randint(round(0.15 * _playersRemainingChips),
                                    round(0.2 * _playersRemainingChips) + 1) + _minimumPotAfterOpen)
    # If chance is higher - check remaining chip stack:
    else:
        # Chip stack < 40: Go all in
        if (_playersRemainingChips < 0.4):
            action = ClientBase.BettingAnswer.ACTION_ALLIN
        # Otherwise: Bet 25% of stack size up to 30% of stack size
        else:
            action = ClientBase.BettingAnswer.ACTION_OPEN, \
                     int(random.randint(round(0.25 * _playersRemainingChips),
                                        round(0.3 * _playersRemainingChips) + 1) + _minimumPotAfterOpen)
    print 'GermanWings:', action
    return action
def get_winner(board, hands):
    for hand in hands:
        assert len(hand) == 2, "Inavlid hand length"

    best_rank = 7463  # rank one worse than worst hand
    winners = []
    evaluator = Evaluator()
    for player, hand in enumerate(hands):
        # evaluate current board position
        rank = evaluator.evaluate(hand, board)
        percentage = 1.0 - evaluator.get_five_card_rank_percentage(
            rank)  # higher better here
        # print "Player %d, percentage rank among all hands = %f" % (player + 1, percentage)

        # detect winner
        if rank == best_rank:
            winners.append(player + 1)
            best_rank = rank
        elif rank < best_rank:
            winners = [player + 1]
            best_rank = rank
    return winners
Example #3
0
def queryCallRaiseAction(_hand, _maximumBet, _minimumAmountToRaiseTo,
                         _playersCurrentBet, _playersRemainingChips):
    poker_hand = []

    ########### EVALUATE HAND ###########
    print '=== EVALUATION ==='

    for n in _hand:
        poker_hand.append(Card.new(n))

    Card.print_pretty_cards(poker_hand)

    score = DEUCES_EVALUATOR.evaluate([], poker_hand)
    rank = DEUCES_EVALUATOR.class_to_string(
        DEUCES_EVALUATOR.get_rank_class(score))
    print "Rank = " + rank
    ########### / EVALUATE HAND ###########

    ########### ESTIMATE WIN CHANCE ###########
    chance = round(
        (float(1) -
         Evaluator.get_five_card_rank_percentage(DEUCES_EVALUATOR, score)), 2)
    print "Chance to win =", chance * 100, "%"
    ########### / ESTIMATE WIN CHANCE ###########

    ########### ACTION DEPENDS ON WIN CHANCE ###########

    print("======= CALL/RAISE ACTION =======")

    # chance to win < 10%: Fold
    if chance < 0.1:
        action = ClientBase.BettingAnswer.ACTION_FOLD

    # chance to win: 10% <= w < 20%
    elif chance < 0.2:
        # Opponent raise > 20 coins: Fold
        if (_maximumBet > 20):
            action = ClientBase.BettingAnswer.ACTION_FOLD
        # Otherwise: Call
        else:
            action = ClientBase.BettingAnswer.ACTION_CALL

    # chance to win: 20% <= w < 40%
    elif (chance < 0.40):
        # bet < 10% of player stack size: Raise up to 2% of stack size
        if _maximumBet < 0.10 * _playersRemainingChips:
            action = ClientBase.BettingAnswer.ACTION_RAISE, int(
                _minimumAmountToRaiseTo + 0.02 * _playersRemainingChips)
        # bet < 15% of player stack size: Call
        elif _maximumBet < 0.15 * _playersRemainingChips:
            action = ClientBase.BettingAnswer.ACTION_CALL
        # for higher bets: Fold
        else:
            action = ClientBase.BettingAnswer.ACTION_FOLD

    # chance to win: 40% <= w < 60%
    elif (chance < 0.6):
        # bet < 20% of player stack size: Raise up to 5% of stack size
        if _maximumBet < 0.2 * _playersRemainingChips:
            action = ClientBase.BettingAnswer.ACTION_RAISE, int(
                _minimumAmountToRaiseTo + 0.05 * _playersRemainingChips)
        # bet < 50% of player stack size: Call
        elif _maximumBet < 0.5 * _playersRemainingChips:
            action = ClientBase.BettingAnswer.ACTION_CALL
        # for higher bets: go Allin
        else:
            action = ClientBase.BettingAnswer.ACTION_ALLIN

    # chance to win: 60% <= w < 80%
    elif (chance < 0.8):
        # bet < 60% of player stack size: Raise up to 10% of stack size
        if (_maximumBet < 0.6 * _playersRemainingChips):
            action = ClientBase.BettingAnswer.ACTION_RAISE, int(
                _minimumAmountToRaiseTo + 0.1 * _playersRemainingChips)
        # player stack size > minimum amount to raise: Call
        elif (_playersRemainingChips > _minimumAmountToRaiseTo):
            action = ClientBase.BettingAnswer.ACTION_CALL
        # Otherwise: go AllIn
        else:
            action = ClientBase.BettingAnswer.ACTION_ALLIN

    # chance to win: w >= 80%
    else:
        # player stack size > (minimum amount to raise + 20% of stack size): Raise up to 15% of stack size
        if (_playersRemainingChips >
                _minimumAmountToRaiseTo + 0.2 * _playersRemainingChips):
            action = ClientBase.BettingAnswer.ACTION_RAISE, int(
                _minimumAmountToRaiseTo + 0.15 * _playersRemainingChips)
        # Otherwise: go AllIn
        else:
            action = ClientBase.BettingAnswer.ACTION_ALLIN

    ########### / ACTION ###########

    print 'GermanWings:', action
    return action
def main():
    count = int(sys.argv[1])
    name = sys.argv[2]
    verbose = True

    samples_for_hand = open('{}_hand.txt'.format(name), 'w')
    samples_for_board3 = open('{}_board3.txt'.format(name), 'w')
    samples_for_board4 = open('{}_board4.txt'.format(name), 'w')
    samples_for_board5 = open('{}_board5.txt'.format(name), 'w')

    index = 0
    while index < count:
        #
        player_number = 5
        hands = []
        hands_features = []
        board3_features = []
        board4_features = []
        board5_features = []

        deck = Deck()
        for i in range(0, player_number):
            hands.append(deck.draw(2))
            if verbose:
                print("Player {}:".format(i + 1))
                print_deuces_card(hands[i])
            # pf0 = convert_card_to_feature(hands[i][0])
            # pf1 = convert_card_to_feature(hands[i][1])

            rank, features = get_rank_of_hand_cards(hands[i][0], hands[i][1])
            print(features)
            hands_features.append(features)

        board = deck.draw(3)
        if verbose:
            print("Board 3 (FLOP):")
            print_deuces_card(board)
        bf0 = convert_card_to_feature(board[0])
        bf1 = convert_card_to_feature(board[1])
        bf2 = convert_card_to_feature(board[2])

        evaluator = Evaluator()

        board3_features = copy.deepcopy(hands_features)
        assert len(hands) == len(
            board3_features
        ), "ERROR: length of hand and features does not match!"
        for i in range(0, len(hands)):
            board3_features[i].update(bf0)
            board3_features[i].update(bf1)
            board3_features[i].update(bf2)

            rank = evaluator.evaluate(board, hands[i])
            percentage = 1.0 - evaluator.get_five_card_rank_percentage(rank)
            board3_features[i][100] = percentage
            # print("Board3 percentage: {}".format(percentage))

        # -----------
        board.append(deck.draw(1))
        if verbose:
            print("Board 4 (TURN):")
            print_deuces_card(board)
        bf0 = convert_card_to_feature(board[0])
        bf1 = convert_card_to_feature(board[1])
        bf2 = convert_card_to_feature(board[2])
        bf3 = convert_card_to_feature(board[3])

        board4_features = copy.deepcopy(board3_features)
        for i in range(0, len(hands)):
            board4_features[i].update(bf3)

            rank = evaluator.evaluate(board, hands[i])
            percentage = 1.0 - evaluator.get_five_card_rank_percentage(rank)
            board4_features[i][101] = percentage

            gap_board_4 = get_gap_between_hands_and_board(hands[i], board)
            board4_features[i][110] = gap_board_4 / 2000

        # -----------
        board.append(deck.draw(1))
        if verbose:
            print("Board 5 (RIVER):")
            print_deuces_card(board)
        bf0 = convert_card_to_feature(board[0])
        bf1 = convert_card_to_feature(board[1])
        bf2 = convert_card_to_feature(board[2])
        bf3 = convert_card_to_feature(board[3])
        bf4 = convert_card_to_feature(board[4])

        board5_features = copy.deepcopy(board4_features)
        for i in range(0, len(hands)):
            board5_features[i].update(bf4)

            rank = evaluator.evaluate(board, hands[i])
            percentage = 1.0 - evaluator.get_five_card_rank_percentage(rank)
            board5_features[i][102] = percentage

            gap_board_5 = get_gap_between_hands_and_board(hands[i], board)
            board5_features[i][111] = gap_board_5 / 2000

        #
        print("\n")
        evaluator = Evaluator()
        if verbose:
            evaluator.hand_summary(board, hands)

        winners = get_winner(board, hands)
        print("Winner: {}".format(winners))

        # dump hands_features into file
        for i in range(0, len(hands_features)):
            if i + 1 in winners:
                libsvm_feature = convert_to_libsvm_format(1, hands_features[i])
            else:
                libsvm_feature = convert_to_libsvm_format(0, hands_features[i])
            samples_for_hand.write(libsvm_feature)

        # dump board3_features
        for i in range(0, len(board3_features)):
            if i + 1 in winners:
                libsvm_feature = convert_to_libsvm_format(
                    1, board3_features[i])
            else:
                libsvm_feature = convert_to_libsvm_format(
                    0, board3_features[i])
            samples_for_board3.write(libsvm_feature)

        # dump board4_features
        for i in range(0, len(board4_features)):
            if i + 1 in winners:
                libsvm_feature = convert_to_libsvm_format(
                    1, board4_features[i])
            else:
                libsvm_feature = convert_to_libsvm_format(
                    0, board4_features[i])
            samples_for_board4.write(libsvm_feature)

        # dump board5_features
        for i in range(0, len(board5_features)):
            if i + 1 in winners:
                libsvm_feature = convert_to_libsvm_format(
                    1, board5_features[i])
            else:
                libsvm_feature = convert_to_libsvm_format(
                    0, board5_features[i])
            samples_for_board5.write(libsvm_feature)

        index += 1

    #
    samples_for_hand.close()
    samples_for_board3.close()
    samples_for_board4.close()
    samples_for_board5.close()