On Two Plus Two poker hand evaluator, how do you get the best 5 cards combination out of the 7 that you passed to it?

前端 未结 4 620

Is it possible to extract that info from the equivalence value?

I understand that the higher the equivalence value the better. Category and rank can also be extracte

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-31 20:47

    The twoplustwo hand evaluator can evaluate five cards hands. Here's the code for that in C#:

    int LookupFiveCardHand(int[] cards) {
        //assert cards size is 5
        int p = HR[53 + cards[i++]];
        p = HR[p + cards[i++]];
        p = HR[p + cards[i++]];
        p = HR[p + cards[i++]];
        p = HR[p + cards[i++]];
        return HR[p];
    }
    

    Notice there's 6 array look-ups despite there being 5 cards.

    Anyways, since the evaluator is so fast, you can just compare every possible 5 card combination. A hand containing 7 card hands will have twenty-one 5 card combinations. Code in C#:

    List GetBestFiveCards(List sevenCardHand) {
        List> fiveCardHandCombos = new List>();
    
        // adds all combinations of five cards to fiveCardHandCombos
        for (int i = 0; i < sevenCardHand.Count; i++) {
            for (int j = i+1; j < sevenCardHand.Count; j++) {
                List fiveCardCombo = new List(sevenCardHand);
                fiveCardHandCombos.RemoveAt(j); // j > i, so remove j first
                fiveCardHandCombos.RemoveAt(i);
                fiveCardHandCombos.Add(fiveCardCombo);
            }
        }
    
        Dictionary, int> comboToValue = new Dictionary, int>();
        for (int i = 0; i < fiveCardHandCombos.Count; i++) {
            comboToValue.Add(fiveCardHandCombos[i], LookupFiveCardHand(fiveCardHandCombos[i]));
        }
        int maxValue = comboToValue.Values.Max();
        return comboToValue.Where(x => x.Value == maxValue).Select(x => x.Key).First(); //grab only the first combo in the event there is a tie
    }
    

提交回复
热议问题